From b0f9b09309dc6cc55816ce904f43c62252d07528 Mon Sep 17 00:00:00 2001 From: chin Date: Sun, 15 May 2022 21:20:00 +0800 Subject: [PATCH] feat: add retrieve instance's metadata - Chore, Add typedef for get{Level}ImagesPath - Fix, missing series UID in not found message of retrieve series's metadata --- .../WADO-RS/retrieveInstanceMetadata.js | 48 +++++++++++++++++++ .../WADO-RS/retrieveSeriesMetadata.js | 2 +- .../WADO-RS/service/WADO-RS.service.js | 4 ++ api/dicom-web/index.js | 4 ++ utils/typeDef/WADO-RS/WADO-RS.def.js | 9 ++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 api/dicom-web/controller/WADO-RS/retrieveInstanceMetadata.js create mode 100644 utils/typeDef/WADO-RS/WADO-RS.def.js diff --git a/api/dicom-web/controller/WADO-RS/retrieveInstanceMetadata.js b/api/dicom-web/controller/WADO-RS/retrieveInstanceMetadata.js new file mode 100644 index 00000000..07fe7d5c --- /dev/null +++ b/api/dicom-web/controller/WADO-RS/retrieveInstanceMetadata.js @@ -0,0 +1,48 @@ +const mongoose = require("mongoose"); +const _ = require("lodash"); +const fs = require("fs"); +const path = require("path"); +const fileExist = require("../../../../utils/file/fileExist"); +const wadoService = require("./service/WADO-RS.service"); +const errorResponse = require("../../../../utils/errorResponse/errorResponseMessage"); +const { logger } = require("../../../../utils/log"); + +/** + * + * @param {import("http").IncomingMessage} req + * @param {import("http").ServerResponse} res + */ +module.exports = async function(req, res) { + logger.info(`[WADO-RS] [Get Study's Series' Instance Metadata] [instance UID: ${req.params.instanceUID}, series UID: ${req.params.seriesUID}, study UID: ${req.params.studyUID}]`); + try { + let responseMetadata = []; + let imagePathObj = await wadoService.getInstanceImagePath(req.params); + if (imagePathObj) { + let instanceDir = path.dirname(imagePathObj.instancePath); + let metadataPath = path.join(instanceDir, `${imagePathObj.instanceUID}.metadata.json`); + if (await fileExist(metadataPath)) { + let metadataJsonStr = fs.readFileSync(metadataPath, { encoding: "utf-8" }); + let metadataJson = JSON.parse(metadataJsonStr); + wadoService.addHostnameOfBulkDataUrl(metadataJson, req); + responseMetadata.push(metadataJson); + } + res.writeHead(200, { + "Content-Type": "application/dicom+json" + }); + return res.end(JSON.stringify(responseMetadata)); + } + res.writeHead(404); + return res.end(JSON.stringify( + errorResponse.getNotFoundErrorMessage( + `Not found metadata of instance UID: ${req.params.instanceUID}, series UID: ${req.params.seriesUID}, study UID: ${req.params.studyUID}` + ) + )); + } catch(e) { + let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e)); + console.error(errorStr); + res.writeHead(500, { + "Content-Type": "application/dicom+json" + }); + return res.end(); + } +}; \ No newline at end of file diff --git a/api/dicom-web/controller/WADO-RS/retrieveSeriesMetadata.js b/api/dicom-web/controller/WADO-RS/retrieveSeriesMetadata.js index 97cbc9c5..c279c0ce 100644 --- a/api/dicom-web/controller/WADO-RS/retrieveSeriesMetadata.js +++ b/api/dicom-web/controller/WADO-RS/retrieveSeriesMetadata.js @@ -36,7 +36,7 @@ module.exports = async function(req, res) { res.writeHead(404); return res.end(JSON.stringify( errorResponse.getNotFoundErrorMessage( - `Not found metadata of study UID: ${req.params.studyUID}` + `Not found metadata of series UID:${req.params.seriesUID} study UID: ${req.params.studyUID}` ) )); } catch(e) { diff --git a/api/dicom-web/controller/WADO-RS/service/WADO-RS.service.js b/api/dicom-web/controller/WADO-RS/service/WADO-RS.service.js index c8c737ee..d86c44db 100644 --- a/api/dicom-web/controller/WADO-RS/service/WADO-RS.service.js +++ b/api/dicom-web/controller/WADO-RS/service/WADO-RS.service.js @@ -7,6 +7,7 @@ const flatten = require("flat"); /** * * @param {import("http").IncomingMessage} req + * @return { string } */ function getAcceptType(req) { return req.headers.accept @@ -19,6 +20,7 @@ function getAcceptType(req) { /** * Get path list of all study's instances with specific study UID * @param {Object} iParam + * @return { Promise | undefined } */ async function getStudyImagesPath(iParam) { let { studyUID } = iParam; @@ -63,6 +65,7 @@ async function getStudyImagesPath(iParam) { /** * Get path list of all study's series' instances with specific study UID * @param {Object} iParam + * @return { Promise | undefined } * @returns */ async function getSeriesImagesPath(iParam) { @@ -115,6 +118,7 @@ async function getSeriesImagesPath(iParam) { /** * Get path * @param {Object} iParam + * @return { Promise | undefined } */ async function getInstanceImagePath(iParam) { let { studyUID, seriesUID, instanceUID } = iParam; diff --git a/api/dicom-web/index.js b/api/dicom-web/index.js index 635e2fe8..26f01b4e 100644 --- a/api/dicom-web/index.js +++ b/api/dicom-web/index.js @@ -251,6 +251,10 @@ app.get( "/studies/:studyUID/series/:seriesUID/metadata", require("./controller/WADO-RS/retrieveSeriesMetadata") ); +app.get( + "/studies/:studyUID/series/:seriesUID/instances/:instanceUID/metadata", + require("./controller/WADO-RS/retrieveInstanceMetadata") +); //#endregion diff --git a/utils/typeDef/WADO-RS/WADO-RS.def.js b/utils/typeDef/WADO-RS/WADO-RS.def.js new file mode 100644 index 00000000..980a7ee0 --- /dev/null +++ b/utils/typeDef/WADO-RS/WADO-RS.def.js @@ -0,0 +1,9 @@ +/** + * @typedef { Object } ImagePathObj + * @property { string } studyUID + * @property { string } seriesUID + * @property { string } instanceUID + * @property { string } instancePath + */ + +module.exports.unUse = {}; \ No newline at end of file