Search in sources :

Example 1 with AntivirusJob

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob in project ArachneCentralAPI by OHDSI.

the class BaseAnalysisServiceImpl method saveFile.

@Override
public AnalysisFile saveFile(MultipartFile multipartFile, IUser user, A analysis, String label, Boolean isExecutable, DataReference dataReference) throws IOException {
    String originalFilename = multipartFile.getOriginalFilename();
    String fileNameLowerCase = UUID.randomUUID().toString();
    try {
        Path analysisPath = getAnalysisPath(analysis);
        Path targetPath = Paths.get(analysisPath.toString(), fileNameLowerCase);
        Files.copy(multipartFile.getInputStream(), targetPath, REPLACE_EXISTING);
        AnalysisFile analysisFile = new AnalysisFile();
        analysisFile.setDataReference(dataReference);
        analysisFile.setUuid(fileNameLowerCase);
        analysisFile.setAnalysis(analysis);
        analysisFile.setContentType(CommonFileUtils.getContentType(originalFilename, targetPath.toString()));
        analysisFile.setLabel(label);
        analysisFile.setAuthor(user);
        analysisFile.setUpdatedBy(user);
        analysisFile.setExecutable(false);
        analysisFile.setRealName(originalFilename);
        Date created = new Date();
        analysisFile.setCreated(created);
        analysisFile.setUpdated(created);
        analysisFile.setVersion(1);
        beforeSaveAnalysisFile(analysisFile);
        AnalysisFile saved = analysisFileRepository.save(analysisFile);
        analysis.getFiles().add(saved);
        afterSaveAnalysisFile(saved);
        if (Boolean.TRUE.equals(isExecutable)) {
            setIsExecutable(saved.getUuid());
        }
        preprocessorService.preprocessFile(analysis, analysisFile);
        eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(saved.getId(), saved.getRealName(), new FileInputStream(targetPath.toString()), AntivirusJobFileType.ANALYSIS_FILE)));
        return saved;
    } catch (IOException | RuntimeException ex) {
        String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
        LOGGER.error(message, ex);
        throw new IOException(message);
    }
}
Also used : Path(java.nio.file.Path) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) IOException(java.io.IOException) Date(java.util.Date) FileInputStream(java.io.FileInputStream)

Example 2 with AntivirusJob

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob in project ArachneCentralAPI by OHDSI.

the class BasePaperServiceImpl method saveFile.

@PreAuthorize("hasPermission(#paperId, 'Paper', " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).LIMITED_EDIT_PAPER)")
@Transactional(rollbackFor = Exception.class)
@Override
public String saveFile(Long paperId, MultipartFile file, PaperFileType fileType, String label, IUser user) throws IOException {
    if (file == null) {
        throw new IllegalArgumentException("File must not be null");
    }
    final Paper paper = get(paperId);
    final String realName = file.getOriginalFilename();
    final String contentType = CommonFileUtils.getContentType(realName, file);
    AbstractPaperFile paperFile = savePaperFile(fileType, label, user, paper, contentType, realName, null);
    fileService.saveFile(file, paperFile);
    AntivirusJobFileType antivirusJobFileType;
    switch(fileType) {
        case PAPER:
            antivirusJobFileType = AntivirusJobFileType.PAPER_PAPER_FILE;
            break;
        case PROTOCOL:
            antivirusJobFileType = AntivirusJobFileType.PAPER_PROTOCOL_FILE;
            break;
        default:
            throw new IllegalArgumentException();
    }
    eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(paperFile.getId(), paperFile.getRealName(), fileService.getFileInputStream(paperFile), antivirusJobFileType)));
    return paperFile.getUuid();
}
Also used : AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) AntivirusJobFileType(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType) Paper(com.odysseusinc.arachne.portal.model.Paper) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) AbstractPaperFile(com.odysseusinc.arachne.portal.model.AbstractPaperFile) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with AntivirusJob

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob in project ArachneCentralAPI by OHDSI.

the class AnalysisFilesSavingServiceImpl method saveFile.

