Search in sources :

Example 1 with AbstractPaperFile

use of com.odysseusinc.arachne.portal.model.AbstractPaperFile in project ArachneCentralAPI by OHDSI.

the class BasePaperController method getFile.

@ApiOperation("Get file of the Paper")
@RequestMapping(value = "/{id}/files/{fileUuid}", method = GET)
public FileDTO getFile(@PathVariable("id") Long id, @PathVariable("fileUuid") String uuid, @RequestParam("type") PaperFileType type, @RequestParam(defaultValue = "true") Boolean withContent) throws PermissionDeniedException, IOException {
    final AbstractPaperFile paperFile = paperService.getFile(id, uuid, type);
    FileDTO fileDto = conversionService.convert(paperFile, PaperFileDTO.class);
    if (withContent) {
        fileDto = FileDtoContentHandler.getInstance(fileDto, fileService.getPathToFile(paperFile).toFile()).withPdfConverter(toPdfConverter::convert).handle();
    }
    return fileDto;
}
Also used : PaperFileDTO(com.odysseusinc.arachne.portal.api.v1.dto.PaperFileDTO) FileDTO(com.odysseusinc.arachne.portal.api.v1.dto.FileDTO) UploadFileDTO(com.odysseusinc.arachne.portal.api.v1.dto.UploadFileDTO) AbstractPaperFile(com.odysseusinc.arachne.portal.model.AbstractPaperFile) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with AbstractPaperFile

use of com.odysseusinc.arachne.portal.model.AbstractPaperFile 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 AbstractPaperFile

use of com.odysseusinc.arachne.portal.model.AbstractPaperFile in project ArachneCentralAPI by OHDSI.

the class BasePaperServiceImpl method deleteFile.

@PreAuthorize("hasPermission(#paperId, 'Paper', " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).LIMITED_EDIT_PAPER)")
// @Transactional not usable for this method.
@Override
public void deleteFile(Long paperId, String fileUuid, PaperFileType fileType) throws FileNotFoundException {
    AbstractPaperFile paperFile;
    switch(fileType) {
        case PAPER:
            {
                paperFile = paperPaperFileRepository.findByPaperIdAndUuid(paperId, fileUuid).orElseThrow(() -> new NotExistException(AbstractPaperFile.class));
                paperPaperFileRepository.delete(paperFile.getId());
                break;
            }
        case PROTOCOL:
            {
                paperFile = paperProtocolFileRepository.findByPaperIdAndUuid(paperId, fileUuid).orElseThrow(() -> new NotExistException(AbstractPaperFile.class));
                paperProtocolFileRepository.delete(paperFile.getId());
                break;
            }
        default:
            {
                throw new IllegalArgumentException("Illegal filetype: " + fileType);
            }
    }
    fileService.delete(paperFile);
}
Also used : NotExistException(com.odysseusinc.arachne.portal.exception.NotExistException) AbstractPaperFile(com.odysseusinc.arachne.portal.model.AbstractPaperFile) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with AbstractPaperFile

use of com.odysseusinc.arachne.portal.model.AbstractPaperFile in project ArachneCentralAPI by OHDSI.

the class BasePaperServiceImpl method updateFile.

@PreAuthorize("hasPermission(#paperId, 'Paper', " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).EDIT_PAPER)")
@Override
public void updateFile(Long paperId, String uuid, MultipartFile multipartFile, PaperFileType fileType, IUser user) throws IOException {
    final AbstractPaperFile paperFile = getAbstractPaperFile(paperId, uuid, fileType);
    fileService.updateFile(multipartFile, paperFile);
}
Also used : AbstractPaperFile(com.odysseusinc.arachne.portal.model.AbstractPaperFile) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with AbstractPaperFile

use of com.odysseusinc.arachne.portal.model.AbstractPaperFile 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, String link, PaperFileType type, String label, IUser user) throws MalformedURLException {
    if (StringUtils.isEmpty(link)) {
        throw new IllegalArgumentException();
    }
    final Paper paper = get(paperId);
    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, HttpMethod.GET, entity, byte[].class);
    if (response.getStatusCode() == HttpStatus.OK) {
        String contentType = response.getHeaders().getContentType().toString();
        final AbstractPaperFile abstractPaperFile = savePaperFile(type, label, user, paper, CommonFileUtils.TYPE_LINK, fileName, link);
        return abstractPaperFile.getUuid();
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Paper(com.odysseusinc.arachne.portal.model.Paper) URL(java.net.URL) AbstractPaperFile(com.odysseusinc.arachne.portal.model.AbstractPaperFile) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AbstractPaperFile (com.odysseusinc.arachne.portal.model.AbstractPaperFile)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 Paper (com.odysseusinc.arachne.portal.model.Paper)2 ApiOperation (io.swagger.annotations.ApiOperation)2 Transactional (org.springframework.transaction.annotation.Transactional)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 FileDTO (com.odysseusinc.arachne.portal.api.v1.dto.FileDTO)1 PaperFileDTO (com.odysseusinc.arachne.portal.api.v1.dto.PaperFileDTO)1 UploadFileDTO (com.odysseusinc.arachne.portal.api.v1.dto.UploadFileDTO)1 NotExistException (com.odysseusinc.arachne.portal.exception.NotExistException)1 AntivirusJob (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob)1 AntivirusJobEvent (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent)1 AntivirusJobFileType (com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1