-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add export dicom dir 7z plugin
- Use GET `/dicom-web/dicomdir?{attr=value}` to export dicom dir 7z file
- Loading branch information
1 parent
ab04e91
commit f3d4ecb
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const { DicomWebServiceError, DicomWebStatusCodes } = require("@error/dicom-web-service"); | ||
const { DicomDir } = require("@models/DICOM/dcm4che/dicomdir"); | ||
const { InstanceModel } = require("@models/mongodb/models/instance.model"); | ||
const { BaseQueryService } = require("@root/api/dicom-web/service/base-query.service"); | ||
const { raccoonConfig } = require("@root/config-class"); | ||
const { SevenZip } = require("@root/utils/sevenZip"); | ||
const path = require("path"); | ||
const shortHash = require("shorthash2"); | ||
const { v4: uuidV4 } = require("uuid"); | ||
|
||
class GetDicomDirService extends BaseQueryService { | ||
constructor(req, res) { | ||
super(req, res); | ||
} | ||
|
||
async getDicomDirSevenZip() { | ||
let instanceFiles = await this.#getInstanceFiles(); | ||
|
||
if (instanceFiles.length === 0) throw new DicomWebServiceError(DicomWebStatusCodes.ProcessingFailure, "No any instance found", 404); | ||
|
||
let dicomDirFilename = path.join(raccoonConfig.dicomWebConfig.storeRootPath, "DICOMDIR" + shortHash(uuidV4())); | ||
|
||
let relativePaths = [ | ||
path.relative(raccoonConfig.dicomWebConfig.storeRootPath, dicomDirFilename) | ||
]; | ||
for(let file of instanceFiles) { | ||
let relativePath = path.relative(raccoonConfig.dicomWebConfig.storeRootPath, file.instancePath); | ||
relativePaths.push(relativePath); | ||
} | ||
|
||
let dicomDirInstance = new DicomDir(dicomDirFilename, ...instanceFiles.map(v=> v.instancePath)); | ||
await dicomDirInstance.exec(); | ||
|
||
await this.compressTo7zip(`${dicomDirFilename}.7z`, relativePaths); | ||
return `${dicomDirFilename}.7z`; | ||
} | ||
|
||
async #getInstanceFiles() { | ||
return await InstanceModel.getInstancePathsByQueryOpts({ | ||
query: this.query, | ||
requestParams: this.request.params | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} zipFile | ||
* @param {string[]} relativePaths | ||
*/ | ||
async compressTo7zip(zipFile, relativePaths) { | ||
let sevenZip = new SevenZip("7z", undefined, zipFile); | ||
|
||
for(let i = 0 ; i < relativePaths.length ; i++) { | ||
sevenZip.addCmd(relativePaths[i]); | ||
} | ||
|
||
let cwd = path.normalize(raccoonConfig.dicomWebConfig.storeRootPath); | ||
await sevenZip.pack({ | ||
cwd: cwd | ||
}); | ||
} | ||
} | ||
|
||
module.exports.GetDicomDirService = GetDicomDirService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { app } = require("@root/app"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { GetDicomDirService } = require("./getDicomDirService"); | ||
|
||
let pluginConfigFile = path.join(__dirname, "../config.template.js"); | ||
if (fs.existsSync(path.join(__dirname, "../config.js"))) pluginConfigFile = path.join(__dirname, "../config.js"); | ||
|
||
const { pluginsConfig } = require(pluginConfigFile); | ||
|
||
app.get("/dicom-web/dicomdir", async (req, res) => { | ||
let getDicomDirService = new GetDicomDirService(req, res); | ||
|
||
let dicomDirSevenZipFilename = await getDicomDirService.getDicomDirSevenZip(); | ||
return fs.createReadStream(dicomDirSevenZipFilename).pipe(res); | ||
}); | ||
|
||
module.exports = []; |