Skip to content

Commit

Permalink
refactor: split getThumbnailAndResponse into two responsibility things
Browse files Browse the repository at this point in the history
- remove `getThumbnailAndResponse` method in wado uri service
- for right responsibility:
  - response should in controller, not in service
  - so, move `response.end` into controller
  - in service, throw error instead of response
  • Loading branch information
Chinlinlee committed Jan 1, 2024
1 parent 553c605 commit d99f68a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
25 changes: 7 additions & 18 deletions api/dicom-web/controller/WADO-RS/service/thumbnail.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const errorResponse = require("../../../../../utils/errorResponse/errorResponseM
const renderedService = require("@api/dicom-web/controller/WADO-RS/service/rendered.service");
const _ = require("lodash");
const { getUidsString } = require("./WADO-RS.service");
const { NotFoundInstanceError } = require("@error/dicom-instance");
class ThumbnailService {

/**
Expand All @@ -18,21 +19,19 @@ class ThumbnailService {
this.apiLogger = apiLogger;
}

async getThumbnailAndResponse() {
async getThumbnail() {
if (!_.get(this.request, "query.viewport")) {
_.set(this.request, "query.viewport", "100,100");
}

let instanceFramesObj = await this.thumbnailFactory.getThumbnailInstance();
if (this.checkInstanceExists(instanceFramesObj)) {
return;
}
this.checkInstanceExists(instanceFramesObj);

let thumbnail = await this.getThumbnailByInstance(instanceFramesObj);
if (thumbnail) {
return this.response.end(thumbnail, "binary");
if (!thumbnail) {
throw new Error(`Can not process this image, instanceUID: ${instanceFramesObj.instanceUID}`);
}
throw new Error(`Can not process this image, instanceUID: ${instanceFramesObj.instanceUID}`);
return thumbnail;
}

async getThumbnailByInstance(instanceFramesObj) {
Expand All @@ -57,18 +56,8 @@ class ThumbnailService {

checkInstanceExists(instanceFramesObj) {
if (!instanceFramesObj) {
this.response.writeHead(404, {
"Content-Type": "application/dicom+json"
});
let notFoundMessage = errorResponse.getNotFoundErrorMessage(`Not Found, ${getUidsString(this.thumbnailFactory.uids)}`);

let notFoundMessageStr = JSON.stringify(notFoundMessage);

this.apiLogger.logger.warn(`[${notFoundMessageStr}]`);

return this.response.end(notFoundMessageStr);
throw new NotFoundInstanceError(`Not Found, ${getUidsString(this.thumbnailFactory.uids)}`);
}
return undefined;
}
}

Expand Down
16 changes: 5 additions & 11 deletions api/dicom-web/controller/WADO-RS/thumbnail/base.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Controller } = require("@root/api/controller.class");
const { StudyImagePathFactory } = require("@api/dicom-web/controller/WADO-RS/service/WADO-RS.service");
const { ThumbnailService } = require("../service/thumbnail.service");
const { ApiLogger } = require("@root/utils/logs/api-logger");
const { ApiErrorArrayHandler } = require("@error/api-errors.handler");

class BaseThumbnailController extends Controller {
constructor(req, res) {
Expand All @@ -19,18 +20,11 @@ class BaseThumbnailController extends Controller {
try {
this.logAction();
let thumbnailService = new ThumbnailService(this.request, this.response, this.apiLogger, this.factory);
return thumbnailService.getThumbnailAndResponse();
let thumbnail = await thumbnailService.getThumbnail();
return this.response.end(thumbnail, "binary");
} catch (e) {
let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e));
this.apiLogger.logger.error(errorStr);

this.response.writeHead(500, {
"Content-Type": "application/dicom+json"
});
return this.response.end({
code: 500,
message: "An exception occurred"
});
let apiErrorArrayHandler = new ApiErrorArrayHandler(this.response, this.apiLogger, e);
return apiErrorArrayHandler.doErrorResponse();
}
}
}
Expand Down

0 comments on commit d99f68a

Please sign in to comment.