示例#1
0
        public IActionResult Put(string id, [FromBody] BrandImage image)
        {
            BrandImageBO brandImageBO;
            ObjectResult response;

            try
            {
                _log.LogInformation($"Starting Put( {id}, '{JsonConvert.SerializeObject(image, Formatting.None)}')");

                brandImageBO = new BrandImageBO(_loggerFactory, _config);

                image.BlobFile.ID = id;
                image             = brandImageBO.Update(image);

                response = Ok(image);

                _log.LogInformation($"Finishing Put( {id} )");
            }
            catch (Exception ex)
            {
                _log.LogError(ex.Message);
                response = StatusCode(500, ex.Message);
            }

            return(response);
        }
        public void ProcessRequest(HttpContext context)
        {
            string brandPhotoId = "";

            brandPhotoId = context.Request.QueryString["id"];
            if (brandPhotoId == "")
            {
                brandPhotoId = "207";
            }
            BrandManager bm = new BrandManager();
            BrandImage   bi = new BrandImage();

            if (context.Request.QueryString["height"] != null)
            {
                try
                {
                    height = int.Parse(context.Request.QueryString["height"]);
                }
                catch
                {
                    height = 0;
                }
            }

            if (context.Request.QueryString["width"] != null)
            {
                try
                {
                    width = int.Parse(context.Request.QueryString["width"]);
                }
                catch
                {
                    width = 0;
                }
            }

            if (width <= 0 && height <= 0)
            {
                context.Response.Clear();
                bi = bm.GetOneBrandImage(brandPhotoId);
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + bi.BrandImageFileName);
                context.Response.ContentType = bi.BrandImageContentType;
                context.Response.BinaryWrite(bi.Photo);
                context.Response.End();
            }
            else
            {
                context.Response.Clear();
                bi = bm.GetOneBrandImage(brandPhotoId);
                context.Response.ContentType =
                    bi.BrandImageContentType;
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + bi.BrandImageFileName);
                byte[] buffer =
                    ResizeImage(bi.Photo, width, height, true);
                context.Response.OutputStream.Write
                    (buffer, 0, buffer.Length);
                context.Response.End();
            }
        }
        public IActionResult Update([FromForm] BrandImage carImage, [FromForm(Name = ("Image"))] IFormFile file)
        {
            var result = _brandImageService.Update(file, carImage);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
        public IResult Delete(BrandImage brandImage)
        {
            IResult result = BusinessRules.Run(BrandImageDelete(brandImage));

            if (result != null)
            {
                return(result);
            }
            _brandImageDal.Delete(brandImage);
            return(new SuccessResult());
        }
示例#5
0
        public BrandImage Update(BrandImage image)
        {
            BrandBO    brandBO;
            BlobFileBO blobFileBO;
            Brand      brand;

            try
            {
                brandBO    = new BrandBO(_loggerFactory, _config);
                blobFileBO = new BlobFileBO(_loggerFactory, _config);

                if (string.IsNullOrEmpty(image.BlobFile?.ID))
                {
                    throw new Exception("ID vazio, avalie a utilização do POST");
                }
                else
                {
                    brand = brandBO.Get(image.BrandID);

                    if (brand != null)
                    {
                        image.BlobFile = blobFileBO.Update(image.BlobFile);

                        switch (image.Destination)
                        {
                        case BrandImage.BrandImageDestination.Desktop:
                            brand.DesktopSpotlightImageID = image.BlobFile.ID;
                            break;

                        case BrandImage.BrandImageDestination.Mobile:
                            brand.MobileSpotlightImageID = image.BlobFile.ID;
                            break;

                        default:
                            break;
                        }

                        brandBO.Update(brand);
                    }
                    else
                    {
                        throw new Exception("Produto não encontrado");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(image);
        }
        //File
        public IResult Add(IFormFile file, BrandImage brandImage)
        {
            IResult result = BusinessRules.Run(CheckImageLimitExceeded(brandImage.BrandId));

            if (result != null)
            {
                return(result);
            }
            brandImage.ImagePath = FileHelper.Add(file);
            brandImage.Date      = DateTime.Now;
            _brandImageDal.Add(brandImage);
            return(new SuccessResult());
        }
        private IResult BrandImageDelete(BrandImage brandImage)
        {
            try
            {
                File.Delete(brandImage.ImagePath);
            }
            catch (Exception exception)
            {
                return(new ErrorResult(exception.Message));
            }

            return(new SuccessResult());
        }
示例#8
0
        private BrandImage ImageFromData(FileData fileData)
        {
            var newImage = new BrandImage()
            {
                File = new Entities.File
                {
                    FileName    = fileData.FileName,
                    ContentType = fileData.ContentType
                }
            };

            return(newImage);
        }
        public IResult Update(IFormFile file, BrandImage brandImage)
        {
            IResult result = BusinessRules.Run(CheckImageLimitExceeded(brandImage.BrandId));

            if (result != null)
            {
                return(result);
            }
            brandImage.Date = DateTime.Now;
            string oldPath = Get(brandImage.Id).Data.ImagePath;

            brandImage.ImagePath = FileHelper.Update(oldPath, file);
            return(new SuccessResult(Messages.BrandImageUpdated));
        }
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                if (HttpContext.Current.Request.Files.AllKeys.Any())
                {
                    BrandManager brandManager = new BrandManager();
                    BrandImage   brandImage   = new BrandImage();
                    int          numOfFiles   = HttpContext.Current.Request.Files.Count;
                    // Get the uploaded image from the Files collection
                    for (int index = 0; index < numOfFiles; index++)
                    {
                        HttpPostedFile httpPostedFile =
                            HttpContext.Current.Request.Files[index] as HttpPostedFile;

                        if (httpPostedFile != null)
                        {
                            //Converting posted file into a byte array

                            using (var binaryReader = new BinaryReader(httpPostedFile.InputStream))
                            {
                                brandImage = new BrandImage();



                                brandImage.BrandID                 = Int32.Parse(HttpContext.Current.Request.Form["BrandId"].ToString());
                                brandImage.BrandImageFileName      = httpPostedFile.FileName;
                                brandImage.BrandImageContentLength = httpPostedFile.ContentLength;
                                brandImage.BrandImageContentType   = httpPostedFile.ContentType;
                                //Call the ReadBytes method of the binaryReader (which has the file information)
                                //to begin writing all the file data into a byte array with the correct size (I used the content length info)
                                brandImage.Photo = binaryReader.ReadBytes(brandImage.BrandImageContentLength);
                            }

                            try
                            {
                                brandManager.AddBrandPhoto(brandImage);
                            }
                            catch (Exception ex)
                            {
                                var failResponse = new
                                {
                                    status  = "error",
                                    message = "Unable to add photo. " +
                                              "Keep calm. Try again. " +
                                              "If problem persist, contact adminstrator"
                                };
                                context.Response.ContentType = "application/json";
                                context.Response.Write(JsonConvert.SerializeObject(failResponse));
                                return;
                            }
                        }
                    }//end of foreach block to save each product photo
                    var successResponse = new
                    {
                        status  = "success",
                        message = "Created " + numOfFiles + " photos."
                    };
                    context.Response.ContentType = "application/json";
                    context.Response.Write(JsonConvert.SerializeObject(successResponse));
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(new KeyValuePair <bool, string>(false, "An error occurred while uploading the file. Error Message: " + ex.Message));
            }
        }//End of ProcessRequest method
        public void ProcessRequest(HttpContext context)
        {
            if (HttpContext.Current.Request.Files.AllKeys.Any())
            {
                BrandManager bm    = new BrandManager();
                BrandImage   bi    = new BrandImage();
                Brand        brand = new Brand();
                brand.BrandID = Int32.Parse(HttpContext.Current.Request.Form["BrandId"].ToString());
                int numOfFiles = HttpContext.Current.Request.Files.Count;
                // Get the uploaded image from the Files collection
                for (int index = 0; index < numOfFiles; index++)
                {
                    HttpPostedFile httpPostedFile =
                        HttpContext.Current.Request.Files[index] as HttpPostedFile;

                    if (httpPostedFile != null)
                    {
                        //Reference: http://stackoverflow.com/questions/359894/how-to-create-byte-array-from-httppostedfile
                        //Converting posted file into a byte array

                        using (var binaryReader = new BinaryReader(httpPostedFile.InputStream))
                        {
                            bi = new BrandImage();

                            if (HttpContext.Current.Request.Form["NEW_" + index.ToString()].ToString() == "1")
                            {
                                bi.IsPrimaryPhoto = true;
                            }
                            else
                            {
                                bi.IsPrimaryPhoto = false;
                            }
                            bi.Brand.BrandID           = brand.BrandID;
                            bi.BrandImageFileName      = httpPostedFile.FileName;
                            bi.BrandImageContentLength = httpPostedFile.ContentLength;
                            bi.BrandImageContentType   = httpPostedFile.ContentType;
                            //Call the ReadBytes method of the binaryReader (which has the file information)
                            //to begin writing all the file data into a byte array with the correct size (I used the content length info)
                            bi.Photo = binaryReader.ReadBytes(bi.BrandImageContentLength);
                        }

                        try
                        {
                            bm.AddBrandPhoto(bi);
                        }
                        catch (Exception ex)
                        {
                            var failResponse = new
                            {
                                status  = "error",
                                message = "Unable to add photo. " +
                                          "Keep calm. Try again. " +
                                          "If problem persist, contact us at [email protected]"
                            };
                            context.Response.ContentType = "application/json";
                            context.Response.Write(JsonConvert.SerializeObject(failResponse));
                            return;
                        }
                    }
                }    //end of foreach block to save each product photo
                var successResponse = new
                {
                    status  = "success",
                    message = "Created a new Brand record with " + numOfFiles + " photos"
                };
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(successResponse));
            }
        }
示例#12
0
 public IResult Upgrade(BrandImage entity)
 {
     throw new NotImplementedException();
 }