Skip to content

Commit

Permalink
fix: response 404 when not found
Browse files Browse the repository at this point in the history
wado.js

fix: Remove surplus  ng-click

refactor: Add some region comment

refactor: Change some code

The boundary in body is same added -- , so just use it
change findEndindex line to added all endindex after

refactor: Added some comment

refactor: Delete unuse code in put
  • Loading branch information
Chinlinlee committed Dec 14, 2020
1 parent 7ba20fd commit f5e205d
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 159 deletions.
6 changes: 4 additions & 2 deletions api/Api_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ module.exports.textSpaceToOrCond = async function (str) {
return resolve(undefined);
});
}

//#region loging auth api
module.exports.isLogin = async function (req, res, next) {
console.log(req.isAuthenticated() + " " + req.user + " Is LoggedIn");
let isNormalLogin = req.isAuthenticated();
let isTokenLogin =await exports.isTokenLogin(req);
let isTokenLogin = await exports.isTokenLogin(req);
let isAuthenticated = (isNormalLogin || isTokenLogin);
if (isAuthenticated) {
return next();
Expand Down Expand Up @@ -104,6 +104,8 @@ module.exports.isTokenLogin = async function (req, res, next) {
})
})
}
//#endregion

//#region moment compare function
module.exports.momentDateFunc = {
">": (value, date) => {
Expand Down
1 change: 0 additions & 1 deletion api/FHIR/organization/controller/getOrganization.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module.exports = async function (req, res) {
delete queryParameter["$and"];
}
try {
//let docs = await mongoFunc.findFilterFields('patients', queryParameter, FHIRFilter, paginationLimit, paginationSkip);
let docs = await mongodb.organization.find(queryParameter ,FHIRFilter).
limit(realLimit).
skip(paginationSkip).
Expand Down
41 changes: 1 addition & 40 deletions api/FHIR/organization/controller/putOrganization.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
const { VERSIONS } = require('@asymmetrik/node-fhir-server-core').constants;
const { resolveSchema } = require('@asymmetrik/node-fhir-server-core');

const mongodb = require('models/mongodb');
const _ = require("lodash");
const base_version ="4_0_0";
const mongoose = require("mongoose");
const { handleError } = require('../../../../models/FHIR/httpMessage');

let getOrganization = base_version => {
return require(resolveSchema(base_version, 'Organization'));
};

const errorMessage = {
code : "" ,
Expand Down Expand Up @@ -39,38 +32,6 @@ module.exports = async function (req ,res) {
return resFunc[status](result);
}

async function updateOrganization (req) {
return new Promise (async (resolve , reject) => {
const id = req.params.id;
mongodb["organization"].findOne ({id : id} , function (err ,doc) {
if (err) {
errorMessage.message = err;
return resolve (["false" , err]);
}
let organizationClass = getOrganization(base_version);
let organization = new organizationClass(req.body);

let cloneOrganization = _.cloneDeep(organization);
let updateDoc = Object.assign(cloneOrganization , {_id :new mongoose.Types.ObjectId});
if (doc) {
delete updateDoc._id;
}
mongodb["organization"].findOneAndUpdate({id : id } ,{$set : updateDoc} , {upsert : true , new : true , rawResult: true} , function (err , newDoc) {
if (err) {
errorMessage.code = 500;
errorMessage.message = err;
return resolve (["false" , err]);
}
return resolve(["true", {
id: id,
doc: newDoc.getFHIRField() ,
code : (newDoc.lastErrorObject.updatedExisting)? 200 : 201
}]);
});
});
});
}

function isDocExist (id) {
return new Promise (async (resolve , reject) => {
mongodb["organization"].findOne ({id : id} , async function (err ,doc) {
Expand Down
47 changes: 5 additions & 42 deletions api/FHIR/patient/controller/putPatient.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
const { VERSIONS } = require('@asymmetrik/node-fhir-server-core').constants;
const { resolveSchema } = require('@asymmetrik/node-fhir-server-core');
const mongodb = require('models/mongodb');
const _ = require("lodash");
const base_version ="4_0_0";
const mongoose = require("mongoose");
let getPatient = base_version => {
return require(resolveSchema(base_version, 'Patient'));
};
const { handleError } = require('../../../../models/FHIR/httpMessage');


const errorMessage = {
message : ""
}
Expand All @@ -20,11 +12,14 @@ module.exports = async function (req ,res) {
return res.status(data.code).send(data.doc);
} ,
"false" : (error) => {
return res.status(500).send(errorMessage);
return res.status(500).send(handleError.exception(errorMessage.message));
}
}
//let [updateStatus , doc] = await updatePatient(req);
let dataExist = await isDocExist(req.params.id);
if (dataExist == 0) {
return res.status(500).json(handleError.exception(errorMessage.message));
}
let dataFuncAfterCheckExist = {
0 : () => {
return ["false" , ""];
Expand All @@ -36,38 +31,6 @@ module.exports = async function (req ,res) {
return resFunc[status](result);
}

async function updatePatient (req) {
return new Promise (async (resolve , reject) => {
const id = req.params.id;
mongodb["patients"].findOne ({id : id} , function (err ,doc) {
if (err) {
errorMessage.message = err;
return resolve (["false" , err]);
}
let patientClass = getPatient(base_version);
let patient = new patientClass(req.body);

let clonePatient = _.cloneDeep(patient);
let updateDoc = Object.assign(clonePatient , {_id :new mongoose.Types.ObjectId});
if (doc) {
delete updateDoc._id;
}
mongodb["patients"].findOneAndUpdate({id : id } ,{$set : updateDoc} , {upsert : true , new : true , rawResult: true} , function (err , newDoc) {
if (err) {
errorMessage.message = err;
return resolve (["false" , err]);
}
return resolve(["true", {
id: id,
doc: newDoc.value.getFHIRField() ,
code : (newDoc.lastErrorObject.updatedExisting)? 200 : 201
}]);
});
});
});
}


function isDocExist (id) {
return new Promise (async (resolve , reject) => {
mongodb["patients"].findOne ({id : id} , async function (err ,doc) {
Expand Down
83 changes: 17 additions & 66 deletions api/dicom-web/stow/controller/postSTOW.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,21 @@ function bufferToStream(buffer) {
return readstream;
}
async function stowMultipartRelated(req) {
req.body= req.body.toString('binary');
let multipartMessage = req.body;
//req.body= req.body.toString('binary');
let multipartMessage = req.body.toString('binary');
//let boundary = req.headers["content-type"].split("boundary=")[1];
let boundary = req.body.split("\r\n")[0].substring(2);
let startBoundary = `--${boundary}`;
let endBoundary = `--${boundary}--`;
let matches = req.body.matchAll(new RegExp(startBoundary , "gi"));
let startBoundary = multipartMessage.split("\r\n")[0];
//let startBoundary = `--${boundary}`;
let matches = multipartMessage.matchAll(new RegExp(startBoundary , "gi"));
let fileEndIndex = [];
let fileStartIndex = [];
for (let match of matches) {
fileEndIndex.push(match.index-2);
}
let regex = new RegExp(`Content-Disposition|Content-Type`);
fileEndIndex = fileEndIndex.slice(1);
let data = multipartMessage.split("\r\n");
let fileLength = 0;
let file = {
buffer : [] ,
text : ""
}
let filename = [];
let files = [];
/*for (let i in data) {
let text = data[i];
if ((text == startBoundary || text== endBoundary) && file.text) {
console.log(Buffer.from(file.text , 'binary'));
files.push(bufferToStream(Buffer.from(file.text , 'binary')));
file = {
buffer : [] ,
text : "" ,
contentType : "" ,
contentDisposition : ""
}
} else if (text.includes("Content-Disposition")) {
let textSplitFileName = text.split("filename=")
filename.push(textSplitFileName[textSplitFileName.length-1].replace(/"/gm , ""));
} else if (text && !text.match(regex) && text != endBoundary && text != startBoundary) {
file.text += text;
}
}*/
let contentDispositionList = [];
let contentTypeList = [];
for (let i in data) {
Expand All @@ -129,68 +105,37 @@ async function stowMultipartRelated(req) {
contentDispositionList = _.uniq(contentDispositionList);
contentTypeList = _.uniq(contentTypeList);
for (let type of contentTypeList) {
let contentTypeMatches = req.body.matchAll(new RegExp(type , "gi")); for (let match of contentTypeMatches) {
fileStartIndex.push(match.index +match['0'].length + 4);
let contentTypeMatches = multipartMessage.matchAll(new RegExp(type , "gi"));
for (let match of contentTypeMatches) {
fileStartIndex.push(match.index +match['0'].length + 4); //+4 because have \r\n
}
}
fileEndIndex = fileEndIndex.slice(1);
for (let i in fileEndIndex) {
let fileData = req.body.substring(fileStartIndex[i] , fileEndIndex[i]);
let fileData = multipartMessage.substring(fileStartIndex[i] , fileEndIndex[i]);
files.push(bufferToStream(Buffer.from(fileData , 'binary')));
}
console.log("Upload Files complete");
return {files : files , filename : filename};
}

async function dicom2mongodb(data) {
//let test = await FHIR_Imagingstudy_model.DCM2FHIR(dirname);
return new Promise(async (resolve)=>
{
let result = await require('../../../FHIR/ImagingStudy/controller/putImagingStudy').putWithoutReq(data.id , data);
if (result) return resolve(true);
return resolve(false);

let options =
{
method: "PUT",
url: `http://${process.env.FHIRSERVER_HOST}:${process.env.SERVER_PORT}/api/fhir/ImagingStudy/${data.id}`,
json: true,
body: data
}
request(options, function (err, response, body) {
if (err) {
return reject(new Error(err));
}
resolve(true);
});
});
}

async function dicom2FHIR(data) {
//let test = await FHIR_Imagingstudy_model.DCM2FHIR(dirname);
return new Promise(async (resolve , reject)=>
{
let resData =await require('../../../FHIR/ImagingStudy/controller/post_convertFHIR').getData( data.id , data);
return resolve(resData);
let options =
{
method: "POST",
url: `http://${process.env.FHIRSERVER_HOST}:${process.env.SERVER_PORT}/api/fhir/ImagingStudy/convertFHIR/${data.id}`,
json: true ,
body: data
}

request(options, function (err, response, body) {
if (err) {
return reject(new Error(err));
}
return resolve(body);
});
});
}

async function dicomEndpoint2MongoDB(data) {
//let test = await FHIR_Imagingstudy_model.DCM2FHIR(dirname);
return new Promise((resolve , reject)=>
{
let options =
Expand Down Expand Up @@ -239,11 +184,17 @@ async function saveDicom (filestream , filename) {
}
return Buffer.concat(buffers);
});
let fhirData = await FHIR_Imagingstudy_model.DCM2FHIR(temp_buffer);
let fhirData = await FHIR_Imagingstudy_model.DCM2FHIR(temp_buffer).catch((err) => {
console.error(err);
return resolve(false);
});
if (!fhirData) {
return resolve(false);
}
// let fhirData = fhirDataList[0];
if (!fhirData.started) {
return resolve(false);
}
let started_date = new Date(fhirData.started).toISOString();
let started_date_split = started_date.split('-');
let year = started_date_split[0];
Expand Down
6 changes: 2 additions & 4 deletions api/dicom/controller/qido.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ const api_func = require("../../Api_function.js");
const {mongoDateQuery} = require("../../../models/mongodb/func");
const mongoFunc = require("../../../models/mongodb/func");
const {ToRegex} = require("../../Api_function");
const {textSpaceToOrCond} = require("../../Api_function");
const _ = require('lodash');
const mongodb = require("../../../models/mongodb");

module.exports = async function (req ,res)
{
Expand Down Expand Up @@ -105,8 +103,8 @@ async function getCount (image_Query) {
$count : "count"
}
];
let count = await mongodb.ImagingStudy.find(image_Query).count();
return resolve(count);
let count = await mongoFunc.aggregate_Func("ImagingStudy" , aggregate_Query);
return resolve(count.length ? count[0].count : 0);
});
}

Expand Down
10 changes: 7 additions & 3 deletions api/dicom/controller/wado.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ const api_func = require('../../Api_function.js');
const mongodb = require('models/mongodb');
const fs = require('fs');
const path = require('path');
const fileFunc = require('../../../models/file/file_Func');
const { exec } = require('child_process');
const PythonShell = require('python-shell').PythonShell;
let condaPath = "H:\\ProgramData\\Anaconda3\\Scripts\\conda.exe";
let condaEnvName = "gdcm";
let theError = {};

module.exports = async(req, res) =>
{
try {
Expand All @@ -25,6 +23,11 @@ module.exports = async(req, res) =>
return;
}
let store_Path = `${disk}${ori_Path}`;
if (!fs.existsSync(store_Path)) {
res.setHeader('Content-Type' , 'text/*');
sendNotFoundMessage(req , res);
return;
}
if (param.contentType == 'image/jpeg') {
let jpgFile = store_Path.replace('.dcm' , '.jpg');
//let isExist =await fileFunc.checkExist(jpgFile);
Expand Down Expand Up @@ -120,6 +123,7 @@ async function get_Instance_StorePath(i_Param)
} catch (e) {
console.log("error\r\n"+ JSON.stringify(aggregate_Query , null ,4));
//console.log(aggregate_Query);
return false;
}

}
Expand Down
2 changes: 1 addition & 1 deletion public/html/ImageMS.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ <h2 ng-show="dataList.length > 0">Study</h2>
<a class="btn btn-secondary btn-block-sm" href="#" ng-click="openFHIRJson(item)">
FHIR JSON
</a>
<a class="btn btn-secondary btn-block-sm" ng-href="/html/BL/bluelight/html/start.html?StudyInstanceUID={{item.dicomJson['0020000D'].Value[0]}}" ng-click="openFHIRJson(item)">
<a class="btn btn-secondary btn-block-sm" ng-href="/html/BL/bluelight/html/start.html?StudyInstanceUID={{item.dicomJson['0020000D'].Value[0]}}" >
View in BlueLight DICOM Viwer
</a>
</td>
Expand Down

0 comments on commit f5e205d

Please sign in to comment.