-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
135 lines (119 loc) · 3.78 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
module.exports = function(xnat){
const fs = require('fs');
const Promise = require('bluebird');
const path = require('path');
const argv = require('minimist')(process.argv.slice(2));
const _ = require('underscore');
const os = require('os');
const help = function(){
console.error("Upload a DICOM directory to XNAT.");
console.error("Usage: node", process.argv[1],"-d <directory> -p <project id>");
console.error("Options:");
console.error("-d <DICOM directory>, directory with DICOM files.");
console.error("-p <project id>, project id in XNAT to upload the files.");
console.error("--pid <patient id>, If patient id is specified, the patient id won't be extracted from the DICOM file.");
console.error("--sessid <session id>, If session id is specified, the session id won't be extracted from the DICOM file.");
console.error("--server <server url>, XNAT server url");
console.error("--prompt , If set, forces prompt for login information again. It will use the previous server URL saved in the configuration file");
console.error("--noext , Find all files regardless of extension and test if it is a DICOM file. The DICOM files are imported to XNAT.");
}
if(!argv["d"] || !argv["p"] || argv["h"] || argv["help"]){
help();
process.exit(1);
}
var projectid = argv["p"];
var directory = argv["d"];
var patientid = argv["pid"];
var sessionid = argv["sessid"];
var promptlogin = argv["prompt"];
var noext = argv["noext"];
const getConfigFile = function () {
return new Promise(function(resolve, reject){
try {
// Try to load the user's personal configuration file
var conf = path.join(os.homedir(), '.xnat.json');
resolve(require(conf));
} catch (e) {
reject(e);
}
});
};
if(noext){
xnat.useDCMExtensionOff();
}
var loginprom = undefined;
if(argv["server"]){
var conf = {};
conf.server = argv["server"];
loginprom = xnat.promptUsernamePassword()
.then(function(user){
conf.user = user;
xnat.writeConfFile(conf);
return conf;
});
}else{
loginprom = getConfigFile()
.then(function(conf){
if(promptlogin){
return xnat.promptUsernamePassword()
.then(function(user){
conf.user = user;
xnat.writeConfFile(conf);
return conf;
});
}else{
return conf;
}
})
.catch(function(e){
throw "Config file not found. Use -h or --help to learn how to use this program";
});
}
loginprom
.then(function(conf){
console.log("Setting server url to ", conf.server);
xnat.setXnatUrl(conf.server);
console.log("Login to xnat.", conf.server);
return xnat.login(conf.user);
})
.then(function(){
console.log("Find dicom files in directory:", directory);
return xnat.findFiles(directory);
})
.then(function(files){
return Promise.map(files, function(file){
return xnat.dicomDump(file)
.then(function(dcmData){
console.log("Importing file", file);
var pid;
if(patientid){
pid = patientid;
}else{
pid = dcmData.dataset["00100020"].value;
pid = pid.replace(/[ .:|&;$%@"<>()+,]/g, "_");
}
var sessid;
if(sessionid){
sessid = sessionid;
}else{
//Create a unique session id. With the old one it might conflict with existing sessions.
sessid = pid + "-" + dcmData.dataset["00080020"].value;
}
return xnat.uploadImage(projectid, pid, sessid, file);
})
.catch(function(err){
console.error(err);
console.error("No problem trying to continue...");
});
}, {
concurrency: 1
});
})
.then(function(uploaded){
return xnat.logout();
})
.catch(function(error){
console.error(error);
return xnat.logout();
});
}