Ejemplo n.º 1
0
        public async Task <string> UpdateImageAsync(int id, IFormFile image)
        {
            var product = await _context.Products.FindAsync(id);

            if (product == null)
            {
                return(null);
            }

            var folder = [email protected]();

            var oldThumbnail = product.Thumbnail;

            try
            {
                var fileName = await _imageWriter.UploadImageAsync(folder, image);

                product.Thumbnail = fileName;

                await _context.SaveChangesAsync();

                // Remove old image
                if (oldThumbnail != null)
                {
                    _imageWriter.DeleteImage(folder, oldThumbnail);
                }

                return(ImageHelper.GetRelativeStaticFolderImagePath(fileName));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves a new profile picture for a user on disk, and removes the old image from disk.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="image"></param>
        /// <returns></returns>
        public async Task <string> UpdateImageAsync(int id, IFormFile image)
        {
            var user = await _context.Users.FindAsync(id);

            if (user == null)
            {
                return(null);
            }

            var folder = [email protected]();

            var oldThumbnail = user.Thumbnail;

            try
            {
                var fileName = await _imageWriter.UploadImageAsync(folder, image);

                user.Thumbnail = fileName;

                await _context.SaveChangesAsync();

                // Remove old image
                if (oldThumbnail != null)
                {
                    _imageWriter.DeleteImage(folder, oldThumbnail);
                }

                return(fileName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> UploadProductImages(IList <IFormFile> files, long productId)
        {
            if (files.Count == 0)
            {
                return(BadRequest());
            }

            IList <Image> images = new List <Image>();

            foreach (IFormFile file in files)
            {
                if (imageWriter.IsImageFile(file))
                {
                    //Create unique file name with UUID and file extension
                    string fileName       = Path.GetFileName(file.FileName);
                    string extension      = Path.GetExtension(fileName);
                    string uniqueFileName = Guid.NewGuid().ToString() + extension;

                    //Get file path in wwwroot folder
                    string imageUrl = "\\images\\products\\" + uniqueFileName;

                    //Get absolute path
                    string uploads = Path.Combine(_appHostingEnv.WebRootPath, "images", "products");
                    string path    = Path.Combine(uploads, uniqueFileName);

                    //Upload image on the server
                    await imageWriter.UploadImageAsync(file, path);

                    imageRepository.AddImage(new Image {
                        Name = uniqueFileName, Url = imageUrl, ProductID = productId
                    });
                    //Add new image in list to return back to client
                    Image image = new Image
                    {
                        Name = uniqueFileName,
                        Url  = imageUrl
                    };
                    images.Add(image);
                }
            }
            return(Ok(images));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> UploadImage(ProductImageVM model)
        {
            string avatar = await _imageWriter.UploadImageAsync(model.File);

            if (await _productService.UpdateAvatar(new Product()
            {
                Id = model.Id, Avatar = avatar
            }))
            {
                return(Ok(true));
            }
            return(BadRequest("Lo sentimos , no se ha podido agregar la imagen"));
        }
        public async Task <IActionResult> UploadImage([FromQuery] ImageModel file)
        {
            if (file is null || file.Image.Length == 0 || string.IsNullOrEmpty(file.ApartmentId))
            {
                return(BadRequest());
            }

            string ownerId = HttpContext.GetUserId();

            try
            {
                var result = await _imageWriter.UploadImageAsync(file.Image, file.ApartmentId, ownerId);

                return(result.IsError
                    ? throw new InvalidOperationException(result.Message)
                    : !result.IsSuccess
                    ? BadRequest(result.Message)
                    : (IActionResult)Ok(result.Data));
            }
            catch (InvalidOperationException ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> UploadImage(IFormFile file)
        {
            var result = await _imageWriter.UploadImageAsync(file);

            return(new ObjectResult(result));
        }