From a877cd49d7dbf802862597f4bf2203d58efc2055 Mon Sep 17 00:00:00 2001 From: chin Date: Sun, 30 Apr 2023 17:18:37 +0800 Subject: [PATCH] feat(wado-uri): #5 --- api/WADO-URI/service/WADO-URI.service.js | 32 ++++++++++++++++++++++-- error/dicom-instance.js | 11 ++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/api/WADO-URI/service/WADO-URI.service.js b/api/WADO-URI/service/WADO-URI.service.js index c6756de0..a187caf2 100644 --- a/api/WADO-URI/service/WADO-URI.service.js +++ b/api/WADO-URI/service/WADO-URI.service.js @@ -6,7 +6,7 @@ const { Dcm2JpgExecutor } = require("../../../models/DICOM/dcm4che/wrapper/org/g const { Dcm2JpgExecutor$Dcm2JpgOptions } = require("../../../models/DICOM/dcm4che/wrapper/org/github/chinlinlee/dcm2jpg/Dcm2JpgExecutor$Dcm2JpgOptions"); const sharp = require('sharp'); const Magick = require("../../../models/magick"); -const { NotFoundInstanceError, InvalidFrameNumberError } = require("../../../error/dicom-instance"); +const { NotFoundInstanceError, InvalidFrameNumberError, InstanceGoneError } = require("../../../error/dicom-instance"); const dicomModel = require("../../../models/mongodb/models/dicom"); class WadoUriService { @@ -36,6 +36,16 @@ class WadoUriService { "Content-Type": "application/dicom+json" }); return this.response.end(); + } else if (e instanceof InstanceGoneError) { + this.response.writeHead(410, { + "Content-Type": "application/dicom+json" + }); + return this.response.end(JSON.stringify({ + Details: e.message, + HttpStatus: 410, + Message: "Image Gone", + Method: "GET" + })); } throw e; @@ -73,6 +83,16 @@ class WadoUriService { Method: "GET" })); + } else if (e instanceof InstanceGoneError) { + this.response.writeHead(410, { + "Content-Type": "application/dicom+json" + }); + return this.response.end(JSON.stringify({ + Details: e.message, + HttpStatus: 410, + Message: "Image Gone", + Method: "GET" + })); } throw e; @@ -85,7 +105,7 @@ class WadoUriService { * @returns {Promise} */ async getDicomInstanceReadStream() { - + let imagePathObj = await this.getDicomInstancePathObj(); return fs.createReadStream(imagePathObj.instancePath); @@ -105,6 +125,14 @@ class WadoUriService { }); if (imagePathObj) { + + try { + await fs.promises.access(imagePathObj.instancePath, fs.constants.F_OK); + } catch(e) { + console.error(e); + throw new InstanceGoneError("The image is deleted permanently, but meta data remain"); + } + return imagePathObj; } diff --git a/error/dicom-instance.js b/error/dicom-instance.js index 4c317438..a60d1378 100644 --- a/error/dicom-instance.js +++ b/error/dicom-instance.js @@ -7,6 +7,15 @@ class NotFoundInstanceError extends Error { } } +class InstanceGoneError extends Error { + constructor(message) { + super(message); + Error.captureStackTrace(this, this.constructor); + + this.name = this.constructor.name; + } +} + class InvalidFrameNumberError extends Error { constructor(message) { super(message); @@ -17,4 +26,6 @@ class InvalidFrameNumberError extends Error { } module.exports.NotFoundInstanceError = NotFoundInstanceError; +module.exports.InstanceGoneError = InstanceGoneError; module.exports.InvalidFrameNumberError = InvalidFrameNumberError; +