Commit ca7a16b7 authored by Artem Hrybeniuk's avatar Artem Hrybeniuk
Browse files

Merge branch '288-filerepository-zip-endpoints' into 'main'

Resolve "FileRepository: ZIP endpoints"

Closes #288

See merge request grin-global/grin-global-server!379
parents 67ec15f0 086541b5
......@@ -163,6 +163,43 @@ public class RepositoryController extends ApiBaseController {
response.flushBuffer();
}
/**
* Download folder as zip.
*
* @param folderUuid the folder uuid
* @throws IOException error in reading from repository or writing to zip
* @throws InvalidRepositoryPathException the invalid repository path of folder
*/
@GetMapping(value = "/folder/download/{folderUuid:\\w{8}\\-\\w{4}.+}")
public void downloadFolderAsZip(@PathVariable("folderUuid") final UUID folderUuid, final HttpServletResponse response) throws IOException, InvalidRepositoryPathException {
final RepositoryFolder repositoryFolder = repositoryService.getFolder(folderUuid);
response.setContentType("application/zip");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s.zip\"", repositoryFolder.getName()));
repositoryService.getFolderAsZip(repositoryFolder, response.getOutputStream());
response.flushBuffer();
}
/**
* Extract zip in repository folder.
*
* @param fileUuid the repository zip file uuid
* @throws IOException error in writing to repository
* @throws NoSuchRepositoryFileException the repository file doesn't exist
* @throws InvalidRepositoryPathException error in creating sub repository folders
* @throws InvalidRepositoryFileDataException if the target repositoryFile doesn't have a zip extension.
* @return the list of created repository files from zip
*/
@GetMapping(value = "/file/extract/{fileUuid:\\w{8}\\-\\w{4}.+}")
public List<RepositoryFile> extractZip(@PathVariable("fileUuid") final UUID fileUuid)
throws NoSuchRepositoryFileException, IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException {
final RepositoryFile repositoryFile = repositoryService.getFile(fileUuid);
return repositoryService.extractZip(repositoryFile);
}
/**
* Move specified file to the specified full file path (folder + new
* originalFilename).
......
......@@ -113,7 +113,7 @@ springdoc.override-with-generic-response=false
file.repository.dir=${data.dir}/repository
file.repository.image.thumbnailSizes=100,300
repository.blacklist.filename=^(\\.|\\~)|^Thumbs.db$
repository.blacklist.foldername=(^\\.)|[~!\\/]
repository.blacklist.foldername=(^\\.)|[~!\\/]|^__MACOSX$
# Mail properties
mail.host=
......
......@@ -40,6 +40,7 @@ import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.transaction.annotation.Transactional;
import java.nio.file.Paths;
......@@ -53,6 +54,7 @@ public class RepositoryControllerTest extends AbstractApiV1Test {
private static final MockMultipartFile MOCK_FILE = new MockMultipartFile("file", "file.txt", "text/plain", new byte[1]);
private static final String FOLDER_PATH_1 = "/aaa/bbb/ccc";
private static final String FOLDER_PATH_2 = "/aaa/bbb/ccc/ddd";
@Autowired
private RepositoryFolderRepository folderRepository;
......@@ -588,4 +590,55 @@ public class RepositoryControllerTest extends AbstractApiV1Test {
;
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void getFolderAsZipTest() throws Exception {
RepositoryFile saved = saveFile(FOLDER_PATH_1, MOCK_FILE, null);
assertEquals(1, repositoryFilePersistence.count());
/*@formatter:off*/
mockMvc
.perform(get(RepositoryController.CONTROLLER_URL.concat("/folder/download/{folderUuid}"), saved.getFolder().getUuid()))
.andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType("application/zip"))
.andExpect(header().string("Content-Disposition", "attachment; filename=\"ccc.zip\""))
;
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void extractZipFileTest() throws Exception {
RepositoryFile saved = saveFile(FOLDER_PATH_1, MOCK_FILE, null);
assertEquals(1, repositoryFilePersistence.count());
/*@formatter:off*/
MvcResult result = mockMvc
.perform(get(RepositoryController.CONTROLLER_URL.concat("/folder/download/{folderUuid}"), saved.getFolder().getUuid()))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType("application/zip"))
.andExpect(header().string("Content-Disposition", "attachment; filename=\"ccc.zip\""))
.andReturn()
;
/*@formatter:on*/
var zipFile = repositoryService.addFile(Paths.get(FOLDER_PATH_2), "ccc.zip", "application/zip", result.getResponse().getContentAsByteArray(), null);
/*@formatter:off*/
mockMvc
.perform(get(RepositoryController.CONTROLLER_URL.concat("/file/extract/{fileUuid}"), zipFile.getUuid()))
.andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].originalFilename", is("file.txt")))
.andExpect(jsonPath("$[0].folder", is("/aaa/bbb/ccc/ddd")))
.andExpect(jsonPath("$[0].contentType", is("text/plain")))
;
/*@formatter:on*/
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment