Esempio n. 1
0
        public async Task <IActionResult> Upload(long parameterId, IFormFile file)
        {
            var parameter = await _parameterRepository.GetAsync(parameterId);

            if (parameter == null)
            {
                return(NotFound());
            }
            if (file == null)
            {
                return(BadRequest("Null file"));
            }
            if (file.Length == 0)
            {
                return(BadRequest("Empty file"));
            }
            if (file.Length > _photoSettings.MaxBytes)
            {
                return(BadRequest("Max file size exceeded"));
            }
            if (!_photoSettings.IsSupported(file.FileName))
            {
                return(BadRequest("Invalid file type"));
            }

            string uploadsFolderPath = Path.Combine(_host.WebRootPath, "clientapp", "uploads", "parameters");

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

            string fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            string filePath = Path.Combine(uploadsFolderPath, fileName);

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

            var figure = new Figure {
                FileName = fileName
            };
            var parameterPhoto = new ParameterFigure {
                Parameter = parameter, Figure = figure
            };

            parameter.ParameterFigures.Add(parameterPhoto);

            await _unitOfWork.CompleteAsync();

            return(Ok(_mapper.Map <Figure, FigureResource>(figure)));
        }
            public async Task <Unit> Handle(UploadFigureCommand request, CancellationToken cancellationToken)
            {
                var parameter = await _parameterRepository.GetAsync(request.ParameterId);

                if (parameter == null)
                {
                    throw new NotFoundException(nameof(Figure), request.ParameterId);
                }
                if (request.File == null)
                {
                    throw new BadRequestException(nameof(Figure), "Null file");
                }
                if (request.File.Length == 0)
                {
                    throw new BadRequestException(nameof(Figure), "Empty file");
                }
                if (request.File.Length > _photoSettings.MaxBytes)
                {
                    throw new BadRequestException(nameof(Figure), "Max file size exceeded");
                }
                if (!_photoSettings.IsSupported(request.File.FileName))
                {
                    throw new BadRequestException(nameof(Figure), "Invalid file type");
                }

                string uploadsFolderPath = Path.Combine(_host.WebRootPath, "clientapp", "uploads", "parameters");

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

                string fileName = Guid.NewGuid().ToString() + Path.GetExtension(request.File.FileName);
                string filePath = Path.Combine(uploadsFolderPath, fileName);

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

                var figure = new Figure {
                    FileName = fileName
                };
                var parameterPhoto = new ParameterFigure {
                    Parameter = parameter, Figure = figure
                };

                parameter.ParameterFigures.Add(parameterPhoto);

                await _unitOfWork.CompleteAsync();

                return(Unit.Value);
            }