use of com.odysseusinc.arachne.portal.exception.IORuntimeException 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.exception.IORuntimeException in project ArachneCentralAPI by OHDSI.
the class BaseAnalysisServiceImpl method saveFile.
@Override
public AnalysisFile saveFile(String link, IUser user, A analysis, String label, Boolean isExecutable) throws IOException {
throwAccessDeniedExceptionIfLocked(analysis);
Study study = analysis.getStudy();
String fileNameLowerCase = UUID.randomUUID().toString();
try {
if (link == null) {
throw new IORuntimeException("wrong url");
}
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.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, HttpMethod.GET, entity, byte[].class);
if (response.getStatusCode() == HttpStatus.OK) {
final String contentType = response.getHeaders().getContentType().toString();
Path pathToAnalysis = getAnalysisPath(analysis);
Files.copy(new ByteArrayInputStream(response.getBody()), pathToAnalysis, REPLACE_EXISTING);
AnalysisFile analysisFile = new AnalysisFile();
analysisFile.setUuid(fileNameLowerCase);
analysisFile.setAnalysis(analysis);
analysisFile.setContentType(contentType);
analysisFile.setLabel(label);
analysisFile.setAuthor(user);
analysisFile.setExecutable(Boolean.TRUE.equals(isExecutable));
analysisFile.setRealName(fileName);
analysisFile.setEntryPoint(fileName);
Date created = new Date();
analysisFile.setCreated(created);
analysisFile.setUpdated(created);
analysisFile.setVersion(1);
return analysisFileRepository.save(analysisFile);
}
} catch (IOException | RuntimeException ex) {
String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
LOGGER.error(message, ex);
throw new IOException(message);
}
return null;
}
use of com.odysseusinc.arachne.portal.exception.IORuntimeException in project ArachneCentralAPI by OHDSI.
the class AnalysisHelper method getAnalysisFolder.
public Path getAnalysisFolder(Analysis analysis) {
String studyFilesPath = getStoreFilesPath();
Path filesStoreDirPath = Paths.get(studyFilesPath);
File fileStoreDir = filesStoreDirPath.toFile();
if (!fileStoreDir.exists()) {
if (!fileStoreDir.mkdirs()) {
throw new IORuntimeException("cann't create directory:" + filesStoreDirPath);
}
}
Path studyStoreDirPath = Paths.get(studyFilesPath, analysis.getStudy().getId().toString());
File studyStoreDir = studyStoreDirPath.toFile();
if (!studyStoreDir.exists()) {
if (!studyStoreDir.mkdirs()) {
throw new IORuntimeException("cann't create directory:" + studyStoreDirPath);
}
}
Path storeDirPath = studyStoreDirPath.resolve(analysis.getId().toString());
File storeDir = storeDirPath.toFile();
if (!storeDir.exists()) {
if (!storeDir.mkdirs()) {
throw new IORuntimeException("cann't create directory:" + storeDirPath);
}
}
return storeDirPath;
}
Aggregations