Skip to content

Commit

Permalink
feat: use commander, write result to file
Browse files Browse the repository at this point in the history
- Use command instead of argv
- Write result to `local-upload-wado-log.json`
- Add commander npm package
  • Loading branch information
Chinlinlee committed Mar 25, 2023
1 parent 6196cd0 commit fb6bb1a
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 112 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ tempScript
/plugins/config.js
/config/log4js.json

/local/local-upload-log.json
/local/local-upload-log.json
/local/local-upload-log-wado.json
33 changes: 24 additions & 9 deletions local/DICOMUploader-wado.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require("path");
process.chdir(path.join(__dirname,"../"));
process.chdir(path.join(__dirname, "../"));
require("rootpath")();
require("dotenv").config();
const fs = require("fs");
Expand All @@ -10,6 +10,7 @@ const request = require('request-compose').extend({
multipart: require('request-multipart')
}
}).client;
const { program } = require("commander");

let osPlatform = os.platform().toLocaleLowerCase();
if (osPlatform.includes("linux")) {
Expand All @@ -18,12 +19,15 @@ if (osPlatform.includes("linux")) {
process.env.ENV = "windows";
}

let filePath = process.argv[2];
const STOW_URL = "http://127.0.0.1:8081/dicom-web/studies";
program.requiredOption("-d, --dir <string>", "The directory path contains DICOM files that need to upload")
.requiredOption("-u, --url <string>", "STOW-RS URL");
program.parse();

const options = program.opts();

async function storeInstance(filename, stowUrl) {
let stream = fs.createReadStream(filename);

let response = await request({
method: "POST",
url: stowUrl,
Expand All @@ -42,25 +46,36 @@ async function storeInstance(filename, stowUrl) {
return response;
}
async function main() {
console.log(filePath);
let inputDir = options.dir;
const STOW_URL = options.url;
console.log(`Input Directory: ${inputDir}`);

let successFiles = [];
glob("**/*.dcm", { cwd: filePath }, async function (err, matches) {
let errorFiles = [];
glob("**/*.dcm", { cwd: inputDir }, async function (err, matches) {
for (let file of matches) {
let fullFilename = path.join(filePath, file);
let fullFilename = path.join(inputDir, file);
try {
let response = await storeInstance(fullFilename, STOW_URL);
let statusCode = response.res.statusCode;
if (statusCode === 200) {
console.log("success: " + fullFilename);
successFiles.push(fullFilename);
} else {
console.error("error: " + response.body.result);
fs.appendFile("upload-error.txt", `error file: ${fullFilename}\r\n`);
errorFiles.push(fullFilename);
}

} catch (e) {
console.error(e);
}
}

fs.writeFileSync(path.join(__dirname, "local-upload-wado-log.json"), JSON.stringify({
successFiles,
errorFiles
}, null, 4));

});
}
(async () => {
Expand Down
20 changes: 14 additions & 6 deletions local/DICOMUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ require("rootpath")();
require("dotenv").config();
const fs = require("fs");
const glob = require("glob");
const { stow } = require("./api/dicom-web/stow/service/stow");
const { stow } = require("../api/dicom-web/stow/service/stow");
const os = require("os");
const { program } = require("commander");

let osPlatform = os.platform().toLocaleLowerCase();
if (osPlatform.includes("linux")) {
Expand All @@ -14,14 +15,21 @@ if (osPlatform.includes("linux")) {
process.env.ENV = "windows";
}

let filePath = process.argv[2];
program.requiredOption("-d, --dir <string>", "The directory path contains DICOM files that need to upload")
.requiredOption("-u, --url <string>", "STOW-RS URL");
program.parse();

const options = program.opts();

function main() {
console.log(filePath);
let inputDir = options.dir;
console.log(`Input Directory: ${inputDir}`);

let successFiles = [];
let errorFiles = [];
glob("**/*.dcm", { cwd: filePath }, async function (err, matches) {
glob("**/*.dcm", { cwd: inputDir }, async function (err, matches) {
for (let file of matches) {
let fullFilename = path.join(filePath, file);
let fullFilename = path.join(inputDir, file);
let storeInstanceResult = await stow(
{
headers: {
Expand All @@ -38,7 +46,7 @@ function main() {
}
}

fs.writeFileSync("local-upload-log.json", JSON.stringify({
fs.writeFileSync(path.join(__dirname, "local-upload-log.json"), JSON.stringify({
successFiles,
errorFiles
}, null, 4));
Expand Down
Loading

0 comments on commit fb6bb1a

Please sign in to comment.