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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | 34x 34x 34x 34x 34x 34x 272x 272x 272x 34x 34x 34x 322x 322x 322x 322x 34x 34x 34x 34x 34x 34x 34x 34x 288x 288x 288x 288x 34x 34x 34x 34x 34x 34x 34x 114487x 114487x 1271199x 1271199x 79496x 105596x 79496x 1191703x 9922x 9922x 114487x 34x 8891x 8891x 8891x 133x 133x 8891x 8891x 8891x 8891x 8891x 15288x 15288x 15288x 8891x 8891x 133x 34x 34x 34x 34x 133x 34x 34x 133x 133x 133x 34x 34x 34x 215x 215x 215x 2x 213x 15024x 15024x 15024x 15024x 213x 39203x 39203x 1299x 34x 34x 34x 34x 34x 272x 102x 272x | import { api } from 'dicomweb-client'; import { DicomMetadataStore, IWebApiDataSource, utils, errorHandler, classes } from '@ohif/core'; import { mapParams, search as qidoSearch, seriesInStudy, processResults, processSeriesResults, } from './qido.js'; import dcm4cheeReject from './dcm4cheeReject.js'; import getImageId from './utils/getImageId.js'; import dcmjs from 'dcmjs'; import { retrieveStudyMetadata, deleteStudyMetadataPromise } from './retrieveStudyMetadata.js'; import StaticWadoClient from './utils/StaticWadoClient'; import getDirectURL from '../utils/getDirectURL'; import { fixBulkDataURI } from './utils/fixBulkDataURI'; const { DicomMetaDictionary, DicomDict } = dcmjs.data; const { naturalizeDataset, denaturalizeDataset } = DicomMetaDictionary; const ImplementationClassUID = '2.25.270695996825855179949881587723571202391.2.0.0'; const ImplementationVersionName = 'OHIF-VIEWER-2.0.0'; const EXPLICIT_VR_LITTLE_ENDIAN = '1.2.840.10008.1.2.1'; const metadataProvider = classes.MetadataProvider; export type DicomWebConfig = { /** Data source name */ name: string; // wadoUriRoot - Legacy? (potentially unused/replaced) /** Base URL to use for QIDO requests */ qidoRoot?: string; wadoRoot?: string; // - Base URL to use for WADO requests wadoUri?: string; // - Base URL to use for WADO URI requests qidoSupportsIncludeField?: boolean; // - Whether QIDO supports the "Include" option to request additional fields in response imageRendering?: string; // - wadors | ? (unsure of where/how this is used) thumbnailRendering?: string; // - wadors | ? (unsure of where/how this is used) /** Whether the server supports reject calls (i.e. DCM4CHEE) */ supportsReject?: boolean; /** Request series meta async instead of blocking */ lazyLoadStudy?: boolean; /** indicates if the retrieves can fetch singlepart. Options are bulkdata, video, image, or true */ singlepart?: boolean | string; /** Transfer syntax to request from the server */ requestTransferSyntaxUID?: string; acceptHeader?: string[]; // - Accept header to use for requests /** Whether to omit quotation marks for multipart requests */ omitQuotationForMultipartRequest?: boolean; /** Whether the server supports fuzzy matching */ supportsFuzzyMatching?: boolean; /** Whether the server supports wildcard matching */ supportsWildcard?: boolean; /** Whether the server supports the native DICOM model */ supportsNativeDICOMModel?: boolean; /** Whether to enable request tag */ enableRequestTag?: boolean; /** Whether to enable study lazy loading */ enableStudyLazyLoad?: boolean; /** Whether to enable bulkDataURI */ bulkDataURI?: BulkDataURIConfig; /** Function that is called after the configuration is initialized */ onConfiguration: (config: DicomWebConfig, params) => DicomWebConfig; /** Whether to use the static WADO client */ staticWado?: boolean; /** User authentication service */ userAuthenticationService: Record<string, unknown>; }; export type BulkDataURIConfig = { /** Enable bulkdata uri configuration */ enabled?: boolean; /** * Remove the startsWith string. * This is used to correct reverse proxied URLs by removing the startsWith path */ startsWith?: string; /** * Adds this prefix path. Only used if the startsWith is defined and has * been removed. This allows replacing the base path. */ prefixWith?: string; /** Transform the bulkdata path. Used to replace a portion of the path */ transform?: (uri: string) => string; /** * Adds relative resolution to the path handling. * series is the default, as the metadata retrieved is series level. */ relativeResolution?: 'studies' | 'series'; }; /** * Creates a DICOM Web API based on the provided configuration. * * @param dicomWebConfig - Configuration for the DICOM Web API * @returns DICOM Web API object */ function createDicomWebApi(dicomWebConfig: DicomWebConfig, servicesManager) { const { userAuthenticationService } = servicesManager.services; let dicomWebConfigCopy, qidoConfig, wadoConfig, qidoDicomWebClient, wadoDicomWebClient, getAuthorizationHeader, generateWadoHeader; // Default to enabling bulk data retrieves, with no other customization as // this is part of hte base standard. dicomWebConfig.bulkDataURI ||= { enabled: true }; const implementation = { initialize: ({ params, query }) => { Iif (dicomWebConfig.onConfiguration && typeof dicomWebConfig.onConfiguration === 'function') { dicomWebConfig = dicomWebConfig.onConfiguration(dicomWebConfig, { params, query, }); } dicomWebConfigCopy = JSON.parse(JSON.stringify(dicomWebConfig)); getAuthorizationHeader = () => { const xhrRequestHeaders = {}; const authHeaders = userAuthenticationService.getAuthorizationHeader(); Iif (authHeaders && authHeaders.Authorization) { xhrRequestHeaders.Authorization = authHeaders.Authorization; } return xhrRequestHeaders; }; generateWadoHeader = () => { const authorizationHeader = getAuthorizationHeader(); //Generate accept header depending on config params const formattedAcceptHeader = utils.generateAcceptHeader( dicomWebConfig.acceptHeader, dicomWebConfig.requestTransferSyntaxUID, dicomWebConfig.omitQuotationForMultipartRequest ); return { ...authorizationHeader, Accept: formattedAcceptHeader, }; }; qidoConfig = { url: dicomWebConfig.qidoRoot, staticWado: dicomWebConfig.staticWado, singlepart: dicomWebConfig.singlepart, headers: userAuthenticationService.getAuthorizationHeader(), errorInterceptor: errorHandler.getHTTPErrorHandler(), supportsFuzzyMatching: dicomWebConfig.supportsFuzzyMatching, }; wadoConfig = { url: dicomWebConfig.wadoRoot, staticWado: dicomWebConfig.staticWado, singlepart: dicomWebConfig.singlepart, headers: userAuthenticationService.getAuthorizationHeader(), errorInterceptor: errorHandler.getHTTPErrorHandler(), supportsFuzzyMatching: dicomWebConfig.supportsFuzzyMatching, }; // TODO -> Two clients sucks, but its better than 1000. // TODO -> We'll need to merge auth later. qidoDicomWebClient = dicomWebConfig.staticWado ? new StaticWadoClient(qidoConfig) : new api.DICOMwebClient(qidoConfig); wadoDicomWebClient = dicomWebConfig.staticWado ? new StaticWadoClient(wadoConfig) : new api.DICOMwebClient(wadoConfig); }, query: { studies: { mapParams: mapParams.bind(), search: async function (origParams) { qidoDicomWebClient.headers = getAuthorizationHeader(); const { studyInstanceUid, seriesInstanceUid, ...mappedParams } = mapParams(origParams, { supportsFuzzyMatching: dicomWebConfig.supportsFuzzyMatching, supportsWildcard: dicomWebConfig.supportsWildcard, }) || {}; const results = await qidoSearch(qidoDicomWebClient, undefined, undefined, mappedParams); return processResults(results); }, processResults: processResults.bind(), }, series: { // mapParams: mapParams.bind(), search: async function (studyInstanceUid) { qidoDicomWebClient.headers = getAuthorizationHeader(); const results = await seriesInStudy(qidoDicomWebClient, studyInstanceUid); return processSeriesResults(results); }, // processResults: processResults.bind(), }, instances: { search: (studyInstanceUid, queryParameters) => { qidoDicomWebClient.headers = getAuthorizationHeader(); return qidoSearch.call( undefined, qidoDicomWebClient, studyInstanceUid, null, queryParameters ); }, }, }, retrieve: { /** * Generates a URL that can be used for direct retrieve of the bulkdata * * @param {object} params * @param {string} params.tag is the tag name of the URL to retrieve * @param {object} params.instance is the instance object that the tag is in * @param {string} params.defaultType is the mime type of the response * @param {string} params.singlepart is the type of the part to retrieve * @returns an absolute URL to the resource, if the absolute URL can be retrieved as singlepart, * or is already retrieved, or a promise to a URL for such use if a BulkDataURI */ directURL: params => { return getDirectURL( { wadoRoot: dicomWebConfig.wadoRoot, singlepart: dicomWebConfig.singlepart, }, params ); }, /** * Provide direct access to the dicom web client for certain use cases * where the dicom web client is used by an external library such as the * microscopy viewer. * Note this instance only needs to support the wado queries, and may not * support any QIDO or STOW operations. */ getWadoDicomWebClient: () => wadoDicomWebClient, bulkDataURI: async ({ StudyInstanceUID, BulkDataURI }) => { qidoDicomWebClient.headers = getAuthorizationHeader(); const options = { multipart: false, BulkDataURI, StudyInstanceUID, }; return qidoDicomWebClient.retrieveBulkData(options).then(val => { const ret = (val && val[0]) || undefined; return ret; }); }, series: { metadata: async ({ StudyInstanceUID, filters, sortCriteria, sortFunction, madeInClient = false, returnPromises = false, } = {}) => { Iif (!StudyInstanceUID) { throw new Error('Unable to query for SeriesMetadata without StudyInstanceUID'); } if (dicomWebConfig.enableStudyLazyLoad) { return implementation._retrieveSeriesMetadataAsync( StudyInstanceUID, filters, sortCriteria, sortFunction, madeInClient, returnPromises ); } return implementation._retrieveSeriesMetadataSync( StudyInstanceUID, filters, sortCriteria, sortFunction, madeInClient ); }, }, }, store: { dicom: async (dataset, request, dicomDict) => { wadoDicomWebClient.headers = getAuthorizationHeader(); if (dataset instanceof ArrayBuffer) { const options = { datasets: [dataset], request, }; await wadoDicomWebClient.storeInstances(options); } else { let effectiveDicomDict = dicomDict; Iif (!dicomDict) { const meta = { FileMetaInformationVersion: dataset._meta?.FileMetaInformationVersion?.Value, MediaStorageSOPClassUID: dataset.SOPClassUID, MediaStorageSOPInstanceUID: dataset.SOPInstanceUID, TransferSyntaxUID: EXPLICIT_VR_LITTLE_ENDIAN, ImplementationClassUID, ImplementationVersionName, }; const denaturalized = denaturalizeDataset(meta); const defaultDicomDict = new DicomDict(denaturalized); defaultDicomDict.dict = denaturalizeDataset(dataset); effectiveDicomDict = defaultDicomDict; } const part10Buffer = effectiveDicomDict.write(); const options = { datasets: [part10Buffer], request, }; await wadoDicomWebClient.storeInstances(options); } }, }, _retrieveSeriesMetadataSync: async ( StudyInstanceUID, filters, sortCriteria, sortFunction, madeInClient ) => { const enableStudyLazyLoad = false; wadoDicomWebClient.headers = generateWadoHeader(); // data is all SOPInstanceUIDs const data = await retrieveStudyMetadata( wadoDicomWebClient, StudyInstanceUID, enableStudyLazyLoad, filters, sortCriteria, sortFunction, dicomWebConfig ); // first naturalize the data const naturalizedInstancesMetadata = data.map(naturalizeDataset); const seriesSummaryMetadata = {}; const instancesPerSeries = {}; naturalizedInstancesMetadata.forEach(instance => { Iif (!seriesSummaryMetadata[instance.SeriesInstanceUID]) { seriesSummaryMetadata[instance.SeriesInstanceUID] = { StudyInstanceUID: instance.StudyInstanceUID, StudyDescription: instance.StudyDescription, SeriesInstanceUID: instance.SeriesInstanceUID, SeriesDescription: instance.SeriesDescription, SeriesNumber: instance.SeriesNumber, SeriesTime: instance.SeriesTime, SOPClassUID: instance.SOPClassUID, ProtocolName: instance.ProtocolName, Modality: instance.Modality, }; } Iif (!instancesPerSeries[instance.SeriesInstanceUID]) { instancesPerSeries[instance.SeriesInstanceUID] = []; } const imageId = implementation.getImageIdsForInstance({ instance, }); instance.imageId = imageId; instance.wadoRoot = dicomWebConfig.wadoRoot; instance.wadoUri = dicomWebConfig.wadoUri; metadataProvider.addImageIdToUIDs(imageId, { StudyInstanceUID, SeriesInstanceUID: instance.SeriesInstanceUID, SOPInstanceUID: instance.SOPInstanceUID, }); instancesPerSeries[instance.SeriesInstanceUID].push(instance); }); // grab all the series metadata const seriesMetadata = Object.values(seriesSummaryMetadata); DicomMetadataStore.addSeriesMetadata(seriesMetadata, madeInClient); Object.keys(instancesPerSeries).forEach(seriesInstanceUID => DicomMetadataStore.addInstances(instancesPerSeries[seriesInstanceUID], madeInClient) ); return seriesSummaryMetadata; }, _retrieveSeriesMetadataAsync: async ( StudyInstanceUID, filters, sortCriteria, sortFunction, madeInClient = false, returnPromises = false ) => { const enableStudyLazyLoad = true; wadoDicomWebClient.headers = generateWadoHeader(); // Get Series const { preLoadData: seriesSummaryMetadata, promises: seriesPromises } = await retrieveStudyMetadata( wadoDicomWebClient, StudyInstanceUID, enableStudyLazyLoad, filters, sortCriteria, sortFunction, dicomWebConfig ); /** * Adds the retrieve bulkdata function to naturalized DICOM data. * This is done recursively, for sub-sequences. */ const addRetrieveBulkDataNaturalized = (naturalized, instance = naturalized) => { Iif (!naturalized) { return naturalized; } for (const key of Object.keys(naturalized)) { const value = naturalized[key]; if (Array.isArray(value) && typeof value[0] === 'object') { // Fix recursive values const validValues = value.filter(Boolean); validValues.forEach(child => addRetrieveBulkDataNaturalized(child, instance)); continue; } // The value.Value will be set with the bulkdata read value // in which case it isn't necessary to re-read this. if (value && value.BulkDataURI && !value.Value) { // handle the scenarios where bulkDataURI is relative path fixBulkDataURI(value, instance, dicomWebConfig); // Provide a method to fetch bulkdata value.retrieveBulkData = retrieveBulkData.bind(qidoDicomWebClient, value); } } return naturalized; }; /** * naturalizes the dataset, and adds a retrieve bulkdata method * to any values containing BulkDataURI. * @param {*} instance * @returns naturalized dataset, with retrieveBulkData methods */ const addRetrieveBulkData = instance => { const naturalized = naturalizeDataset(instance); // if we know the server doesn't use bulkDataURI, then don't Iif (!dicomWebConfig.bulkDataURI?.enabled) { return naturalized; } return addRetrieveBulkDataNaturalized(naturalized); }; // Async load series, store as retrieved function storeInstances(instances) { const naturalizedInstances = instances.map(addRetrieveBulkData); // Adding instanceMetadata to OHIF MetadataProvider naturalizedInstances.forEach(instance => { instance.wadoRoot = dicomWebConfig.wadoRoot; instance.wadoUri = dicomWebConfig.wadoUri; const { StudyInstanceUID, SeriesInstanceUID, SOPInstanceUID } = instance; const numberOfFrames = instance.NumberOfFrames || 1; // Process all frames consistently, whether single or multiframe for (let i = 0; i < numberOfFrames; i++) { const frameNumber = i + 1; const frameImageId = implementation.getImageIdsForInstance({ instance, frame: frameNumber, }); // Add imageId specific mapping to this data as the URL isn't necessarily WADO-URI. metadataProvider.addImageIdToUIDs(frameImageId, { StudyInstanceUID, SeriesInstanceUID, SOPInstanceUID, frameNumber: numberOfFrames > 1 ? frameNumber : undefined, }); } // Adding imageId to each instance // Todo: This is not the best way I can think of to let external // metadata handlers know about the imageId that is stored in the store const imageId = implementation.getImageIdsForInstance({ instance, }); instance.imageId = imageId; }); DicomMetadataStore.addInstances(naturalizedInstances, madeInClient); } function setSuccessFlag() { const study = DicomMetadataStore.getStudy(StudyInstanceUID); Iif (!study) { return; } study.isLoaded = true; } // Google Cloud Healthcare doesn't return StudyInstanceUID, so we need to add // it manually here seriesSummaryMetadata.forEach(aSeries => { aSeries.StudyInstanceUID = StudyInstanceUID; }); DicomMetadataStore.addSeriesMetadata(seriesSummaryMetadata, madeInClient); const seriesDeliveredPromises = seriesPromises.map(promise => { Iif (!returnPromises) { promise?.start(); } return promise.then(instances => { storeInstances(instances); }); }); if (returnPromises) { Promise.all(seriesDeliveredPromises).then(() => setSuccessFlag()); return seriesPromises; } else E{ await Promise.all(seriesDeliveredPromises); setSuccessFlag(); } return seriesSummaryMetadata; }, deleteStudyMetadataPromise, getImageIdsForDisplaySet(displaySet) { const images = displaySet.images; const imageIds = []; if (!images) { return imageIds; } displaySet.images.forEach(instance => { const NumberOfFrames = instance.NumberOfFrames; Iif (NumberOfFrames > 1) { for (let frame = 1; frame <= NumberOfFrames; frame++) { const imageId = this.getImageIdsForInstance({ instance, frame, }); imageIds.push(imageId); } } else { const imageId = this.getImageIdsForInstance({ instance }); imageIds.push(imageId); } }); return imageIds; }, getImageIdsForInstance({ instance, frame = undefined }) { const imageIds = getImageId({ instance, frame, config: dicomWebConfig, }); return imageIds; }, getConfig() { return dicomWebConfigCopy; }, getStudyInstanceUIDs({ params, query }) { const paramsStudyInstanceUIDs = params.StudyInstanceUIDs || params.studyInstanceUIDs; const queryStudyInstanceUIDs = utils.splitComma( query.getAll('StudyInstanceUIDs').concat(query.getAll('studyInstanceUIDs')) ); const StudyInstanceUIDs = (queryStudyInstanceUIDs.length && queryStudyInstanceUIDs) || paramsStudyInstanceUIDs; const StudyInstanceUIDsAsArray = StudyInstanceUIDs && Array.isArray(StudyInstanceUIDs) ? StudyInstanceUIDs : [StudyInstanceUIDs]; return StudyInstanceUIDsAsArray; }, }; if (dicomWebConfig.supportsReject) { implementation.reject = dcm4cheeReject(dicomWebConfig.wadoRoot, getAuthorizationHeader); } return IWebApiDataSource.create(implementation); } /** * A bindable function that retrieves the bulk data against this as the * dicomweb client, and on the given value element. * * @param value - a bind value that stores the retrieve value to short circuit the * next retrieve instance. * @param options - to allow specifying the content type. */ function retrieveBulkData(value, options = {}) { const { mediaType } = options; const useOptions = { // The bulkdata fetches work with either multipart or // singlepart, so set multipart to false to let the server // decide which type to respond with. multipart: false, BulkDataURI: value.BulkDataURI, mediaTypes: mediaType ? [{ mediaType }, { mediaType: 'application/octet-stream' }] : undefined, ...options, }; return this.retrieveBulkData(useOptions).then(val => { // There are DICOM PDF cases where the first ArrayBuffer in the array is // the bulk data and DICOM video cases where the second ArrayBuffer is // the bulk data. Here we play it safe and do a find. const ret = (val instanceof Array && val.find(arrayBuffer => arrayBuffer?.byteLength)) || undefined; value.Value = ret; return ret; }); } export { createDicomWebApi }; |