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;
}
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());
}
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();
}
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);
}
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));
}
Aggregations