public async Task AddAsync(ImageUploadBindingModel uploadBindingModel) { var file = await this.ToByteArrayAsync(uploadBindingModel.File); //var fileExtension = Path.GetExtension(uploadBindingModel.File.FileName); var fileExtension = this.GetExtension(uploadBindingModel.File.FileName); var fileName = this.GenerateName(uploadBindingModel.File.FileName, fileExtension); if (FileIsEmpty(file)) { throw new FileNotFoundException(); } if (!SUPPORTED_FORMATS.Contains(fileExtension)) { throw new FormatException("Please select a .jpeg, .png or .bmp file"); } await this.blobStorageService.UploadFromFileAsync(file, fileName, fileExtension); var image = new Image() { Name = fileName, Description = uploadBindingModel.Description, UserId = this.currentUserId, IsPublic = uploadBindingModel.IsPublic }; await tagService.AddToImageAsync(image); await this.images.AddAsync(image); await this.images.SaveChangesAsync(); }
public override async Task <object> ReadFromStreamAsync( Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { var provider = await content.ReadAsMultipartAsync(); var imageFileContents = provider.Contents .First(c => c.Headers.ContentDisposition.Name.NormalizeName().Matches(@"Image")); var deviceDetialsFileContents = provider.Contents .First(c => c.Headers.ContentDisposition.Name.NormalizeName().Matches(@"DeviceDetails")); var uploadIdFileContents = provider.Contents .First(c => c.Headers.ContentDisposition.Name.NormalizeName().Matches(@"UploadId")); var addVisitorViewModel = new ImageUploadBindingModel { Image = await imageFileContents.ReadAsByteArrayAsync(), DeviceDetails = await deviceDetialsFileContents.ReadAsStringAsync(), UploadId = await uploadIdFileContents.ReadAsStringAsync() }; return(addVisitorViewModel); }
public async Task <IActionResult> Upload(string id, ImageUploadBindingModel model) { if (!this.ModelState.IsValid) { this.ViewData["ErrorMessage"] = InvalidFormatImageMessage; return(View(model ?? new ImageUploadBindingModel())); } if (model.Images.Count > 0) { if (this.imageServices.ImagesCount(id) < GlobalConstants.ImageUploadLimit) { int sequence = 1; foreach (var image in model.Images) { var imageId = Guid.NewGuid().ToString(); //var imageUrl = await this.cloudinaryService.UploadPictureAsync(image, imageId); //The code above is commented since Cloudinary is no longer used for saving the images. //Images are saved in the file system. The below method returns the image name, the whole url is no longer needed since the views use the relative path "~/images/imagename". Now in the Db in column Url we save the image name instead of the url. The url would be needed only when using Cloudinary. var imageName = await this.imageServices.ProcessPhotoAsync(image); var isImageAddedInDb = await this.imageServices.AddImageAsync(imageId, imageName, id, sequence); sequence++; } } } RedirectToActionResult redirectResult = new RedirectToActionResult("Create", "Offer", new { @Id = $"{id}" }); return(redirectResult); }
public void AddAsync_WithEmptyFile_Should_ThrowError() { //act var memoryStream = new MemoryStream(); var IFormFileMock = new Mock <IFormFile>(); IFormFileMock .Setup(f => f.OpenReadStream()) .Returns(memoryStream) .Verifiable(); IFormFileMock .Setup(f => f.FileName) .Returns("test.jpg"); var uploadBindingModel = new ImageUploadBindingModel() { File = IFormFileMock.Object }; //assert Assert.ThrowsAsync <FileNotFoundException>(async() => await this.imageService.AddAsync(uploadBindingModel)); }
public async Task <IActionResult> Upload(string id, ImageUploadBindingModel model) { if (!this.ModelState.IsValid) { this.ViewData["ErrorMessage"] = InvalidFormatImageMessage; return(View(model ?? new ImageUploadBindingModel())); } if (model.Images.Count > 0) { if (this.imageServices.ImagesCount(id) < GlobalConstants.ImageUploadLimit) { int sequence = 1; foreach (var image in model.Images) { var imageId = Guid.NewGuid().ToString(); var imageUrl = await this.cloudinaryService.UploadPictureAsync(image, imageId); var isImageAddedInDb = await this.imageServices.AddImageAsync(imageId, imageUrl, id, sequence); sequence++; } } } RedirectToActionResult redirectResult = new RedirectToActionResult("Create", "Offer", new { @Id = $"{id}" }); return(redirectResult); }
public void AddAsync_WithValidImage_Should_Pass() { var IFormFileMock = new Mock <IFormFile>(); IFormFileMock .Setup(f => f.OpenReadStream()) .Returns(this.GenerateStreamFromString("asd")) .Verifiable(); IFormFileMock .Setup(f => f.FileName) .Returns("test.jpg"); var uploadBindingModel = new ImageUploadBindingModel() { File = IFormFileMock.Object }; Assert.That(async() => await this.imageService.AddAsync(uploadBindingModel), Throws.Nothing); }
public void AddAsync_WithInvalidFileExtension_Should_ThrowError() { var IFormFileMock = new Mock <IFormFile>(); IFormFileMock .Setup(f => f.OpenReadStream()) .Returns(this.GenerateStreamFromString("asd")) .Verifiable(); IFormFileMock .Setup(f => f.FileName) .Returns("test.txt"); var uploadBindingModel = new ImageUploadBindingModel() { File = IFormFileMock.Object }; Assert.ThrowsAsync <FormatException>(async() => await this.imageService.AddAsync(uploadBindingModel)); }
public async Task <IActionResult> Upload(ImageUploadBindingModel uploadBindingModel) { await this.imageService.AddAsync(uploadBindingModel); return(RedirectToAction("Details", "Profile")); }