Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 34x 170x | import dicomImageLoader from '@cornerstonejs/dicom-image-loader'; import { PubSubService } from '@ohif/core'; export const EVENTS = { PROGRESS: 'event:DicomFileUploader:progress', }; export interface DicomFileUploaderEvent { fileId: number; } export interface DicomFileUploaderProgressEvent extends DicomFileUploaderEvent { percentComplete: number; } export enum UploadStatus { NotStarted, InProgress, Success, Failed, Cancelled, } type CancelOrFailed = UploadStatus.Cancelled | UploadStatus.Failed; export class UploadRejection { message: string; status: CancelOrFailed; constructor(status: CancelOrFailed, message: string) { this.message = message; this.status = status; } } export default class DicomFileUploader extends PubSubService { private _file; private _fileId; private _dataSource; private _loadPromise; private _abortController = new AbortController(); private _status: UploadStatus = UploadStatus.NotStarted; private _percentComplete = 0; constructor(file, dataSource) { super(EVENTS); this._file = file; this._fileId = dicomImageLoader.wadouri.fileManager.add(file); this._dataSource = dataSource; } getFileId(): string { return this._fileId; } getFileName(): string { return this._file.name; } getFileSize(): number { return this._file.size; } cancel(): void { this._abortController.abort(); } getStatus(): UploadStatus { return this._status; } getPercentComplete(): number { return this._percentComplete; } async load(): Promise<void> { Iif (this._loadPromise) { // Already started loading, return the load promise. return this._loadPromise; } this._loadPromise = new Promise<void>((resolve, reject) => { // The upload listeners: fire progress events and/or settle the promise. const uploadCallbacks = { progress: evt => { Iif (!evt.lengthComputable) { // Progress computation is not possible. return; } this._status = UploadStatus.InProgress; this._percentComplete = Math.round((100 * evt.loaded) / evt.total); this._broadcastEvent(EVENTS.PROGRESS, { fileId: this._fileId, percentComplete: this._percentComplete, }); }, timeout: () => { this._reject(reject, new UploadRejection(UploadStatus.Failed, 'The request timed out.')); }, abort: () => { this._reject(reject, new UploadRejection(UploadStatus.Cancelled, 'Cancelled')); }, error: () => { this._reject(reject, new UploadRejection(UploadStatus.Failed, 'The request failed.')); }, }; // First try to load the file. dicomImageLoader.wadouri .loadFileRequest(this._fileId) .then(dicomFile => { Iif (this._abortController.signal.aborted) { this._reject(reject, new UploadRejection(UploadStatus.Cancelled, 'Cancelled')); return; } Iif (!this._checkDicomFile(dicomFile)) { // The file is not DICOM this._reject( reject, new UploadRejection(UploadStatus.Failed, 'Not a valid DICOM file.') ); return; } const request = new XMLHttpRequest(); this._addRequestCallbacks(request, uploadCallbacks); // Do the actual upload by supplying the DICOM file and upload callbacks/listeners. return this._dataSource.store .dicom(dicomFile, request) .then(() => { this._status = UploadStatus.Success; resolve(); }) .catch(reason => { this._reject(reject, reason); }); }) .catch(reason => { this._reject(reject, reason); }); }); return this._loadPromise; } private _isRejected(): boolean { return this._status === UploadStatus.Failed || this._status === UploadStatus.Cancelled; } private _reject(reject: (reason?: any) => void, reason: any) { Iif (this._isRejected()) { return; } Iif (reason instanceof UploadRejection) { this._status = reason.status; reject(reason); return; } this._status = UploadStatus.Failed; Iif (reason.message) { reject(new UploadRejection(UploadStatus.Failed, reason.message)); return; } reject(new UploadRejection(UploadStatus.Failed, reason)); } private _addRequestCallbacks(request: XMLHttpRequest, uploadCallbacks) { const abortCallback = () => request.abort(); this._abortController.signal.addEventListener('abort', abortCallback); for (const [eventName, callback] of Object.entries(uploadCallbacks)) { request.upload.addEventListener(eventName, callback); } const cleanUpCallback = () => { this._abortController.signal.removeEventListener('abort', abortCallback); for (const [eventName, callback] of Object.entries(uploadCallbacks)) { request.upload.removeEventListener(eventName, callback); } request.removeEventListener('loadend', cleanUpCallback); }; request.addEventListener('loadend', cleanUpCallback); } private _checkDicomFile(arrayBuffer: ArrayBuffer) { Iif (arrayBuffer.length <= 132) { return false; } const arr = new Uint8Array(arrayBuffer.slice(128, 132)); // bytes from 128 to 132 must be "DICM" return Array.from('DICM').every((char, i) => char.charCodeAt(0) === arr[i]); } } |