Search in sources :

Example 1 with TempFile

use of org.obiba.mica.file.TempFile in project mica2 by obiba.

the class TempFileService method addTempFile.

@NotNull
public TempFile addTempFile(@NotNull TempFile tempFile, @NotNull InputStream uploadedInputStream) throws IOException {
    TempFile savedTempFile;
    if (tempFile.getId() != null) {
        savedTempFile = tempFileRepository.findOne(tempFile.getId());
        if (savedTempFile == null) {
            savedTempFile = tempFileRepository.save(tempFile);
        }
    } else {
        savedTempFile = tempFileRepository.save(tempFile);
    }
    File file = getFile(savedTempFile.getId());
    OutputStream fileOut = new FileOutputStream(file);
    ByteStreams.copy(uploadedInputStream, fileOut);
    fileOut.close();
    savedTempFile.setSize(file.length());
    savedTempFile.setMd5(Files.hash(file, Hashing.md5()).toString());
    tempFileRepository.save(savedTempFile);
    return savedTempFile;
}
Also used : TempFile(org.obiba.mica.file.TempFile) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) TempFile(org.obiba.mica.file.TempFile) File(java.io.File) NotNull(javax.validation.constraints.NotNull)

Example 2 with TempFile

use of org.obiba.mica.file.TempFile in project mica2 by obiba.

the class StudyPackageImportServiceImpl method saveTempFile.

private void saveTempFile(Attachment attachment, ByteSource content) throws IOException {
    TempFile tempFile = new TempFile();
    tempFile.setId(attachment.getId());
    tempFile.setName(attachment.getName());
    tempFileService.addTempFile(tempFile, content.openStream());
    attachment.setMd5(content.hash(Hashing.md5()).toString());
    attachment.setSize(content.size());
}
Also used : TempFile(org.obiba.mica.file.TempFile)

Example 3 with TempFile

use of org.obiba.mica.file.TempFile in project mica2 by obiba.

the class TempFilesResource method upload.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Timed
public Response upload(@Context HttpServletRequest request, @Context UriInfo uriInfo) throws IOException, FileUploadException {
    FileItem fileItem = getUploadedFile(request);
    if (fileItem == null)
        throw new FileUploadException("Failed to extract file item from request");
    TempFile tempFile = tempFileService.addTempFile(fileItem.getName(), fileItem.getInputStream());
    URI location = uriInfo.getBaseUriBuilder().path(TempFilesResource.class).path(TempFilesResource.class, "file").build(tempFile.getId());
    return Response.created(location).build();
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) TempFile(org.obiba.mica.file.TempFile) URI(java.net.URI) FileUploadException(org.apache.commons.fileupload.FileUploadException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed)

Example 4 with TempFile

use of org.obiba.mica.file.TempFile in project mica2 by obiba.

the class TempFileService method addTempFile.

@NotNull
public TempFile addTempFile(@NotNull String fileName, @NotNull InputStream uploadedInputStream) throws IOException {
    TempFile tempFile = new TempFile();
    tempFile.setName(fileName);
    return addTempFile(tempFile, uploadedInputStream);
}
Also used : TempFile(org.obiba.mica.file.TempFile) NotNull(javax.validation.constraints.NotNull)

Example 5 with TempFile

use of org.obiba.mica.file.TempFile in project mica2 by obiba.

the class TempFileService method cleanupTempFiles.

@Scheduled(fixedDelay = TEMP_FILE_CLEANUP_INTERVAL)
public void cleanupTempFiles() {
    log.debug("Cleaning up tempfiles");
    List<TempFile> tempFiles = tempFileRepository.findByCreatedDateLessThan(DateTime.now().minusHours(TEMP_FILE_EXPIRE_TIMEOUT), new PageRequest(0, 100));
    tempFiles.forEach(f -> tempFileRepository.delete(f));
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) TempFile(org.obiba.mica.file.TempFile) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

TempFile (org.obiba.mica.file.TempFile)5 NotNull (javax.validation.constraints.NotNull)2 Timed (com.codahale.metrics.annotation.Timed)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 PageRequest (org.springframework.data.domain.PageRequest)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1