@Override
@PreAuthorize("hasPermission(#analysis, " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).UPLOAD_ANALYSIS_FILES)")
public AnalysisFile saveFile(MultipartFile multipartFile, IUser user, A analysis, String label, Boolean isExecutable, DataReference dataReference) throws AlreadyExistException {
    ensureLabelIsUnique(analysis.getId(), label);
    String originalFilename = multipartFile.getOriginalFilename();
    String fileNameLowerCase = UUID.randomUUID().toString();
    try {
        Path analysisPath = analysisHelper.getAnalysisPath(analysis);
        Path targetPath = Paths.get(analysisPath.toString(), fileNameLowerCase);
        Files.copy(multipartFile.getInputStream(), targetPath, REPLACE_EXISTING);
        final String contentType = CommonFileUtils.getContentType(originalFilename, targetPath.toString());
        AnalysisFile analysisFile = buildNewAnalysisFileEntry(user, analysis, label, isExecutable, originalFilename, fileNameLowerCase, contentType);
        analysisFile.setDataReference(dataReference);
        AnalysisFile saved = analysisFileRepository.save(analysisFile);
        analysis.getFiles().add(saved);
        preprocessorService.preprocessFile(analysis, analysisFile);
        eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(saved.getId(), saved.getName(), new FileInputStream(targetPath.toString()), AntivirusJobFileType.ANALYSIS_FILE)));
        return saved;
    } catch (IOException | RuntimeException ex) {
        String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
        log.error(message, ex);
        throw new ArachneSystemRuntimeException(message);
    }
}
Also used : Path(java.nio.file.Path) ArachneSystemRuntimeException(com.odysseusinc.arachne.portal.exception.ArachneSystemRuntimeException) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) ValidationRuntimeException(com.odysseusinc.arachne.portal.exception.ValidationRuntimeException) ArachneSystemRuntimeException(com.odysseusinc.arachne.portal.exception.ArachneSystemRuntimeException) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with AntivirusJob

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob 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.getOne(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);
    }
}
Also used : UserStudy(com.odysseusinc.arachne.portal.model.UserStudy) Study(com.odysseusinc.arachne.portal.model.Study) FavouriteStudy(com.odysseusinc.arachne.portal.model.FavouriteStudy) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) StudyFile(com.odysseusinc.arachne.portal.model.StudyFile) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) IOException(java.io.IOException) Date(java.util.Date) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with AntivirusJob

use of com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob 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);
    }
}
Also used : Path(java.nio.file.Path) Study(com.odysseusinc.arachne.portal.model.Study) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) IOException(java.io.IOException) Date(java.util.Date) FileInputStream(java.io.FileInputStream) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) ByteArrayInputStream(java.io.ByteArrayInputStream) Analysis(com.odysseusinc.arachne.portal.model.Analysis) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

AntivirusJob (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob)7 AntivirusJobEvent (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent)7 IOException (java.io.IOException)5 IORuntimeException (com.odysseusinc.arachne.portal.exception.IORuntimeException)4 FileInputStream (java.io.FileInputStream)4 Path (java.nio.file.Path)4 Date (java.util.Date)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 AnalysisFile (com.odysseusinc.arachne.portal.model.AnalysisFile)3 InputStream (java.io.InputStream)3 Analysis (com.odysseusinc.arachne.portal.model.Analysis)2 Study (com.odysseusinc.arachne.portal.model.Study)2 AntivirusJobFileType (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArachneSystemRuntimeException (com.odysseusinc.arachne.portal.exception.ArachneSystemRuntimeException)1 ValidationRuntimeException (com.odysseusinc.arachne.portal.exception.ValidationRuntimeException)1 AbstractPaperFile (com.odysseusinc.arachne.portal.model.AbstractPaperFile)1 AntivirusStatus (com.odysseusinc.arachne.portal.model.AntivirusStatus)1 FavouriteStudy (com.odysseusinc.arachne.portal.model.FavouriteStudy)1 Paper (com.odysseusinc.arachne.portal.model.Paper)1