diff --git a/shanoir-ng-datasets/src/main/java/org/shanoir/ng/dataset/service/DatasetDownloaderServiceImpl.java b/shanoir-ng-datasets/src/main/java/org/shanoir/ng/dataset/service/DatasetDownloaderServiceImpl.java index dd065fda98..11aa56c5a5 100644 --- a/shanoir-ng-datasets/src/main/java/org/shanoir/ng/dataset/service/DatasetDownloaderServiceImpl.java +++ b/shanoir-ng-datasets/src/main/java/org/shanoir/ng/dataset/service/DatasetDownloaderServiceImpl.java @@ -117,69 +117,21 @@ public void massiveDownload(String format, List datasets, HttpServletRe try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) { for (Dataset dataset : datasets) { - if (!dataset.isDownloadable()) { - downloadResults.put(dataset.getId(), new DatasetDownloadError("Dataset not downloadable", DatasetDownloadError.ERROR)); - continue; - } - DatasetDownloadError downloadResult = new DatasetDownloadError(); - downloadResults.put(dataset.getId(), downloadResult); - // Create a new folder organized by subject / examination String subjectName = getSubjectName(dataset); if (subjectName.contains(File.separator)) { subjectName = subjectName.replaceAll(File.separator, "_"); } + String studyName = studyRepository.findById(dataset.getStudyId()).orElse(null).getName(); + String datasetFilePath = null; if (datasets.size() != 1) { datasetFilePath = getDatasetFilepath(dataset, studyName, subjectName); } + manageDatasetDownload(dataset, downloadResults, zipOutputStream, subjectName, datasetFilePath, format, withManifest, filesByAcquisitionId, converterId); - List pathURLs = new ArrayList<>(); - - if (dataset.getDatasetProcessing() != null) { - // DOWNLOAD PROCESSED DATASET - DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult); - DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, datasetFilePath); - } else if (dataset instanceof EegDataset) { - // DOWNLOAD EEG - DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.EEG, downloadResult); - DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, datasetFilePath); - } else if (dataset instanceof BidsDataset) { - // DOWNLOAD BIDS - DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.BIDS, downloadResult); - DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, datasetFilePath); - // Manage errors here - } else if (DCM.equals(format)) { - // DOWNLOAD DICOM - DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.DICOM, downloadResult); - List files = downloader.downloadDicomFilesForURLsAsZip(pathURLs, zipOutputStream, subjectName, dataset, datasetFilePath, downloadResult); - if (withManifest) { - filesByAcquisitionId.putIfAbsent(dataset.getDatasetAcquisition().getId(), new ArrayList<>()); - filesByAcquisitionId.get(dataset.getDatasetAcquisition().getId()).addAll(files); - } - } else if (NII.equals(format)) { - // Check if we have a specific converter -> nifti reconversion - if (converterId != null) { - reconvertToNifti(format, converterId, dataset, pathURLs, downloadResult, subjectName, zipOutputStream); - } else { - // Check that we have existing nifti, otherwise reconvert using dcm2niix by default. - DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult); - if (!pathURLs.isEmpty()) { - List files = DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, datasetFilePath); - } else { - // Reconvert using dcm2niix by default. - reconvertToNifti(format, DEFAULT_NIFTI_CONVERTER_ID, dataset, pathURLs, downloadResult, subjectName, zipOutputStream); - } - } - } else { - downloadResult.update("Dataset format was not adapted to dataset download choosen", DatasetDownloadError.ERROR); - } - - if (downloadResult.getStatus() == null) { - downloadResults.remove(dataset.getId()); - } } if(!filesByAcquisitionId.isEmpty()){ DatasetFileUtils.writeManifestForExport(zipOutputStream, filesByAcquisitionId); @@ -208,6 +160,60 @@ public void massiveDownload(String format, List datasets, HttpServletRe } } + protected void manageDatasetDownload(Dataset dataset, Map downloadResults, ZipOutputStream zipOutputStream, String subjectName, String datasetFilePath, String format, boolean withManifest, Map> filesByAcquisitionId, Long converterId) throws IOException, RestServiceException { + if (!dataset.isDownloadable()) { + downloadResults.put(dataset.getId(), new DatasetDownloadError("Dataset not downloadable", DatasetDownloadError.ERROR)); + return; + } + DatasetDownloadError downloadResult = new DatasetDownloadError(); + downloadResults.put(dataset.getId(), downloadResult); + + List pathURLs = new ArrayList<>(); + + if (dataset.getDatasetProcessing() != null) { + // DOWNLOAD PROCESSED DATASET + DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult); + DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, datasetFilePath); + } else if (dataset instanceof EegDataset) { + // DOWNLOAD EEG + DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.EEG, downloadResult); + DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, datasetFilePath); + } else if (dataset instanceof BidsDataset) { + // DOWNLOAD BIDS + DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.BIDS, downloadResult); + DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, datasetFilePath); + // Manage errors here + } else if (Objects.equals("dcm", format)) { + // DOWNLOAD DICOM + DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.DICOM, downloadResult); + List files = downloader.downloadDicomFilesForURLsAsZip(pathURLs, zipOutputStream, subjectName, dataset, datasetFilePath, downloadResult); + if (withManifest) { + filesByAcquisitionId.putIfAbsent(dataset.getDatasetAcquisition().getId(), new ArrayList<>()); + filesByAcquisitionId.get(dataset.getDatasetAcquisition().getId()).addAll(files); + } + } else if (Objects.equals("nii", format)) { + // Check if we have a specific converter -> nifti reconversion + if (converterId != null) { + reconvertToNifti(format, converterId, dataset, pathURLs, downloadResult, subjectName, zipOutputStream); + } else { + // Check that we have existing nifti, otherwise reconvert using dcm2niix by default. + DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult); + if (!pathURLs.isEmpty()) { + List files = DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, datasetFilePath); + } else { + // Reconvert using dcm2niix by default. + reconvertToNifti(format, DEFAULT_NIFTI_CONVERTER_ID, dataset, pathURLs, downloadResult, subjectName, zipOutputStream); + } + } + } else { + downloadResult.update("Dataset format was not adapted to dataset download choosen", DatasetDownloadError.ERROR); + } + + if (downloadResult.getStatus() == null) { + downloadResults.remove(dataset.getId()); + } + } + protected void reconvertToNifti(String format, Long converterId, Dataset dataset, List pathURLs, DatasetDownloadError downloadResult, String subjectName, ZipOutputStream zipOutputStream) throws RestServiceException, IOException { File userDir = DatasetFileUtils.getUserImportDir("/tmp"); String tmpFilePath = userDir + File.separator + dataset.getId() + "_" + format; diff --git a/shanoir-ng-datasets/src/main/java/org/shanoir/ng/processing/service/ProcessingDownloaderServiceImpl.java b/shanoir-ng-datasets/src/main/java/org/shanoir/ng/processing/service/ProcessingDownloaderServiceImpl.java index 6844c332be..b68dcfb7d6 100644 --- a/shanoir-ng-datasets/src/main/java/org/shanoir/ng/processing/service/ProcessingDownloaderServiceImpl.java +++ b/shanoir-ng-datasets/src/main/java/org/shanoir/ng/processing/service/ProcessingDownloaderServiceImpl.java @@ -77,7 +77,7 @@ private void manageProcessingsDownload(List processingList, M String processingFilePath = getExecFilepath(processing.getId(), getExaminationDatas(processing.getInputDatasets())); String subjectName = getProcessingSubject(processing); for (Dataset dataset : Stream.concat(processing.getInputDatasets().stream(), processing.getOutputDatasets().stream()).toList()) { - manageDatasetDownload(dataset, downloadResults, zipOutputStream, subjectName, processingFilePath, format, withManifest, filesByAcquisitionId, converterId); + manageDatasetDownload(dataset, downloadResults, zipOutputStream, subjectName, processingFilePath + "/" + shapeForPath(dataset.getName()), format, withManifest, filesByAcquisitionId, converterId); } } @@ -95,60 +95,6 @@ private void manageProcessingsDownload(List processingList, M } } - private void manageDatasetDownload(Dataset dataset, Map downloadResults, ZipOutputStream zipOutputStream, String subjectName, String processingFilePath, String format, boolean withManifest, Map> filesByAcquisitionId, Long converterId) throws IOException, RestServiceException { - if (!dataset.isDownloadable()) { - downloadResults.put(dataset.getId(), new DatasetDownloadError("Dataset not downloadable", DatasetDownloadError.ERROR)); - return; - } - DatasetDownloadError downloadResult = new DatasetDownloadError(); - downloadResults.put(dataset.getId(), downloadResult); - - List pathURLs = new ArrayList<>(); - - if (dataset.getDatasetProcessing() != null) { - // DOWNLOAD PROCESSED DATASET - DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult); - DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, processingFilePath); - } else if (dataset instanceof EegDataset) { - // DOWNLOAD EEG - DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.EEG, downloadResult); - DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, processingFilePath); - } else if (dataset instanceof BidsDataset) { - // DOWNLOAD BIDS - DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.BIDS, downloadResult); - DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, true, processingFilePath); - // Manage errors here - } else if (Objects.equals("dcm", format)) { - // DOWNLOAD DICOM - DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.DICOM, downloadResult); - List files = downloader.downloadDicomFilesForURLsAsZip(pathURLs, zipOutputStream, subjectName, dataset, processingFilePath + "/" + shapeForPath(dataset.getName()), downloadResult); - if (withManifest) { - filesByAcquisitionId.putIfAbsent(dataset.getDatasetAcquisition().getId(), new ArrayList<>()); - filesByAcquisitionId.get(dataset.getDatasetAcquisition().getId()).addAll(files); - } - } else if (Objects.equals("nii", format)) { - // Check if we have a specific converter -> nifti reconversion - if (converterId != null) { - reconvertToNifti(format, converterId, dataset, pathURLs, downloadResult, subjectName, zipOutputStream); - } else { - // Check that we have existing nifti, otherwise reconvert using dcm2niix by default. - DatasetFileUtils.getDatasetFilePathURLs(dataset, pathURLs, DatasetExpressionFormat.NIFTI_SINGLE_FILE, downloadResult); - if (!pathURLs.isEmpty()) { - List files = DatasetFileUtils.copyNiftiFilesForURLs(pathURLs, zipOutputStream, dataset, subjectName, false, processingFilePath + "/" + shapeForPath(dataset.getName())); - } else { - // Reconvert using dcm2niix by default. - reconvertToNifti(format, DEFAULT_NIFTI_CONVERTER_ID, dataset, pathURLs, downloadResult, subjectName, zipOutputStream); - } - } - } else { - downloadResult.update("Dataset format was not adapted to dataset download choosen", DatasetDownloadError.ERROR); - } - - if (downloadResult.getStatus() == null) { - downloadResults.remove(dataset.getId()); - } - } - public void massiveDownloadByExaminations(List examinationList, String processingComment, boolean resultOnly, String format, HttpServletResponse response, boolean withManifest, Long converterId) throws RestServiceException { List processingIdsList = datasetProcessingRepository.findAllIdsByExaminationIds(examinationList.stream().map(Examination::getId).toList()); List processingList = datasetProcessingService.findAllById(processingIdsList).stream().filter(it -> Objects.equals(it.getComment(), processingComment)).toList();