Search in sources :

Example 1 with ProjectExportRequest

use of de.tudarmstadt.ukp.clarin.webanno.export.ProjectExportRequest in project webanno by webanno.

the class RemoteApiController2 method projectExport.

@ApiOperation(value = "Export a project to a ZIP file")
@RequestMapping(value = ("/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + EXPORT), method = RequestMethod.GET, produces = { "application/zip", APPLICATION_JSON_UTF8_VALUE })
public ResponseEntity<InputStreamResource> projectExport(@PathVariable(PARAM_PROJECT_ID) long aProjectId) throws Exception {
    // Get project (this also ensures that it exists and that the current user can access it
    Project project = getProject(aProjectId);
    ProjectExportRequest per = new ProjectExportRequest(Model.of(project), "bin");
    File exportedFile = exportService.generateZipFile(per);
    // Turn the file into a resource and auto-delete the file when the resource closes the
    // stream.
    InputStreamResource result = new InputStreamResource(new FileInputStream(exportedFile) {

        @Override
        public void close() throws IOException {
            super.close();
            FileUtils.forceDelete(exportedFile);
        }
    });
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.valueOf("application/zip"));
    httpHeaders.setContentLength(exportedFile.length());
    httpHeaders.set("Content-Disposition", "attachment; filename=\"" + exportedFile.getName() + "\"");
    return new ResponseEntity<>(result, httpHeaders, HttpStatus.OK);
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ProjectExportRequest(de.tudarmstadt.ukp.clarin.webanno.export.ProjectExportRequest) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileInputStream(java.io.FileInputStream) InputStreamResource(org.springframework.core.io.InputStreamResource) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ProjectExportRequest (de.tudarmstadt.ukp.clarin.webanno.export.ProjectExportRequest)1 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)1 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject)1 ApiOperation (io.swagger.annotations.ApiOperation)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamResource (org.springframework.core.io.InputStreamResource)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1