UNPKG

cod-dicomweb-server

Version:

A wadors server proxy that get data from a Cloud Optimized Dicom format.

65 lines (64 loc) 2.25 kB
class FileManager { files = {}; set(url, file) { this.files[url] = { ...file, lastModified: Date.now() }; } 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; this.files[url].lastModified = Date.now(); } } 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.values(this.files).reduce((total, { data }) => { return total + data.byteLength; }, 0); } remove(url) { try { delete this.files[url]; console.log(`Removed ${url} from CodDicomwebServer cache`); } catch (error) { console.warn(`Error removing ${url} from CodDicomwebServer cache:`, error); } } purge() { const fileURLs = Object.keys(this.files); const totalSize = this.getTotalSize(); fileURLs.forEach((url) => this.remove(url)); console.log(`Purged ${totalSize - this.getTotalSize()} bytes from CodDicomwebServer cache`); } decacheNecessaryBytes(url, bytesNeeded) { const totalSize = this.getTotalSize(); const filesToDelete = []; let collectiveSize = 0; Object.entries(this.files) .sort(([, a], [, b]) => a.lastModified - b.lastModified) .forEach(([key, file]) => { if (collectiveSize < bytesNeeded && key !== url) { filesToDelete.push(key); collectiveSize += file.data.byteLength; } }); filesToDelete.forEach((key) => this.remove(key)); console.log(`Decached ${totalSize - this.getTotalSize()} bytes`); return collectiveSize; } } export default FileManager;