Search in sources :

Example 1 with StudyFile

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();
}
Also used : StudyFile(com.odysseusinc.arachne.portal.model.StudyFile) InputStream(java.io.InputStream) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with StudyFile

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);
    }
}
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 3 with StudyFile

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);
    }
}
Also used : UserStudy(com.odysseusinc.arachne.portal.model.UserStudy) Study(com.odysseusinc.arachne.portal.model.Study) FavouriteStudy(com.odysseusinc.arachne.portal.model.FavouriteStudy) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) StudyFile(com.odysseusinc.arachne.portal.model.StudyFile) IOException(java.io.IOException) URL(java.net.URL) Date(java.util.Date) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with StudyFile

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;
}
Also used : StudyFile(com.odysseusinc.arachne.portal.model.StudyFile) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with StudyFile

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);
    }
}
Also used : StudyFile(com.odysseusinc.arachne.portal.model.StudyFile) AntivirusJobResponse(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobResponse) EventListener(org.springframework.context.event.EventListener) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

StudyFile (com.odysseusinc.arachne.portal.model.StudyFile)9 IORuntimeException (com.odysseusinc.arachne.portal.exception.IORuntimeException)3 FavouriteStudy (com.odysseusinc.arachne.portal.model.FavouriteStudy)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 Study (com.odysseusinc.arachne.portal.model.Study)2 StudyDataSourceLink (com.odysseusinc.arachne.portal.model.StudyDataSourceLink)2 UserStudy (com.odysseusinc.arachne.portal.model.UserStudy)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Date (java.util.Date)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 EntityGraph (com.cosium.spring.data.jpa.entity.graph.domain.EntityGraph)1 CommonHealthStatus (com.odysseusinc.arachne.commons.api.v1.dto.CommonHealthStatus)1 CommonFileUtils (com.odysseusinc.arachne.commons.utils.CommonFileUtils)1 BaseAnalysisDTO (com.odysseusinc.arachne.portal.api.v1.dto.BaseAnalysisDTO)1 DataSourceDTO (com.odysseusinc.arachne.portal.api.v1.dto.DataSourceDTO)1 FileDTO (com.odysseusinc.arachne.portal.api.v1.dto.FileDTO)1 ParticipantDTO (com.odysseusinc.arachne.portal.api.v1.dto.ParticipantDTO)1 PermissionsDTO (com.odysseusinc.arachne.portal.api.v1.dto.PermissionsDTO)1 StudyDTO (com.odysseusinc.arachne.portal.api.v1.dto.StudyDTO)1