public void Transform_Returns_The_Instance_Of_FileListDto_Returned_By_FileListTransformer() { var fileListTransformer = Mock.Of <ITransformer <FileList, FileListDto> >(); var fileListDto = new FileListDto(); Mock.Get(fileListTransformer) .Setup(q => q.Transform(It.IsAny <FileList>())) .Returns(fileListDto); var result = CreateSut(fileListTransformer).Transform(new FileList()); result.ShouldBeSameAs(fileListDto); }
public async Task <ActionResult <object> > Rearrange([FromBody] FileListDto fileList) { try { var locations = from pdfFile in fileList.PdfFiles select pdfFile.Location; await _pdfFileRepository.RearrangePdfFileList(locations); return(StatusCode(StatusCodes.Status200OK, "")); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }
public async Task Rearrange_Uses_RepositoryRearrange_With_Locations() { var fileList = new FileListDto(); var pdfFiles = new List <PdfFileDto>(); pdfFiles.Add(new PdfFileDto { Location = Guid.NewGuid() }); fileList.PdfFiles = pdfFiles; var repositoryMock = new Mock <IPdfFileRepository>(); var response = await CreateSut(pdfFileRepository : repositoryMock.Object).Rearrange(fileList); (response.Result as ObjectResult).StatusCode.ShouldBe(StatusCodes.Status200OK); repositoryMock.Verify(m => m.RearrangePdfFileList(It.IsAny <IEnumerable <Guid> >()), Times.Once); }
public string GetAll() { FileListDto response = new FileListDto(); try { response.Files = dokumentumokService.GetAllFileNameFromFolder(dokumentumokService.GetFolderPath()); } catch (DirectoryNotFoundException aEx) { response.ErrorMessage = $"{nameof(DirectoryNotFoundException)}, message: {aEx.Message}"; } catch (Exception ex) { response.ErrorMessage = $"Unknow exception, message: {ex.Message}"; } return(JsonConvert.SerializeObject(response)); }
public async Task GetFileList_Returns_Ok_With_FileList() { var fileList = new FileList(); var fileListDto = new FileListDto(); var repositoryMock = new Mock <IPdfFileRepository>(); var transformerMock = new Mock <IFileListTransformerService>(); repositoryMock.Setup(m => m.GetPdfFileList()).ReturnsAsync(fileList); transformerMock.Setup(m => m.Transform(fileList)).Returns(fileListDto); var response = await CreateSut(pdfFileRepository : repositoryMock.Object, fileListTransformerService : transformerMock.Object).GetFileList(); repositoryMock.Verify(m => m.GetPdfFileList(), Times.Once); transformerMock.Verify(m => m.Transform(fileList), Times.Once); response.Result.ShouldBeOfType(typeof(OkObjectResult)); var okObjectResult = response.Result as OkObjectResult; okObjectResult.Value.ShouldBeSameAs(fileListDto); }
public async Task <IActionResult> UploadPhysical() { if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) { ModelState.AddModelError("File", "The request couldn't be processed (Error 1)."); return(BadRequest(ModelState)); } var boundary = MultipartRequestHelper.GetBoundary( MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(boundary, HttpContext.Request.Body); var section = await reader.ReadNextSectionAsync(); var filesDto = new FileListDto { TargetFilePath = _targetFilePath }; while (section != null) { var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse( section.ContentDisposition, out var contentDisposition); if (hasContentDispositionHeader) { // This check assumes that there's a file // present without form data. If form data // is present, this method immediately fails // and returns the model error. if (!MultipartRequestHelper.HasFileContentDisposition(contentDisposition)) { ModelState.AddModelError("File", "The request couldn't be processed (Error 2)."); // Log error return(BadRequest(ModelState)); } else { // Don't trust the file name sent by the client. To display // the file name, HTML-encode the value. var trustedFileNameForDisplay = WebUtility.HtmlEncode( contentDisposition.FileName.Value); var trustedFileNameForFileStorage = Path.GetRandomFileName() + Path.GetExtension(trustedFileNameForDisplay).ToLower(); var streamedFileContent = await FileHelpers.ProcessStreamedFile(section, ModelState, _fileSizeLimit); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (var targetStream = System.IO.File.Create( Path.Combine(_targetFilePath, trustedFileNameForFileStorage))) { await targetStream.WriteAsync(streamedFileContent); var file = new FileDto { FileSize = streamedFileContent.Length, FileName = trustedFileNameForDisplay, FileStorageName = trustedFileNameForFileStorage, LocalUrl = Path.Combine(_targetFilePath, trustedFileNameForFileStorage) }; file.ThumbnailImage = ConverThumbnail(file.LocalUrl, targetStream); filesDto.Files.Add(file); filesDto.Size += streamedFileContent.Length; } } } // Drain any remaining section body that hasn't been consumed and // read the headers for the next section. section = await reader.ReadNextSectionAsync(); } filesDto.Count = filesDto.Files.Count; return(Ok(filesDto)); }