private IServiceResult ValidateImageFile(IFormFile file)
        {
            try
            {
                if (file == null || file.Length == 0)
                {
                    throw new NullOrEmptyException(nameof(file), file);
                }

                if (file.Length > _imageSettings.MaxByte)
                {
                    throw new OversizeException(nameof(file), _imageSettings.MaxByte);
                }

                if (!_imageSettings.IsSupported(file.FileName))
                {
                    throw new NotSupportException(nameof(file), Path.GetExtension(file.FileName));
                }

                _logger.LogInformation($"Validate image with name: {file.FileName} successfully.");
                return(new ServiceResult());
            }
            catch (Exception e)
            {
                _logger.LogError($"Validate image failed. {e.Message}");
                return(new ServiceResult(false, e.Message));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Upload(int orderId, IFormFile fileImage)
        {
            var order = await _repository.GetOrder(orderId, includeRelated : false);

            if (order == null)
            {
                return(NotFound());
            }

            if (fileImage == null)
            {
                return(BadRequest("Null Image"));
            }

            if (fileImage.Length == 0)
            {
                return(BadRequest("Empty File"));
            }

            if (fileImage.Length > imageSettings.MaxSize)
            {
                return(BadRequest("Maximum sixe exceeded"));
            }

            if (!imageSettings.IsSupported(fileImage.FileName))
            {
                return(BadRequest("Invalid type"));
            }

            var pathUpload = Path.Combine(_host.WebRootPath, "uploads");

            if (!Directory.Exists(pathUpload))
            {
                Directory.CreateDirectory(pathUpload);
            }

            var fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileImage.FileName);

            var filePath = Path.Combine(pathUpload, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await fileImage.CopyToAsync(stream);
            }

            var image = new Image {
                FileName = fileName
            };

            order.Images.Add(image);

            await _uow.CompleteAsync();

            return(Ok(_mapper.Map <Image, ImageResource>(image)));
        }
        public IActionResult Upload(int Id, IFormFile file)
        {
            var movie = _uow.Movies.GetById(Id);

            if (movie == null)
            {
                return(NotFound());
            }

            if (file == null)
            {
                return(BadRequest("File not valid"));
            }
            if (file.Length == 0)
            {
                return(BadRequest("Empty File"));
            }
            if (file.Length > _options.MaxBytes)
            {
                return(BadRequest("File exceeded 10 MB size!"));
            }

            if (!_options.IsSupported(file.FileName))
            {
                return(BadRequest("Invalid File Type"));
            }
            var uploadsFolder = Path.Combine(_host.WebRootPath, "uploads");

            if (!Directory.Exists(uploadsFolder))
            {
                Directory.CreateDirectory(uploadsFolder);
            }

            var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var filepath = Path.Combine(uploadsFolder, fileName);

            using (var stream = new FileStream(filepath, FileMode.Create))
            {
                file.CopyTo(stream);
            }

            var image = new Image {
                FileName = fileName
            };

            movie.Images.Add(image);
            _uow.Commit();
            return(Ok(image));
        }
Beispiel #4
0
        public async Task <IActionResult> UpLoad(string productId, IFormFile file)
        {
            bool isValid = Guid.TryParse(productId, out var id);

            if (!isValid)
            {
                return(InvalidId(productId));
            }

            var product = await _unitOfWork.ProductRepository.GetProduct(id, false);

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

            if (file == null || file.Length == 0)
            {
                return(NullOrEmpty());
            }

            if (file.Length > _imageSettings.MaxByte)
            {
                return(InvalidSize(ApplicationConstants.ControllerName.Image, _imageSettings.MaxByte));
            }

            if (!_imageSettings.IsSupported(file.FileName))
            {
                return(UnSupportedType(_imageSettings.AcceptedTypes));
            }


            var uploadFolderPath = Path.Combine(_host.WebRootPath, "images");

            // Create a folder if the folder does not exist
            if (!Directory.Exists(uploadFolderPath))
            {
                Directory.CreateDirectory(uploadFolderPath);
            }

            var fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);

            var filePath = Path.Combine(uploadFolderPath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            var image = new Image {
                FileName = fileName, ProductId = id
            };
            await _unitOfWork.ImageRepository.AddAsync(image);

            if (!await _unitOfWork.CompleteAsync())
            {
                _logger.LogMessage(LoggingEvents.Fail, ApplicationConstants.ControllerName.Image, image.Id);
                return(FailedToSave(image.Id));
            }
            _logger.LogMessage(LoggingEvents.Created, ApplicationConstants.ControllerName.Image, image.Id);

            return(Ok(_mapper.Map <Image, ImageResource>(image)));
        }