From 074aa9739cd9df4c56706a2f6e352af51c3a8fa4 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sat, 8 Apr 2017 20:20:32 -0400 Subject: [PATCH] Cleanup, better error handling --- index.js | 52 +++++++++++++++++++++++++++-------------------- public/js/main.js | 33 +++++++++++++++--------------- 2 files changed, 46 insertions(+), 39 deletions(-) diff --git a/index.js b/index.js index 0b88d5c..b737b57 100644 --- a/index.js +++ b/index.js @@ -64,8 +64,6 @@ app.use(bodyParser.json()); app.use(express.static('public')); app.use('/swagger.json', express.static('docs/swagger.json')); - - let upload = multer({ storage: multer.diskStorage({ destination: (req, file, cb) => { @@ -157,11 +155,7 @@ app.post('/task/new', addRequestId, upload.array('images'), (req, res) => { }); }, - cb => { fs.mkdir(srcPath, undefined, cb); }, - cb => { fs.mkdir(destPath, undefined, cb); }, - cb => { fs.mkdir(destGpcPath, undefined, cb); }, - - // Move all uploads to data//images dir + // Move all uploads to data//images dir (if any) cb => { if (req.files && req.files.length > 0) { fs.stat(destPath, (err, stat) => { @@ -172,25 +166,33 @@ app.post('/task/new', addRequestId, upload.array('images'), (req, res) => { cb(); } }, - cb => fs.rename(srcPath, destImagesPath, cb), + + // Unzips zip URL to data// (if any) cb => { if (req.body.zipurl) { - let archiveDestPath = path.join(destPath, "zipurl.zip"); + let archive = "zipurl.zip"; - download(req.body.zipurl, archiveDestPath, err => { + upload.storage.getDestination(req, archive, (err, dstPath) => { if (err) cb(err); else{ - // unzip and flatten the zip file (in case there are folders in the zip) - fs.createReadStream(archiveDestPath).pipe(unzip.Parse()) - .on('entry', function(entry) { - if (entry.type === 'File') { - entry.pipe(fs.createWriteStream(destImagesPath + '/' + path.basename(entry.path))); - } else { - entry.autodrain(); - } - }) - .on('close', cb) - .on('error', cb); + let archiveDestPath = path.join(dstPath, archive); + + download(req.body.zipurl, archiveDestPath, err => { + if (err) cb(err); + else{ + // unzip and flatten the zip file (in case there are folders in the zip) + fs.createReadStream(archiveDestPath).pipe(unzip.Parse()) + .on('entry', function(entry) { + if (entry.type === 'File') { + entry.pipe(fs.createWriteStream(path.join(srcPath, path.basename(entry.path)))); + } else { + entry.autodrain(); + } + }) + .on('close', cb) + .on('error', cb); + } + }); } }); } else { @@ -198,15 +200,21 @@ app.post('/task/new', addRequestId, upload.array('images'), (req, res) => { } }, - + cb => fs.mkdir(destPath, undefined, cb), + cb => fs.mkdir(destGpcPath, undefined, cb), + cb => fs.rename(srcPath, destImagesPath, cb), + cb => { // Find any *.txt (GPC) file and move it to the data//gpc directory + // also remove any lingering zipurl.zip fs.readdir(destImagesPath, (err, entries) => { if (err) cb(err); else { async.eachSeries(entries, (entry, cb) => { if (/\.txt$/gi.test(entry)) { fs.rename(path.join(destImagesPath, entry), path.join(destGpcPath, entry), cb); + }else if (entry === 'zipurl.zip'){ + fs.unlink(path.join(destImagesPath, entry), cb); } else cb(); }, cb); } diff --git a/public/js/main.js b/public/js/main.js index 85b5d09..81a390c 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -292,27 +292,26 @@ $(function() { $("#images").fileinput('upload'); }); -// zip file control -$('#btnShowImport').on('click', function(e){ - e.preventDefault(); - $('#zipFileInput').removeClass('hidden'); - $('#btnShowUpload').removeClass('hidden'); + // zip file control + $('#btnShowImport').on('click', function(e){ + e.preventDefault(); + $('#zipFileInput').removeClass('hidden'); + $('#btnShowUpload').removeClass('hidden'); - $('#imagesInput').addClass('hidden'); - $('#btnShowImport').addClass('hidden'); + $('#imagesInput').addClass('hidden'); + $('#btnShowImport').addClass('hidden'); -}); + }); -$('#btnShowUpload').on('click', function(e){ - e.preventDefault(); - $('#imagesInput').removeClass('hidden'); - $('#btnShowImport').removeClass('hidden'); - - $('#zipFileInput').addClass('hidden'); - $('#btnShowUpload').addClass('hidden'); - -}) + $('#btnShowUpload').on('click', function(e){ + e.preventDefault(); + $('#imagesInput').removeClass('hidden'); + $('#btnShowImport').removeClass('hidden'); + $('#zipFileInput').addClass('hidden'); + $('#btnShowUpload').addClass('hidden'); + $('#zipurl').val(''); + }); var btnUploadLabel = $("#btnUpload").val(); $("#images")