From fbca4187be1f0a415a902ae572defa0f6ff847d8 Mon Sep 17 00:00:00 2001 From: Lee Pepper Date: Tue, 11 Apr 2017 21:55:52 -0600 Subject: [PATCH] Feature: Allow Zip File uploads Feature: Optional webhook callback option --- index.js | 53 +++++++++++++++++++++++++++++++++++---------- libs/Task.js | 4 +++- libs/TaskManager.js | 17 +++++++++++++++ public/index.html | 15 +++++++++++-- public/js/main.js | 10 ++++++++- 5 files changed, 83 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index b737b57..f102cce 100644 --- a/index.js +++ b/index.js @@ -181,16 +181,17 @@ app.post('/task/new', addRequestId, upload.array('images'), (req, res) => { 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); + // 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); + cb(); } }); } @@ -204,6 +205,34 @@ app.post('/task/new', addRequestId, upload.array('images'), (req, res) => { cb => fs.mkdir(destGpcPath, undefined, cb), cb => fs.rename(srcPath, destImagesPath, cb), + + cb => { + // Find any *.zip file and extract + fs.readdir(destImagesPath, (err, entries) => { + if (err) cb(err); + else { + async.eachSeries(entries, (entry, cb) => { + if (/\.zip$/gi.test(entry)) { + + + fs.createReadStream(path.join(destImagesPath, entry)).pipe(unzip.Parse()) + .on('entry', function(entry) { + if (entry.type === 'File') { + entry.pipe(fs.createWriteStream(path.join(destImagesPath, path.basename(entry.path)))); + } else { + entry.autodrain(); + } + }) + .on('close', cb) + .on('error', cb); + + + } else cb(); + }, cb); + } + }); + }, + cb => { // Find any *.txt (GPC) file and move it to the data//gpc directory // also remove any lingering zipurl.zip @@ -213,7 +242,7 @@ app.post('/task/new', addRequestId, upload.array('images'), (req, res) => { 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'){ + }else if (/\.zip$/gi.test(entry)){ fs.unlink(path.join(destImagesPath, entry), cb); } else cb(); }, cb); @@ -230,7 +259,7 @@ app.post('/task/new', addRequestId, upload.array('images'), (req, res) => { res.json({ uuid: req.id }); cb(); } - }, req.body.options); + }, req.body.options, req.body.webhook); } ], err => { if (err) res.json({ error: err.message }); diff --git a/libs/Task.js b/libs/Task.js index 1f47d02..89bf007 100644 --- a/libs/Task.js +++ b/libs/Task.js @@ -34,7 +34,7 @@ let Directories = require('./Directories'); let statusCodes = require('./statusCodes'); module.exports = class Task{ - constructor(uuid, name, done, options = []){ + constructor(uuid, name, done, options = [], webhook){ assert(uuid !== undefined, "uuid must be set"); assert(done !== undefined, "ready must be set"); @@ -47,6 +47,8 @@ module.exports = class Task{ this.gpcFiles = []; this.output = []; this.runningProcesses = []; + this.webhook = webhook; + async.series([ // Read images info diff --git a/libs/TaskManager.js b/libs/TaskManager.js index 8231b3d..03f432c 100644 --- a/libs/TaskManager.js +++ b/libs/TaskManager.js @@ -27,6 +27,10 @@ let statusCodes = require('./statusCodes'); let async = require('async'); let schedule = require('node-schedule'); let Directories = require('./Directories'); +let request = require('request'); + + + const TASKS_DUMP_FILE = path.join(Directories.data, "tasks.json"); const CLEANUP_TASKS_IF_OLDER_THAN = 1000 * 60 * 60 * 24 * config.cleanupTasksAfter; // days @@ -149,6 +153,19 @@ module.exports = class TaskManager{ if (task){ this.addToRunningQueue(task); task.start(() => { + if(task.webhook && task.webhook.length > 3){ + request({ + uri: task.webhook, + method: 'POST', + json: task.getInfo() + }, + function (error, response, body) { + if (!error && response.statusCode == 200) { + + } + }); + } + this.removeFromRunningQueue(task); this.processNextTask(); }); diff --git a/public/index.html b/public/index.html index 056961d..85c1f13 100644 --- a/public/index.html +++ b/public/index.html @@ -46,11 +46,11 @@