Skip to content

Instantly share code, notes, and snippets.

@bdunnette
Last active December 28, 2015 03:49
Show Gist options
  • Save bdunnette/7438144 to your computer and use it in GitHub Desktop.
Save bdunnette/7438144 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var path = require('path');
var http = require('http');
var querystring = require('querystring');
var args = process.argv.splice(2);
if (args.length >= 2) {
var sourceDir = args[0];
var caseNum = args[1];
var requestor = args[2]||'';
var now = new Date();
var destDir = path.join(process.cwd().toString(), now.getFullYear().toString(), caseNum.toString());
var logFile = path.join(process.cwd().toString(), 'file-upload.log');
var data = querystring.stringify({
specimenID: caseNum,
modality: 'EM',
requestor: requestor,
link: '\\\\labmed.ahc.umn.edu\\labmed\\LabMed\\Path\\Images\\EM\\' + now.getFullYear().toString() + '\\' + caseNum
});
var options = {
host: '160.94.51.184',
port: 1337,
path: '/imagingrequest',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};
console.log(destDir);
try {
newDir = fs.mkdirSync(destDir);
} catch(err) {
console.log(err);
};
dirs = fs.readdirSync(sourceDir)
for (dir in dirs) {
sourceDir2 = path.join(sourceDir, dirs[dir])
files = fs.readdirSync(sourceDir2)
for (file in files) {
try {
var fileName = files[file];
if (fileName != 'Thumbs.db') {
filePath = path.join(sourceDir2, files[file]);
outFilePath = path.join(destDir, files[file]);
console.log(filePath + ' -> ' + outFilePath);
var fileDate = new Date();
var fileContents = fs.readFileSync(filePath);
fs.writeFile(outFilePath, fileContents, function(err) {
if (err) throw err;
fs.appendFile(logFile, fileDate.toString() + ': ' + outFilePath + '\r\n');
});
};
}
catch(err) {
console.log(err);
};
};
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("body: " + chunk);
});
});
req.write(data);
req.end();
} else {
console.log("Please run the script in the following format: node upload-files.js <path to images> <case number>");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment