예제 #1
0
        public async Task <IActionResult> UploadAsync(int vehicleId, IFormFile file)
        {
            if (file == null)
            {
                return(BadRequest("Null file"));
            }
            if (file.Length == 0)
            {
                return(BadRequest("Empty file"));
            }
            if (file.Length > _photoSettings.MaxBytes)
            {
                return(BadRequest("Maximum file size exceeded!"));
            }

            var extension = Path.GetExtension(file.FileName);

            if (!_photoSettings.IsSupported(extension))
            {
                return(BadRequest("Not allowed file type!"));
            }

            var vehicle = await _vehicleRepository.GetVehicleAsync(vehicleId);

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

            var uploadsFolderPath = Path.Combine(_host.WebRootPath, "uploads");
            var photo             = await _photoManager.UploadPhotoAsync(vehicle, file, uploadsFolderPath);

            return(Ok(_mapper.Map <Photo, PhotoResource>(photo)));
        }