use of com.odysseusinc.arachne.portal.model.AnalysisFile 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.model.AnalysisFile in project ArachneCentralAPI by OHDSI.
the class BaseAnalysisServiceImpl method fullDelete.
@Override
public void fullDelete(List<A> analyses) {
for (A analysis : analyses) {
List<AnalysisFile> files = analysis.getFiles();
analysisFileRepository.delete(files);
for (AnalysisFile file : files) {
deleteAnalysisFile(analysis, file);
}
}
analysisRepository.delete(analyses);
}
use of com.odysseusinc.arachne.portal.model.AnalysisFile in project ArachneCentralAPI by OHDSI.
the class BaseAnalysisServiceImpl method writeToFile.
@Override
@PreAuthorize("hasPermission(#analysisFile, " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).DELETE_ANALYSIS_FILES)")
public void writeToFile(AnalysisFile analysisFile, FileDTO fileContentDTO, IUser updatedBy) throws IOException {
Analysis analysis = analysisFile.getAnalysis();
throwAccessDeniedExceptionIfLocked(analysis);
Study study = analysis.getStudy();
try {
Path analysisFolder = analysisHelper.getAnalysisFolder(analysis);
if (Files.notExists(analysisFolder)) {
Files.createDirectories(analysisFolder);
}
Path targetPath = analysisFolder.resolve(analysisFile.getUuid());
byte[] content = fileContentDTO.getContent().getBytes(StandardCharsets.UTF_8);
try (final InputStream stream = new ByteArrayInputStream(content)) {
Files.copy(stream, targetPath, REPLACE_EXISTING);
}
analysisFile.setUpdated(new Date());
analysisFile.setEntryPoint(analysisFile.getEntryPoint());
analysisFile.setUpdatedBy(updatedBy);
analysisFile.setContentType(CommonFileUtils.getContentType(analysisFile.getRealName(), targetPath.toString()));
analysisFile.incrementVersion();
analysisFile.setAntivirusStatus(AntivirusStatus.SCANNING);
analysisFile.setAntivirusDescription(null);
final AnalysisFile saved = analysisFileRepository.save(analysisFile);
eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(saved.getId(), saved.getRealName(), new FileInputStream(targetPath.toString()), AntivirusJobFileType.ANALYSIS_FILE)));
} catch (IOException | RuntimeException ex) {
String message = "error save file to disk, filename=" + analysisFile.getUuid() + " ex=" + ex.toString();
LOGGER.error(message, ex);
throw new IOException(message);
}
}
use of com.odysseusinc.arachne.portal.model.AnalysisFile in project ArachneCentralAPI by OHDSI.
the class BaseAnalysisServiceImpl method setIsExecutable.
/**
* Updates isExecutable flag in a file with the given uuid and sets entry point
*
* @param uuid analysis file uuid
*/
@Override
public void setIsExecutable(String uuid) {
AnalysisFile analysisFile = analysisFileRepository.findByUuid(uuid);
Analysis analysis = analysisFile.getAnalysis();
throwAccessDeniedExceptionIfLocked(analysis);
setExecutableFileInAnalysis(analysis, uuid);
}
use of com.odysseusinc.arachne.portal.model.AnalysisFile in project ArachneCentralAPI by OHDSI.
the class BaseAnalysisController method deleteFile.
@ApiOperation("Delete Analysis file")
@RequestMapping(value = "/api/v1/analysis-management/analyses/{analysisId}/code-files/{fileUuid}", method = DELETE)
public JsonResult<Boolean> deleteFile(@PathVariable("analysisId") Long analysisId, @PathVariable("fileUuid") String uuid) throws PermissionDeniedException, NotExistException {
final JsonResult<Boolean> result;
AnalysisFile analysisFile = analysisService.getAnalysisFile(analysisId, uuid);
T analysis = (T) analysisFile.getAnalysis();
Boolean deleteAnalysisFile = analysisService.deleteAnalysisFile(analysis, analysisFile);
result = new JsonResult<>(NO_ERROR);
result.setResult(deleteAnalysisFile);
return result;
}
Aggregations