2019-07-26 15:04:28 +02:00
|
|
|
import { httpErrorToStr, checkDicomFile } from '../utils/helpers';
|
2019-07-11 12:18:25 +02:00
|
|
|
import { api } from 'dicomweb-client';
|
2020-04-23 04:01:34 +02:00
|
|
|
import { errorHandler } from '@ohif/core';
|
2019-07-10 10:26:19 +03:00
|
|
|
|
|
|
|
|
class DicomUploadService {
|
2019-07-26 15:04:28 +02:00
|
|
|
async smartUpload(files, url, uploadCallback, cancellationToken) {
|
2019-07-10 10:26:19 +03:00
|
|
|
const CHUNK_SIZE = 1; // Only one file per request is supported so far
|
|
|
|
|
const MAX_PARALLEL_JOBS = 50; // FIXME: tune MAX_PARALLEL_JOBS number
|
2019-07-26 15:04:28 +02:00
|
|
|
|
2019-07-10 10:26:19 +03:00
|
|
|
let filesArray = Array.from(files);
|
|
|
|
|
if (filesArray.length === 0) {
|
2019-07-26 15:04:28 +02:00
|
|
|
throw new Error('No files were provided.');
|
2019-07-10 10:26:19 +03:00
|
|
|
}
|
2019-07-26 15:04:28 +02:00
|
|
|
|
2019-07-10 10:26:19 +03:00
|
|
|
let parallelJobsCount = Math.min(filesArray.length, MAX_PARALLEL_JOBS);
|
|
|
|
|
let completed = false;
|
|
|
|
|
|
|
|
|
|
const processJob = async (resolve, reject) => {
|
|
|
|
|
while (filesArray.length > 0) {
|
|
|
|
|
if (cancellationToken.get()) return;
|
|
|
|
|
let chunk = filesArray.slice(0, CHUNK_SIZE);
|
|
|
|
|
filesArray = filesArray.slice(CHUNK_SIZE);
|
|
|
|
|
let error = null;
|
|
|
|
|
try {
|
|
|
|
|
if (chunk.length > 1) throw new Error('Not implemented');
|
2020-04-23 04:01:34 +02:00
|
|
|
if (chunk.length === 1) await this.simpleUpload(chunk[0], url);
|
2019-07-10 10:26:19 +03:00
|
|
|
} catch (err) {
|
|
|
|
|
// It looks like a stupid bug of Babel that err is not an actual Exception object
|
|
|
|
|
error = httpErrorToStr(err);
|
|
|
|
|
}
|
|
|
|
|
chunk.forEach(file => uploadCallback(file.fileId, error));
|
|
|
|
|
if (!completed && filesArray.length === 0) {
|
|
|
|
|
completed = true;
|
|
|
|
|
resolve();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await new Promise(resolve => {
|
|
|
|
|
for (let i = 0; i < parallelJobsCount; i++) {
|
|
|
|
|
processJob(resolve);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 15:04:28 +02:00
|
|
|
async simpleUpload(file, url) {
|
2019-07-10 10:26:19 +03:00
|
|
|
const client = this.getClient(url);
|
|
|
|
|
const loadedFile = await this.readFile(file);
|
|
|
|
|
const content = loadedFile.content;
|
|
|
|
|
if (!checkDicomFile(content))
|
2019-07-26 15:04:28 +02:00
|
|
|
throw new Error('This is not a valid DICOM file.');
|
|
|
|
|
|
2019-07-10 10:26:19 +03:00
|
|
|
await client.storeInstances({ datasets: [content] });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readFile(file) {
|
2019-07-10 14:54:27 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const reader = new FileReader();
|
2019-07-10 10:26:19 +03:00
|
|
|
reader.onload = () => {
|
|
|
|
|
resolve({
|
|
|
|
|
name: file.name,
|
|
|
|
|
size: file.size,
|
|
|
|
|
type: file.type,
|
|
|
|
|
content: reader.result,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
reader.onerror = error => reject(error);
|
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 15:04:28 +02:00
|
|
|
setRetrieveAuthHeaderFunction(func) {
|
|
|
|
|
this.retrieveAuthHeaderFunc = func;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-10 10:26:19 +03:00
|
|
|
getClient(url) {
|
2019-07-26 15:04:28 +02:00
|
|
|
const headers = this.retrieveAuthHeaderFunc();
|
2020-04-23 04:01:34 +02:00
|
|
|
const errorInterceptor = errorHandler.getHTTPErrorHandler();
|
2019-07-26 15:04:28 +02:00
|
|
|
|
|
|
|
|
// TODO: a bit weird we are creating a new dicomweb client instance for every upload
|
2019-07-11 12:18:25 +02:00
|
|
|
return new api.DICOMwebClient({
|
2019-07-10 10:26:19 +03:00
|
|
|
url,
|
2020-04-23 04:01:34 +02:00
|
|
|
headers,
|
2019-07-10 10:26:19 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default new DicomUploadService();
|