cod-dicomweb-server
Version:
A wadors server proxy that get data from a Cloud Optimized Dicom format.
53 lines (52 loc) • 1.82 kB
JavaScript
import { getDataRetrievalManager } from './dataRetrieval/dataRetrievalManager';
class FileManager {
files = {};
fileStreamingScriptName;
constructor({ fileStreamingScriptName }) {
this.fileStreamingScriptName = fileStreamingScriptName;
}
set(url, file) {
this.files[url] = file;
}
get(url, offsets) {
if (!this.files[url] || (offsets && this.files[url].position <= offsets.endByte)) {
return null;
}
return offsets ? this.files[url].data.slice(offsets.startByte, offsets.endByte) : this.files[url].data;
}
setPosition(url, position) {
if (this.files[url]) {
this.files[url].position = position;
}
}
getPosition(url) {
return this.files[url]?.position;
}
append(url, chunk, position) {
if (this.files[url] && position) {
this.files[url].data.set(chunk, position - chunk.length);
this.setPosition(url, position);
}
}
getTotalSize() {
return Object.entries(this.files).reduce((total, [url, { position }]) => {
return url.includes('?bytes=') ? total : total + position;
}, 0);
}
remove(url) {
const removedSize = this.getPosition(url);
delete this.files[url];
if (url.includes('?bytes=')) {
return;
}
const retrievalManager = getDataRetrievalManager();
retrievalManager.executeTask(this.fileStreamingScriptName, 'decreaseFetchedSize', removedSize);
}
purge() {
const totalSize = this.getTotalSize();
this.files = {};
const retrievalManager = getDataRetrievalManager();
retrievalManager.executeTask(this.fileStreamingScriptName, 'decreaseFetchedSize', totalSize);
}
}
export default FileManager;