public async Task <IActionResult> Upload(int vehicleId, IFormFile file) { var vehicle = await repository.GetVehicle(vehicleId, includeRelated : false); if (vehicle == null) { return(NotFound()); } if (string.IsNullOrWhiteSpace(host.WebRootPath)) { host.WebRootPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"); } if (file == null) { return(BadRequest("Null file")); } if (file.Length == 0) { return(BadRequest("Empty file")); } if (file.Length > MAX_BYTES) { return(BadRequest("Max file size exceeded")); } if (!ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(file.FileName))) { return(BadRequest("Invalid file type.")); } var uploadsFolderPath = Path.Combine(host.WebRootPath, "uploads"); if (!Directory.Exists(uploadsFolderPath)) { Directory.CreateDirectory(uploadsFolderPath); } var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var filePath = Path.Combine(uploadsFolderPath, fileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } var photo = new VehiclePhoto { FileName = fileName }; vehicle.VehiclePhotos.Add(photo); await unitOfWork.CompleteAsync(); return(Ok(mapper.Map <VehiclePhoto, VehiclePhotoResource>(photo))); }
public async Task <IActionResult> AddPhoto([FromRoute] int vehicleId, [FromForm] IFormFile file) { if (vehicleId == default || file == null) { return(BadRequest()); } if (await Repository.GetVehicle(vehicleId) == default) { return(NotFound()); } if (file.Length == 0) { return(BadRequest("empty file")); } if (file.Length > PhotosSettings.MaxBytes) { return(BadRequest("big file")); } if (!PhotosSettings.IsSupportedFileType(file.FileName)) { return(BadRequest("not supported type")); } string fileRootPath = Path.Combine(Env.WebRootPath, "Photos"); if (!Directory.Exists(fileRootPath)) { Directory.CreateDirectory(fileRootPath); } string fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); string fullFilePath = Path.Combine(fileRootPath, fileName); using (FileStream sw = new FileStream(fullFilePath, FileMode.Create)) { await file.CopyToAsync(sw); } Image thumb; using (var s = new FileStream(fullFilePath, FileMode.Open)) { Image img = Image.FromStream(s); thumb = img.GetThumbnailImage(32, 32, () => false, IntPtr.Zero); } string thumbFileName = Path.GetFileNameWithoutExtension(fileName) + "-thumb" + Path.GetExtension(fileName); thumb.Save(Path.Combine(fileRootPath, thumbFileName)); VehiclePhoto photo = new VehiclePhoto() { FileName = fileName, VehicleId = vehicleId }; await Repository.AddPhoto(photo); await UnitOfWork.CompleteAsync(); return(Ok(Mapper.Map <VehiclePhoto, VehiclePhotoResource>(photo))); }
public void WhenConstructed_ThenPopulated() { VehiclePhoto actual = new VehiclePhoto(); Assert.NotNull(actual); Assert.Equal(0, actual.VehiclePhotoId); Assert.Null(actual.Image); Assert.Null(actual.ImageMimeType); }
public void WehnVehiclePhotoIdSet_ThenValueUpdated() { VehiclePhoto target = new VehiclePhoto(); target.VehiclePhotoId = 4; int actual = target.VehiclePhotoId; Assert.Equal(4, actual); }
public void WhenImageMimeTypeSet_ThenValueUpdated() { VehiclePhoto target = new VehiclePhoto(); target.ImageMimeType = "ImageMimeType"; string actual = target.ImageMimeType; Assert.Equal("ImageMimeType", actual); }
public void WhenImageSetToNull_ThenValueUpdated() { VehiclePhoto target = new VehiclePhoto(); target.Image = new byte[] { 1, 2, 3 }; target.Image = null; byte[] actual = target.Image; Assert.Null(actual); }
public void WhenImageMimeTypeSetToNull_ThenUpdatesValue() { VehiclePhoto target = new VehiclePhoto(); target.ImageMimeType = "ImageMimeType"; target.ImageMimeType = null; string actual = target.ImageMimeType; Assert.Null(actual); }
public void WhenImageMimeTypeSetToValidValue_ThenValidationPasses() { VehiclePhoto target = new VehiclePhoto(); target.Image = new byte[1]; target.ImageMimeType = "ImageMimeType"; var validationContext = new ValidationContext(target, null, null); var validationResults = new List <ValidationResult>(); bool actual = Validator.TryValidateObject(target, validationContext, validationResults, true); Assert.True(actual); Assert.Equal(0, validationResults.Count); }
VehiclePhoto CreateVehiclePhoto(Image image, Int32 vehicleId) { byte[] buffer; using (var memoryStream = new MemoryStream()) { image.Save(memoryStream, new ImageFormat(image.RawFormat.Guid)); buffer = memoryStream.ToArray(); } var vehiclePhoto = new VehiclePhoto { ImageMimeType = "image/jpeg", Image = buffer, VehicleId = vehicleId }; VehiclePhotos.Add(vehiclePhoto); SaveChanges(); return(vehiclePhoto); }
public void WhenCreateCalled_ThenPhotoPersists() { var repository = new VehiclePhotoRepository(); var photo = new VehiclePhoto { ImageMimeType = "image/jpeg", Image = new byte[1] }; repository.Create(1, photo); var repository2 = new VehiclePhotoRepository(); Assert.NotNull(repository2.Get(1)); }
public void WhenImageSet_ThenValueUpdated() { VehiclePhoto target = new VehiclePhoto(); byte[] expected = new byte[] { 1, 2, 3 }; target.Image = expected; byte[] actual = target.Image; Assert.Equal(expected.Length, actual.Length); for (int i = 0; i < expected.Length; i++) { Assert.Equal(expected[i], actual[i]); } }
public void WhenImageMimeTypeSetTo101Characters_ThenValidationFails() { VehiclePhoto target = new VehiclePhoto(); target.Image = new byte[1]; target.ImageMimeType = new string('1', 101); var validationContext = new ValidationContext(target, null, null); var validationResults = new List <ValidationResult>(); bool actual = Validator.TryValidateObject(target, validationContext, validationResults, true); Assert.False(actual); Assert.Equal(1, validationResults.Count); Assert.Equal(1, validationResults[0].MemberNames.Count()); Assert.Equal("ImageMimeType", validationResults[0].MemberNames.First()); }
public void WhenDeleteCalled_ThenPhotoNuked() { const int vehicleId = 1; var repository = new VehiclePhotoRepository(); var photo = new VehiclePhoto { ImageMimeType = "image/jpeg", Image = new byte[1] }; repository.Create(vehicleId, photo); repository.Delete(photo.Id); var repository2 = new VehiclePhotoRepository(); Assert.Null(repository2.Get(photo.Id)); }
public void Create(int vehicleId, VehiclePhoto photo) { photo.VehicleId = vehicleId; this.GetDbSet <VehiclePhoto>().Add(photo); this.UnitOfWork.SaveChanges(); }
public async Task AddPhoto(VehiclePhoto photo) { await DbContext.Photos.AddAsync(photo); }