예제 #1
0
        public HttpResponseMessage PostImage(int id)
        {
            if (!planService.ContainsId(id))
            {
                const string errorMessage = "No plan with this id in database.";
                return(Request.CreateResponse(HttpStatusCode.NoContent, errorMessage));
            }

            if (HttpContext.Current.Request.Files.Count != 1)
            {
                const string errorMessage = "Only one image can be sent.";
                return(Request.CreateResponse(HttpStatusCode.BadRequest, errorMessage));
            }

            try
            {
                var postedFile = HttpContext.Current.Request.Files[0];
                if (postedFile.ContentLength > 0)
                {
                    var allowedFileExtensions = new List <string>(Constants.ImageRestrictions.Extensions);

                    var extension = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.')).ToLower();
                    if (!allowedFileExtensions.Contains(extension))
                    {
                        const string errorMessage = "Types allowed only .jpeg .jpg .png";
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, errorMessage));
                    }

                    const int maxContentLength = Constants.ImageRestrictions.MaxSize;
                    if (postedFile.ContentLength > maxContentLength)
                    {
                        const string errorMessage = "Please Upload a file upto 1 mb.";
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, errorMessage));
                    }

                    byte[] imageData;
                    using (var binaryReader = new BinaryReader(postedFile.InputStream))
                    {
                        imageData = binaryReader.ReadBytes(postedFile.ContentLength);
                    }

                    planService.SetImage(id, imageData, postedFile.FileName);
                    const string okMessage = "Successfully created image.";
                    return(Request.CreateResponse(HttpStatusCode.OK, okMessage));
                }
                const string emptyImageMessage = "Empty image.";
                return(Request.CreateErrorResponse(HttpStatusCode.NotModified, emptyImageMessage));
            }
            catch (EntityException e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }
        public async Task <ActionResult> PostImageAsync(int id)
        {
            if (!await planService.ContainsId(id))
            {
                return(NoContent());
            }
            if (_accessor.HttpContext.Request.Form.Files.Count != 1)
            {
                string errorMessage = "Only one image can be sent.";
                return(BadRequest(errorMessage));
            }
            try
            {
                var postedFile = _accessor.HttpContext.Request.Form.Files[0];
                if (postedFile.Length > 0)
                {
                    var allowedFileExtensions = new List <string>(Constants.ImageRestrictions.Extensions);
                    var extension             = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.')).ToLower();
                    if (!allowedFileExtensions.Contains(extension))
                    {
                        string errorMessage = "Types allowed only .jpeg .jpg .png";
                        return(BadRequest(errorMessage));
                    }
                    const int maxContentLength = Constants.ImageRestrictions.MaxSize;
                    if (postedFile.Length > maxContentLength)
                    {
                        string errorMessage = "Please Upload a file upto 1 mb.";
                        return(BadRequest(errorMessage));
                    }
                    byte[] imageData;
                    using (var binaryReader = new BinaryReader(postedFile.OpenReadStream()))
                    {
                        imageData = binaryReader.ReadBytes(Convert.ToInt32(postedFile.Length));
                    }
                    await planService.SetImageAsync(id, imageData, postedFile.FileName);

                    string okMessage = "Successfully created image.";
                    return(Ok(okMessage));
                }
                return(StatusCode(304));
            }
            catch (Exception e)
            {
                return(BadRequest());
            }
        }