示例#1
0
        /// <summary>
        /// Method to get a list of associated Sections of an User.
        /// </summary>
        /// <returns>WPF Web Server response data of list of Sections.</returns>
        public override WebServerResponseData Index()
        {
            log.Debug($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name}");
            log.Info($"{MethodBase.GetCurrentMethod().Name} : Serving Picture request. Please wait...");

            if (!IsAuth())
            {
                log.Info($"{MethodBase.GetCurrentMethod().Name} : Return response user not auth.");
                return(ResponseNotAuth());
            }

            try
            {
                var a = Uri.QueryString;

                // Check if request params are correct.
                if (Uri.QueryString.Count == 0)
                {
                    log.Info($"{MethodBase.GetCurrentMethod().Name} : The Request is empty.");
                    log.Info($"{MethodBase.GetCurrentMethod().Name} : Return response => Picture not found or doesn't exists.");
                    return(ResponseError404("Picture not found or doesn't exists."));
                }

                // Check if request params are correct.
                if (!Uri.QueryString.Keys.Cast <string>().Contains("id"))
                {
                    log.Info($"{MethodBase.GetCurrentMethod().Name} : Request param [id] not found.");
                    log.Info($"{MethodBase.GetCurrentMethod().Name} : Return response => Picture not found or doesn't exists.");
                    return(ResponseError404("Picture not found or doesn't exists."));
                }

                int    id   = int.Parse(Uri.QueryString["id"]);
                string type = Uri.QueryString["type"];

                // Get user and dependencies.
                UserEntity    user   = GetAuthUser();
                PictureEntity entity = RouteHelper.GetUserPicture(id, user);

                var aa = entity;

                // Check if request params are correct.
                if (entity == null)
                {
                    log.Info($"{MethodBase.GetCurrentMethod().Name} : Request Picture not found.");
                    log.Info($"{MethodBase.GetCurrentMethod().Name} : Return response => Picture not found or doesn't exists.");
                    return(ResponseError404("Picture not found or doesn't exists."));
                }

                Response = new WebServerResponseData(Uri.AbsoluteUrl);

                if (File.Exists(entity.PicturePath))
                {
                    Response.ServeFileUnSafe(entity.PicturePath);
                }
                else
                {
                    // Convert the image to byte[]
                    MemoryStream stream = new MemoryStream();
                    Properties.Images.error_404.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] imageBytes = stream.ToArray();

                    // Add the bytes to the response.
                    Response.SetContentType("error-404.png");
                    Response.ContentAppend(imageBytes);
                }

                return(Response);
            }
            catch (Exception e)
            {
                log.Fatal($"{MethodBase.GetCurrentMethod().Name} : Serving picture request failed.", e);
                return(ResponseError500());
            }
        }