public async Task CreateAsync(UploadClientDocumentViewModel input, string userId, string imagePath) { Directory.CreateDirectory($"{imagePath}/clientDocuments/"); foreach (var image in input.Images) { var extension = Path.GetExtension(image.FileName).TrimStart('.'); if (!this.allowedExtensions.Any(x => extension.EndsWith(x))) { throw new Exception($"Invalid image extension {extension}"); } var dbImage = new ClientDocument { Size = (image.Length / 1000).ToString(), // size is in kb AddedByUserId = userId, CaseId = input.CaseId, Name = input.Name, Extension = extension, }; await this.clientDocumentsRepository.AddAsync(dbImage); await this.clientDocumentsRepository.SaveChangesAsync(); var physicalPath = $"{imagePath}/clientDocuments/{dbImage.Id}.{extension}"; using Stream fileStream = new FileStream(physicalPath, FileMode.Create); await image.CopyToAsync(fileStream); } }
public async Task <IActionResult> Upload(UploadClientDocumentViewModel input) { if (!this.ModelState.IsValid) { return(this.View()); } var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; try { await this.clientDocumentsService.CreateAsync(input, userId, $"{this.environment.WebRootPath}/images"); } catch (Exception ex) { this.ModelState.AddModelError(string.Empty, ex.Message); return(this.View()); } return(this.RedirectToAction("All", new { caseId = input.CaseId })); }
public async Task CreateAsyncDocumentTests() { AutoMapperConfig.RegisterMappings(typeof(CreateCaseInputViewModel).GetTypeInfo().Assembly); var clientDocumentsList = new List <ClientDocument>(); var clientDocumentsMockRepo = new Mock <IDeletableEntityRepository <ClientDocument> >(); clientDocumentsMockRepo.Setup(x => x.AddAsync(It.IsAny <ClientDocument>())).Callback( (ClientDocument clientDocument) => { clientDocument.Id = Guid.NewGuid().ToString(); clientDocumentsList.Add(clientDocument); }); var file = new Mock <IFormFile>(); string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // get current number of pictures in the directory so we can compare at the end Directory.CreateDirectory($"{desktopPath}/clientDocuments/"); int filesCountBeginning = Directory.GetFiles( $"{desktopPath}/clientDocuments", "*", SearchOption.TopDirectoryOnly).Length; var sourceImg = File.OpenRead(desktopPath + "/SFA_scheme.png"); var ms = new MemoryStream(); var writer = new StreamWriter(ms); writer.Write(sourceImg); writer.Flush(); ms.Position = 0; var fileName = "Test.png"; file.Setup(f => f.FileName).Returns(fileName).Verifiable(); file.Setup(_ => _.CopyToAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())) .Returns((Stream stream, CancellationToken token) => ms.CopyToAsync(stream)) .Verifiable(); UploadClientDocumentViewModel input = new UploadClientDocumentViewModel() { CaseId = "1", Name = "test", Images = new List <IFormFile>() { file.Object }, }; var clientDocumentService = new ClientDocumentsService(clientDocumentsMockRepo.Object); var userId = "NewUserId"; // test Happy path await clientDocumentService.CreateAsync(input, userId, desktopPath); Assert.Single(clientDocumentsList); file.Verify(); int filesCountEnd = Directory.GetFiles( $"{desktopPath}/clientDocuments", "*", SearchOption.TopDirectoryOnly).Length; Assert.Equal(filesCountBeginning + 1, filesCountEnd); // test extensions filter var file2 = new Mock <IFormFile>(); var fileName2 = "Test.pdf"; file2.Setup(f => f.FileName).Returns(fileName2).Verifiable(); UploadClientDocumentViewModel input2 = new UploadClientDocumentViewModel() { CaseId = "1", Name = "test", Images = new List <IFormFile>() { file2.Object }, }; await Assert.ThrowsAsync <Exception>(async() => await clientDocumentService.CreateAsync(input2, userId, desktopPath)); }