@Test void testDownload() { String fileId = "MyFileId"; String contentType = "application/octet-stream"; String filename = "filename"; FileMeta fileMeta = mock(FileMeta.class); when(fileMeta.getContentType()).thenReturn(contentType); when(fileMeta.getFilename()).thenReturn(filename); when(dataService.findOneById("sys_FileMeta", fileId, FileMeta.class)).thenReturn(fileMeta); ResponseEntity<StreamingResponseBody> responseEntity = filesApiServiceImpl.download(fileId); assertEquals(OK, responseEntity.getStatusCode()); assertEquals(valueOf(contentType), responseEntity.getHeaders().getContentType()); assertEquals( parse("attachment; filename=\"filename\""), responseEntity.getHeaders().getContentDisposition()); }
@Transactional(readOnly = true) @Override public ResponseEntity<StreamingResponseBody> download(String fileId) { FileMeta fileMeta = getFileMeta(fileId); ResponseEntity.BodyBuilder builder = ResponseEntity.ok(); builder.header(CONTENT_TYPE, fileMeta.getContentType()); builder.header(CONTENT_DISPOSITION, "attachment; filename=\"" + fileMeta.getFilename() + "\""); Long contentLength = fileMeta.getSize(); if (contentLength != null) { builder.contentLength(contentLength); } return builder.body( outputStream -> { try (ReadableByteChannel fromChannel = blobStore.newChannel(fileId)) { ByteStreams.copy(fromChannel, Channels.newChannel(outputStream)); } }); }
FilesServiceImpl implements FilesService { @Transactional(readOnly = true) @Override public ResponseEntity<StreamingResponseBody> download(String fileId) { FileMeta fileMeta = getFileMeta(fileId); ResponseEntity.BodyBuilder builder = ResponseEntity.ok(); builder.header(CONTENT_TYPE, fileMeta.getContentType()); builder.header(CONTENT_DISPOSITION, "attachment; filename=\"" + fileMeta.getFilename() + "\""); Long contentLength = fileMeta.getSize(); if (contentLength != null) { builder.contentLength(contentLength); } return builder.body( outputStream -> { try (ReadableByteChannel fromChannel = blobStore.newChannel(fileId)) { ByteStreams.copy(fromChannel, Channels.newChannel(outputStream)); } }); } }
FilesServiceImpl implements FilesService { @Transactional(readOnly = true) @Override public ResponseEntity<StreamingResponseBody> download(String fileId) { FileMeta fileMeta = getFileMeta(fileId); ResponseEntity.BodyBuilder builder = ResponseEntity.ok(); builder.header(CONTENT_TYPE, fileMeta.getContentType()); builder.header(CONTENT_DISPOSITION, "attachment; filename=\"" + fileMeta.getFilename() + "\""); Long contentLength = fileMeta.getSize(); if (contentLength != null) { builder.contentLength(contentLength); } return builder.body( outputStream -> { try (ReadableByteChannel fromChannel = blobStore.newChannel(fileId)) { ByteStreams.copy(fromChannel, Channels.newChannel(outputStream)); } }); } FilesServiceImpl(DataService dataService, BlobStore blobStore, FileMetaFactory fileMetaFactory); }
FilesServiceImpl implements FilesService { @Transactional(readOnly = true) @Override public ResponseEntity<StreamingResponseBody> download(String fileId) { FileMeta fileMeta = getFileMeta(fileId); ResponseEntity.BodyBuilder builder = ResponseEntity.ok(); builder.header(CONTENT_TYPE, fileMeta.getContentType()); builder.header(CONTENT_DISPOSITION, "attachment; filename=\"" + fileMeta.getFilename() + "\""); Long contentLength = fileMeta.getSize(); if (contentLength != null) { builder.contentLength(contentLength); } return builder.body( outputStream -> { try (ReadableByteChannel fromChannel = blobStore.newChannel(fileId)) { ByteStreams.copy(fromChannel, Channels.newChannel(outputStream)); } }); } FilesServiceImpl(DataService dataService, BlobStore blobStore, FileMetaFactory fileMetaFactory); @Transactional(readOnly = true) @Override FileMeta getFileMeta(String fileId); @Override void delete(String fileId); @Transactional @Override CompletableFuture<FileMeta> upload(HttpServletRequest httpServletRequest); @Transactional(readOnly = true) @Override ResponseEntity<StreamingResponseBody> download(String fileId); }
FilesServiceImpl implements FilesService { @Transactional(readOnly = true) @Override public ResponseEntity<StreamingResponseBody> download(String fileId) { FileMeta fileMeta = getFileMeta(fileId); ResponseEntity.BodyBuilder builder = ResponseEntity.ok(); builder.header(CONTENT_TYPE, fileMeta.getContentType()); builder.header(CONTENT_DISPOSITION, "attachment; filename=\"" + fileMeta.getFilename() + "\""); Long contentLength = fileMeta.getSize(); if (contentLength != null) { builder.contentLength(contentLength); } return builder.body( outputStream -> { try (ReadableByteChannel fromChannel = blobStore.newChannel(fileId)) { ByteStreams.copy(fromChannel, Channels.newChannel(outputStream)); } }); } FilesServiceImpl(DataService dataService, BlobStore blobStore, FileMetaFactory fileMetaFactory); @Transactional(readOnly = true) @Override FileMeta getFileMeta(String fileId); @Override void delete(String fileId); @Transactional @Override CompletableFuture<FileMeta> upload(HttpServletRequest httpServletRequest); @Transactional(readOnly = true) @Override ResponseEntity<StreamingResponseBody> download(String fileId);  }