use of com.odysseusinc.arachne.portal.model.StudyFile in project ArachneCentralAPI by OHDSI.
the class BaseStudyController method downloadFile.
@ApiOperation("Download file of the study.")
@RequestMapping(value = "/api/v1/study-management/studies/{studyId}/files/{fileUuid}/download", method = GET)
public void downloadFile(@PathVariable("studyId") Long studyId, @PathVariable("fileUuid") String uuid, HttpServletResponse response) throws PermissionDeniedException, NotExistException, IOException {
StudyFile studyFile = studyService.getStudyFile(studyId, uuid);
final InputStream is = fileService.getFileInputStream(studyFile);
response.setContentType(studyFile.getContentType());
response.setHeader("Content-type", studyFile.getContentType());
response.setHeader("Content-Disposition", "attachment; filename=" + studyFile.getRealName());
org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
}
use of com.odysseusinc.arachne.portal.model.StudyFile in project ArachneCentralAPI by OHDSI.
the class BaseStudyServiceImpl method saveFile.
@Override
@PreAuthorize("hasPermission(#studyId, 'Study', " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).UPLOAD_FILES)")
public String saveFile(MultipartFile multipartFile, Long studyId, String label, IUser user) throws IOException {
Study study = studyRepository.findOne(studyId);
String fileNameLowerCase = UUID.randomUUID().toString();
try {
StudyFile studyFile = new StudyFile();
studyFile.setUuid(fileNameLowerCase);
studyFile.setStudy(study);
studyFile.setLabel(label);
studyFile.setRealName(multipartFile.getOriginalFilename());
Date created = new Date();
studyFile.setCreated(created);
studyFile.setUpdated(created);
studyFile.setAuthor(user);
// Save study after uuid and Study were set
fileService.saveFile(multipartFile, studyFile);
// Detect file content type (requires file to exist)
String contentType = CommonFileUtils.getContentType(multipartFile.getOriginalFilename(), fileService.getStudyFilePath(studyFile).toAbsolutePath().toString());
studyFile.setContentType(contentType);
// Save entity
studyFileRepository.save(studyFile);
eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(studyFile.getId(), studyFile.getRealName(), fileService.getFileInputStream(studyFile), AntivirusJobFileType.STUDY_FILE)));
return fileNameLowerCase;
} catch (IOException | RuntimeException ex) {
String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
LOGGER.debug(message, ex);
throw new IOException(message);
}
}
use of com.odysseusinc.arachne.portal.model.StudyFile in project ArachneCentralAPI by OHDSI.
the class BaseStudyServiceImpl method saveFile.
@Override
@PreAuthorize("hasPermission(#studyId, 'Study', " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).UPLOAD_FILES)")
public String saveFile(String link, Long studyId, String label, IUser user) throws IOException {
Study study = studyRepository.findOne(studyId);
String fileNameLowerCase = UUID.randomUUID().toString();
try {
if (link == null) {
throw new IORuntimeException("wrong url");
}
HttpHeaders headers = new HttpHeaders();
headers.setAccept(singletonList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity<String> entity = new HttpEntity<>(headers);
URL url = new URL(link);
String fileName = FilenameUtils.getName(url.getPath());
ResponseEntity<byte[]> response = restTemplate.exchange(link.toString(), HttpMethod.GET, entity, byte[].class);
if (response.getStatusCode() == HttpStatus.OK) {
String contentType = CommonFileUtils.TYPE_LINK;
StudyFile studyFile = new StudyFile();
studyFile.setUuid(fileNameLowerCase);
studyFile.setContentType(contentType);
studyFile.setStudy(study);
studyFile.setLabel(label);
studyFile.setRealName(fileName);
studyFile.setLink(link);
Date created = new Date();
studyFile.setCreated(created);
studyFile.setUpdated(created);
studyFile.setAuthor(user);
studyFile.setAntivirusStatus(AntivirusStatus.WILL_NOT_SCAN);
studyFile.setAntivirusDescription("External links are not scanned");
studyFileRepository.save(studyFile);
return fileNameLowerCase;
}
return null;
} catch (IOException | RuntimeException ex) {
String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
LOGGER.debug(message, ex);
throw new IOException(message);
}
}
use of com.odysseusinc.arachne.portal.model.StudyFile in project ArachneCentralAPI by OHDSI.
the class BaseStudyServiceImpl method getDeleteStudyFile.
@Override
@PreAuthorize("hasPermission(#studyId, 'Study', " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).ACCESS_STUDY)")
public Boolean getDeleteStudyFile(Long studyId, String uuid) throws FileNotFoundException {
StudyFile studyFile = studyFileRepository.findByUuid(uuid);
if (!Objects.equals(studyFile.getContentType(), "link") && studyFile.getLink() == null) {
fileService.delete(studyFile);
}
studyFileRepository.delete(studyFile);
return true;
}
use of com.odysseusinc.arachne.portal.model.StudyFile in project ArachneCentralAPI by OHDSI.
the class BaseStudyServiceImpl method processAntivirusResponse.
@EventListener
@Transactional
@Override
public void processAntivirusResponse(AntivirusJobStudyFileResponseEvent event) {
final AntivirusJobResponse antivirusJobResponse = event.getAntivirusJobResponse();
final StudyFile studyFile = studyFileRepository.findOne(antivirusJobResponse.getFileId());
if (studyFile != null) {
studyFile.setAntivirusStatus(antivirusJobResponse.getStatus());
studyFile.setAntivirusDescription(antivirusJobResponse.getDescription());
studyFileRepository.save(studyFile);
}
}
Aggregations