From 8a891ce5814a8395ff46867022d6c1230318f00b Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Mon, 7 Oct 2013 18:13:20 +0300 Subject: [PATCH 01/38] readme cleanup --- README.md | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/README.md b/README.md index 7110a2d..0aaac0a 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,6 @@ jquery-file-upload-middleware ============================= -### Personal Patch Repository - -Please comeback [Aleksandr Guidrevitch](http://aguidrevitch.blogspot.com/) lol - -- use bower to install jquery-file-upload (planed) -- use not npm to install jquery-file-upload-middleware -- use this repository by - -```json -{ - "name": "your project", - "version": "0.1.0", - "private": true, - "dependencies": { - "express": "3.3.x", - "jquery-file-upload-middleware": "git://github.com/soomtong/jquery-file-upload-middleware.git", - "swig": "1.0.x", - "imagemagick": "0.1.x" - }, - "devDependencies": { - "nodeunit":"*" - } -} -``` - - - - - ---- - -## Readme will update later - jQuery-File-Upload Express.js middleware. Based on the server code of [jQuery-File-Upload](https://github.com/blueimp/jQuery-File-Upload) Installation: From 94779c6a1dc3d1a3e320739b8118f599083a4a68 Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Mon, 7 Oct 2013 18:13:48 +0300 Subject: [PATCH 02/38] version bump --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 722e4e8..ea657a4 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "express", "middleware" ], - "version": "0.0.9", + "version": "0.1.0", "dependencies": { "formidable": ">=1.0.11", "imagemagick": ">=0.1.2", @@ -28,6 +28,6 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "_id": "jquery-file-upload-middleware@0.0.9", + "_id": "jquery-file-upload-middleware@0.1.0", "license": "MIT" } From eb6e518465c5e2a6e3689618938a65a5e672122b Mon Sep 17 00:00:00 2001 From: Guntur Sarwohadi Date: Sat, 12 Oct 2013 09:22:53 +0700 Subject: [PATCH 03/38] Collect formData on 'field' event from form property in UploadHandler.prototype.post method to req.fields object (created new object if undefined). Extend fileInfo with req.fields on 'end' event. --- lib/uploadhandler.js | 360 ++++++++++++++++++++++--------------------- 1 file changed, 182 insertions(+), 178 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index ece4b55..05f60ba 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -1,184 +1,188 @@ var EventEmitter = require('events').EventEmitter, - path = require('path'), - fs = require('fs'), - formidable = require('formidable'), - imageMagick = require('imagemagick'), - mkdirp = require('mkdirp'), - _ = require('lodash'); + path = require('path'), + fs = require('fs'), + formidable = require('formidable'), + imageMagick = require('imagemagick'), + mkdirp = require('mkdirp'), + _ = require('lodash'); module.exports = function (options) { - var FileInfo = require('./fileinfo')( - _.extend({ - baseDir: options.uploadDir - }, _.pick(options, 'minFileSize', 'maxFileSize', 'acceptFileTypes')) - ); - - var UploadHandler = function (req, res, callback) { - EventEmitter.call(this); - this.req = req; - this.res = res; - this.callback = callback; - }; - require('util').inherits(UploadHandler, EventEmitter); - - UploadHandler.prototype.noCache = function () { - this.res.set({ - 'Pragma': 'no-cache', - 'Cache-Control': 'no-store, no-cache, must-revalidate', - 'Content-Disposition': 'inline; filename="files.json"' - }); - }; - - UploadHandler.prototype.get = function () { - this.noCache(); - var files = []; - fs.readdir(options.uploadDir(), _.bind(function (err, list) { - _.each(list, function (name) { - var stats = fs.statSync(options.uploadDir() + '/' + name), - fileInfo; - if (stats.isFile()) { - fileInfo = new FileInfo({ - name: name, - size: stats.size - }); - this.initUrls(fileInfo); - files.push(fileInfo); - } - }, this); - this.callback({files: files}); - }, this)); - }; - - UploadHandler.prototype.post = function () { - var self = this, - form = new formidable.IncomingForm(), - tmpFiles = [], - files = [], - map = {}, - counter = 1, - redirect, - finish = _.bind(function () { - if (!--counter) { - _.each(files, function (fileInfo) { - this.initUrls(fileInfo); - this.emit('end', fileInfo); - }, this); - this.callback({files: files}, redirect); - } - }, this); - - this.noCache(); - - form.uploadDir = options.tmpDir; - form - .on('fileBegin', function (name, file) { - tmpFiles.push(file.path); - var fileInfo = new FileInfo(file); - fileInfo.safeName(); - map[path.basename(file.path)] = fileInfo; - files.push(fileInfo); - self.emit('begin', fileInfo); - }) - .on('field', function (name, value) { - if (name === 'redirect') { - redirect = value; - } - }) - .on('file', function (name, file) { - var fileInfo = map[path.basename(file.path)]; - if (fs.existsSync(file.path)) { - fileInfo.size = file.size; - if (!fileInfo.validate()) { - fs.unlink(file.path); - return; - } - - var generatePreviews = function () { - if (options.imageTypes.test(fileInfo.name)) { - _.each(options.imageVersions, function (value, version) { - // creating directory recursive - if (!fs.existsSync(options.uploadDir() + '/' + version + '/')) - mkdirp.sync(options.uploadDir() + '/' + version + '/'); - - counter++; - var opts = options.imageVersions[version]; - imageMagick.resize({ - width: opts.width, - height: opts.height, - srcPath: options.uploadDir() + '/' + fileInfo.name, - dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, - customArgs: opts.imageArgs || ['-auto-orient'] - }, finish); - }); - } - } - - if (!fs.existsSync(options.uploadDir() + '/')) - mkdirp.sync(options.uploadDir() + '/'); - - counter++; - fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { - if (!err) { - generatePreviews(); - finish(); - } else { - var is = fs.createReadStream(file.path); - var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name); - is.on('end', function (err) { - if (!err) { - fs.unlinkSync(file.path); - generatePreviews(); - } - finish(); - }); - is.pipe(os); - } - }); - } - }) - .on('aborted', function () { - _.each(tmpFiles, function (file) { - var fileInfo = map[path.basename(file)]; - self.emit('abort', fileInfo); - fs.unlink(file); - }); - }) - .on('error', function (e) { - self.emit('error', e); - }) - .on('progress', function (bytesReceived, bytesExpected) { - if (bytesReceived > options.maxPostSize) - self.req.connection.destroy(); - }) - .on('end', finish) - .parse(self.req); - }; - - UploadHandler.prototype.destroy = function () { - var self = this, - fileName = path.basename(decodeURIComponent(this.req.url)); - - fs.unlink(options.uploadDir() + '/' + fileName, function (ex) { - _.each(options.imageVersions, function (value, version) { - fs.unlink(options.uploadDir() + '/' + version + '/' + fileName); - }); - self.emit('delete', fileName); - self.callback({success: !ex}); - }); - }; - - UploadHandler.prototype.initUrls = function (fileInfo) { - var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host')); - fileInfo.setUrl(null, baseUrl + options.uploadUrl()); - fileInfo.setUrl('delete', baseUrl + this.req.originalUrl); - _.each(options.imageVersions, function (value, version) { - if (fs.existsSync(options.uploadDir() + '/' + version + '/' + fileInfo.name)) { - fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version); - } - }, this); - }; - - return UploadHandler; + var FileInfo = require('./fileinfo')( + _.extend({ + baseDir: options.uploadDir + }, _.pick(options, 'minFileSize', 'maxFileSize', 'acceptFileTypes')) + ); + + var UploadHandler = function (req, res, callback) { + EventEmitter.call(this); + this.req = req; + this.res = res; + this.callback = callback; + }; + require('util').inherits(UploadHandler, EventEmitter); + + UploadHandler.prototype.noCache = function () { + this.res.set({ + 'Pragma': 'no-cache', + 'Cache-Control': 'no-store, no-cache, must-revalidate', + 'Content-Disposition': 'inline; filename="files.json"' + }); + }; + + UploadHandler.prototype.get = function () { + this.noCache(); + var files = []; + fs.readdir(options.uploadDir(), _.bind(function (err, list) { + _.each(list, function (name) { + var stats = fs.statSync(options.uploadDir() + '/' + name), + fileInfo; + if (stats.isFile()) { + fileInfo = new FileInfo({ + name: name, + size: stats.size + }); + this.initUrls(fileInfo); + files.push(fileInfo); + } + }, this); + this.callback({files: files}); + }, this)); + }; + + UploadHandler.prototype.post = function () { + var self = this, + form = new formidable.IncomingForm(), + tmpFiles = [], + files = [], + map = {}, + counter = 1, + redirect, + finish = _.bind(function () { + if (!--counter) { + _.each(files, function (fileInfo) { + this.initUrls(fileInfo); + fileInfo = _.extend(fileInfo, self.req.fields); + this.emit('end', fileInfo); + }, this); + this.callback({files: files}, redirect); + } + }, this); + + this.noCache(); + + form.uploadDir = options.tmpDir; + form + .on('fileBegin', function (name, file) { + tmpFiles.push(file.path); + var fileInfo = new FileInfo(file); + fileInfo.safeName(); + map[path.basename(file.path)] = fileInfo; + files.push(fileInfo); + self.emit('begin', fileInfo); + }) + .on('field', function (name, value) { + if (name === 'redirect') { + redirect = value; + } + if ( !self.req.fields ) + self.req.fields = {}; + self.req.fields[name] = value; + }) + .on('file', function (name, file) { + var fileInfo = map[path.basename(file.path)]; + if (fs.existsSync(file.path)) { + fileInfo.size = file.size; + if (!fileInfo.validate()) { + fs.unlink(file.path); + return; + } + + var generatePreviews = function () { + if (options.imageTypes.test(fileInfo.name)) { + _.each(options.imageVersions, function (value, version) { + // creating directory recursive + if (!fs.existsSync(options.uploadDir() + '/' + version + '/')) + mkdirp.sync(options.uploadDir() + '/' + version + '/'); + + counter++; + var opts = options.imageVersions[version]; + imageMagick.resize({ + width: opts.width, + height: opts.height, + srcPath: options.uploadDir() + '/' + fileInfo.name, + dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, + customArgs: opts.imageArgs || ['-auto-orient'] + }, finish); + }); + } + } + + if (!fs.existsSync(options.uploadDir() + '/')) + mkdirp.sync(options.uploadDir() + '/'); + + counter++; + fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { + if (!err) { + generatePreviews(); + finish(); + } else { + var is = fs.createReadStream(file.path); + var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name); + is.on('end', function (err) { + if (!err) { + fs.unlinkSync(file.path); + generatePreviews(); + } + finish(); + }); + is.pipe(os); + } + }); + } + }) + .on('aborted', function () { + _.each(tmpFiles, function (file) { + var fileInfo = map[path.basename(file)]; + self.emit('abort', fileInfo); + fs.unlink(file); + }); + }) + .on('error', function (e) { + self.emit('error', e); + }) + .on('progress', function (bytesReceived, bytesExpected) { + if (bytesReceived > options.maxPostSize) + self.req.connection.destroy(); + }) + .on('end', finish) + .parse(self.req); + }; + + UploadHandler.prototype.destroy = function () { + var self = this, + fileName = path.basename(decodeURIComponent(this.req.url)); + + fs.unlink(options.uploadDir() + '/' + fileName, function (ex) { + _.each(options.imageVersions, function (value, version) { + fs.unlink(options.uploadDir() + '/' + version + '/' + fileName); + }); + self.emit('delete', fileName); + self.callback({success: !ex}); + }); + }; + + UploadHandler.prototype.initUrls = function (fileInfo) { + var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host')); + fileInfo.setUrl(null, baseUrl + options.uploadUrl()); + fileInfo.setUrl('delete', baseUrl + this.req.originalUrl); + _.each(options.imageVersions, function (value, version) { + if (fs.existsSync(options.uploadDir() + '/' + version + '/' + fileInfo.name)) { + fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version); + } + }, this); + }; + + return UploadHandler; } From 83668f56623a99f0e781622e7d68f5d9f4b165b4 Mon Sep 17 00:00:00 2001 From: Guntur Sarwohadi Date: Sat, 12 Oct 2013 11:37:36 +0700 Subject: [PATCH 04/38] Collect formData on 'field' event from form property in UploadHandler.prototype.post method to req.fields object (created new object if undefined). Extend fileInfo with req.fields on 'end' event. --- lib/uploadhandler.js | 364 +++++++++++++++++++++---------------------- 1 file changed, 182 insertions(+), 182 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 05f60ba..6099e58 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -1,188 +1,188 @@ var EventEmitter = require('events').EventEmitter, - path = require('path'), - fs = require('fs'), - formidable = require('formidable'), - imageMagick = require('imagemagick'), - mkdirp = require('mkdirp'), - _ = require('lodash'); + path = require('path'), + fs = require('fs'), + formidable = require('formidable'), + imageMagick = require('imagemagick'), + mkdirp = require('mkdirp'), + _ = require('lodash'); module.exports = function (options) { - var FileInfo = require('./fileinfo')( - _.extend({ - baseDir: options.uploadDir - }, _.pick(options, 'minFileSize', 'maxFileSize', 'acceptFileTypes')) - ); - - var UploadHandler = function (req, res, callback) { - EventEmitter.call(this); - this.req = req; - this.res = res; - this.callback = callback; - }; - require('util').inherits(UploadHandler, EventEmitter); - - UploadHandler.prototype.noCache = function () { - this.res.set({ - 'Pragma': 'no-cache', - 'Cache-Control': 'no-store, no-cache, must-revalidate', - 'Content-Disposition': 'inline; filename="files.json"' - }); - }; - - UploadHandler.prototype.get = function () { - this.noCache(); - var files = []; - fs.readdir(options.uploadDir(), _.bind(function (err, list) { - _.each(list, function (name) { - var stats = fs.statSync(options.uploadDir() + '/' + name), - fileInfo; - if (stats.isFile()) { - fileInfo = new FileInfo({ - name: name, - size: stats.size - }); - this.initUrls(fileInfo); - files.push(fileInfo); - } - }, this); - this.callback({files: files}); - }, this)); - }; - - UploadHandler.prototype.post = function () { - var self = this, - form = new formidable.IncomingForm(), - tmpFiles = [], - files = [], - map = {}, - counter = 1, - redirect, - finish = _.bind(function () { - if (!--counter) { - _.each(files, function (fileInfo) { - this.initUrls(fileInfo); - fileInfo = _.extend(fileInfo, self.req.fields); - this.emit('end', fileInfo); - }, this); - this.callback({files: files}, redirect); - } - }, this); - - this.noCache(); - - form.uploadDir = options.tmpDir; - form - .on('fileBegin', function (name, file) { - tmpFiles.push(file.path); - var fileInfo = new FileInfo(file); - fileInfo.safeName(); - map[path.basename(file.path)] = fileInfo; - files.push(fileInfo); - self.emit('begin', fileInfo); - }) - .on('field', function (name, value) { - if (name === 'redirect') { - redirect = value; - } - if ( !self.req.fields ) - self.req.fields = {}; - self.req.fields[name] = value; - }) - .on('file', function (name, file) { - var fileInfo = map[path.basename(file.path)]; - if (fs.existsSync(file.path)) { - fileInfo.size = file.size; - if (!fileInfo.validate()) { - fs.unlink(file.path); - return; - } - - var generatePreviews = function () { - if (options.imageTypes.test(fileInfo.name)) { - _.each(options.imageVersions, function (value, version) { - // creating directory recursive - if (!fs.existsSync(options.uploadDir() + '/' + version + '/')) - mkdirp.sync(options.uploadDir() + '/' + version + '/'); - - counter++; - var opts = options.imageVersions[version]; - imageMagick.resize({ - width: opts.width, - height: opts.height, - srcPath: options.uploadDir() + '/' + fileInfo.name, - dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, - customArgs: opts.imageArgs || ['-auto-orient'] - }, finish); - }); - } - } - - if (!fs.existsSync(options.uploadDir() + '/')) - mkdirp.sync(options.uploadDir() + '/'); - - counter++; - fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { - if (!err) { - generatePreviews(); - finish(); - } else { - var is = fs.createReadStream(file.path); - var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name); - is.on('end', function (err) { - if (!err) { - fs.unlinkSync(file.path); - generatePreviews(); - } - finish(); - }); - is.pipe(os); - } - }); - } - }) - .on('aborted', function () { - _.each(tmpFiles, function (file) { - var fileInfo = map[path.basename(file)]; - self.emit('abort', fileInfo); - fs.unlink(file); - }); - }) - .on('error', function (e) { - self.emit('error', e); - }) - .on('progress', function (bytesReceived, bytesExpected) { - if (bytesReceived > options.maxPostSize) - self.req.connection.destroy(); - }) - .on('end', finish) - .parse(self.req); - }; - - UploadHandler.prototype.destroy = function () { - var self = this, - fileName = path.basename(decodeURIComponent(this.req.url)); - - fs.unlink(options.uploadDir() + '/' + fileName, function (ex) { - _.each(options.imageVersions, function (value, version) { - fs.unlink(options.uploadDir() + '/' + version + '/' + fileName); - }); - self.emit('delete', fileName); - self.callback({success: !ex}); - }); - }; - - UploadHandler.prototype.initUrls = function (fileInfo) { - var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host')); - fileInfo.setUrl(null, baseUrl + options.uploadUrl()); - fileInfo.setUrl('delete', baseUrl + this.req.originalUrl); - _.each(options.imageVersions, function (value, version) { - if (fs.existsSync(options.uploadDir() + '/' + version + '/' + fileInfo.name)) { - fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version); - } - }, this); - }; - - return UploadHandler; + var FileInfo = require('./fileinfo')( + _.extend({ + baseDir: options.uploadDir + }, _.pick(options, 'minFileSize', 'maxFileSize', 'acceptFileTypes')) + ); + + var UploadHandler = function (req, res, callback) { + EventEmitter.call(this); + this.req = req; + this.res = res; + this.callback = callback; + }; + require('util').inherits(UploadHandler, EventEmitter); + + UploadHandler.prototype.noCache = function () { + this.res.set({ + 'Pragma': 'no-cache', + 'Cache-Control': 'no-store, no-cache, must-revalidate', + 'Content-Disposition': 'inline; filename="files.json"' + }); + }; + + UploadHandler.prototype.get = function () { + this.noCache(); + var files = []; + fs.readdir(options.uploadDir(), _.bind(function (err, list) { + _.each(list, function (name) { + var stats = fs.statSync(options.uploadDir() + '/' + name), + fileInfo; + if (stats.isFile()) { + fileInfo = new FileInfo({ + name: name, + size: stats.size + }); + this.initUrls(fileInfo); + files.push(fileInfo); + } + }, this); + this.callback({files: files}); + }, this)); + }; + + UploadHandler.prototype.post = function () { + var self = this, + form = new formidable.IncomingForm(), + tmpFiles = [], + files = [], + map = {}, + counter = 1, + redirect, + finish = _.bind(function () { + if (!--counter) { + _.each(files, function (fileInfo) { + this.initUrls(fileInfo); + fileInfo = _.extend(fileInfo, self.req.fields); + this.emit('end', fileInfo); + }, this); + this.callback({files: files}, redirect); + } + }, this); + + this.noCache(); + + form.uploadDir = options.tmpDir; + form + .on('fileBegin', function (name, file) { + tmpFiles.push(file.path); + var fileInfo = new FileInfo(file); + fileInfo.safeName(); + map[path.basename(file.path)] = fileInfo; + files.push(fileInfo); + self.emit('begin', fileInfo); + }) + .on('field', function (name, value) { + if (name === 'redirect') { + redirect = value; + } + if ( !self.req.fields ) + self.req.fields = {}; + self.req.fields[name] = value; + }) + .on('file', function (name, file) { + var fileInfo = map[path.basename(file.path)]; + if (fs.existsSync(file.path)) { + fileInfo.size = file.size; + if (!fileInfo.validate()) { + fs.unlink(file.path); + return; + } + + var generatePreviews = function () { + if (options.imageTypes.test(fileInfo.name)) { + _.each(options.imageVersions, function (value, version) { + // creating directory recursive + if (!fs.existsSync(options.uploadDir() + '/' + version + '/')) + mkdirp.sync(options.uploadDir() + '/' + version + '/'); + + counter++; + var opts = options.imageVersions[version]; + imageMagick.resize({ + width: opts.width, + height: opts.height, + srcPath: options.uploadDir() + '/' + fileInfo.name, + dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, + customArgs: opts.imageArgs || ['-auto-orient'] + }, finish); + }); + } + } + + if (!fs.existsSync(options.uploadDir() + '/')) + mkdirp.sync(options.uploadDir() + '/'); + + counter++; + fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { + if (!err) { + generatePreviews(); + finish(); + } else { + var is = fs.createReadStream(file.path); + var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name); + is.on('end', function (err) { + if (!err) { + fs.unlinkSync(file.path); + generatePreviews(); + } + finish(); + }); + is.pipe(os); + } + }); + } + }) + .on('aborted', function () { + _.each(tmpFiles, function (file) { + var fileInfo = map[path.basename(file)]; + self.emit('abort', fileInfo); + fs.unlink(file); + }); + }) + .on('error', function (e) { + self.emit('error', e); + }) + .on('progress', function (bytesReceived, bytesExpected) { + if (bytesReceived > options.maxPostSize) + self.req.connection.destroy(); + }) + .on('end', finish) + .parse(self.req); + }; + + UploadHandler.prototype.destroy = function () { + var self = this, + fileName = path.basename(decodeURIComponent(this.req.url)); + + fs.unlink(options.uploadDir() + '/' + fileName, function (ex) { + _.each(options.imageVersions, function (value, version) { + fs.unlink(options.uploadDir() + '/' + version + '/' + fileName); + }); + self.emit('delete', fileName); + self.callback({success: !ex}); + }); + }; + + UploadHandler.prototype.initUrls = function (fileInfo) { + var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host')); + fileInfo.setUrl(null, baseUrl + options.uploadUrl()); + fileInfo.setUrl('delete', baseUrl + this.req.originalUrl); + _.each(options.imageVersions, function (value, version) { + if (fs.existsSync(options.uploadDir() + '/' + version + '/' + fileInfo.name)) { + fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version); + } + }, this); + }; + + return UploadHandler; } From 559273888d3fb24525fddb35540260eb6b52dda1 Mon Sep 17 00:00:00 2001 From: Dudemullet Date: Sat, 1 Feb 2014 23:18:21 -0600 Subject: [PATCH 05/38] Get multi-platform tmp dir When using platforms where "/tmp" wasn't the default temp directory AND you wouldn't define a specific `tmp` directory the middleware would throw an error. --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1488b5f..0ebaf48 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var _ = require('lodash'), - EventEmitter = require('events').EventEmitter; + EventEmitter = require('events').EventEmitter, + os = require("os"); var JqueryFileUploadMiddleware = function () { EventEmitter.call(this); @@ -10,7 +11,7 @@ require('util').inherits(JqueryFileUploadMiddleware, EventEmitter); JqueryFileUploadMiddleware.prototype.prepareOptions = function (options) { options = _.extend({ - tmpDir: '/tmp', + tmpDir: os.tmpdir(), uploadDir: __dirname + '/public/files', uploadUrl: '/files/', maxPostSize: 11000000000, // 11 GB From 6e2d68da931f5ddcf3af1937743d52efb847dc9d Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Tue, 4 Feb 2014 21:34:37 +0300 Subject: [PATCH 06/38] 0.1.1, fixes #5 --- History.md | 11 +++++++++++ package.json | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index bbfd1b2..70cbe56 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,14 @@ +0.1.1 / 2014-02-04 +================== + + * #5 fixed + +0.1.0 / 2013-10-07 +================== + + * documentation cleanup + + 0.0.9 / 2013-10-07 ================== diff --git a/package.json b/package.json index ea657a4..5d08788 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "express", "middleware" ], - "version": "0.1.0", + "version": "0.1.1", "dependencies": { "formidable": ">=1.0.11", "imagemagick": ">=0.1.2", @@ -28,6 +28,6 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "_id": "jquery-file-upload-middleware@0.1.0", + "_id": "jquery-file-upload-middleware@0.1.1", "license": "MIT" } From 46f5e37e06571ae841f42898767a4ce0caa850d1 Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 17:00:36 +0900 Subject: [PATCH 07/38] reduce sync operations --- lib/uploadhandler.js | 51 +++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index ece4b55..8c38c3d 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -4,7 +4,8 @@ var EventEmitter = require('events').EventEmitter, formidable = require('formidable'), imageMagick = require('imagemagick'), mkdirp = require('mkdirp'), - _ = require('lodash'); + _ = require('lodash'), + async = require('async'); module.exports = function (options) { @@ -34,19 +35,25 @@ module.exports = function (options) { this.noCache(); var files = []; fs.readdir(options.uploadDir(), _.bind(function (err, list) { - _.each(list, function (name) { - var stats = fs.statSync(options.uploadDir() + '/' + name), - fileInfo; - if (stats.isFile()) { - fileInfo = new FileInfo({ - name: name, - size: stats.size - }); - this.initUrls(fileInfo); - files.push(fileInfo); - } - }, this); - this.callback({files: files}); + async.each(list, function(name, cb) { + fs.stat(options.uploadDir() + '/' + name, function(err, stats) { + if (!err) { + if (stats.isFile()) { + fileInfo = new FileInfo({ + name: name, + size: stats.size + }); + this.initUrls(fileInfo); + files.push(fileInfo); + } + } + cb(err); + }); + }, + function(err) { + if (err) console.log(err); + this.callback({files: files}); + }); }, this)); }; @@ -87,7 +94,8 @@ module.exports = function (options) { }) .on('file', function (name, file) { var fileInfo = map[path.basename(file.path)]; - if (fs.existsSync(file.path)) { + fs.exists(file.path, function(exists) { + if (exists) { fileInfo.size = file.size; if (!fileInfo.validate()) { fs.unlink(file.path); @@ -98,9 +106,7 @@ module.exports = function (options) { if (options.imageTypes.test(fileInfo.name)) { _.each(options.imageVersions, function (value, version) { // creating directory recursive - if (!fs.existsSync(options.uploadDir() + '/' + version + '/')) - mkdirp.sync(options.uploadDir() + '/' + version + '/'); - + mkdirp(options.uploadDir() + '/' + version + '/', function (err, made) { counter++; var opts = options.imageVersions[version]; imageMagick.resize({ @@ -110,13 +116,12 @@ module.exports = function (options) { dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, customArgs: opts.imageArgs || ['-auto-orient'] }, finish); + } }); } } - if (!fs.existsSync(options.uploadDir() + '/')) - mkdirp.sync(options.uploadDir() + '/'); - + mkdirp(options.uploadDir() + '/', function(err, made) { counter++; fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { if (!err) { @@ -127,7 +132,7 @@ module.exports = function (options) { var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name); is.on('end', function (err) { if (!err) { - fs.unlinkSync(file.path); + fs.unlink(file.path); generatePreviews(); } finish(); @@ -135,7 +140,9 @@ module.exports = function (options) { is.pipe(os); } }); + }); } + } }) .on('aborted', function () { _.each(tmpFiles, function (file) { From f36bb8a639cdc46b44b98ba06cfd5b69cb69639e Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 18:22:53 +0900 Subject: [PATCH 08/38] reduce more sync operations --- lib/uploadhandler.js | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 8c38c3d..044d93f 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -43,8 +43,11 @@ module.exports = function (options) { name: name, size: stats.size }); - this.initUrls(fileInfo); - files.push(fileInfo); + this.initUrls(fileInfo, function(err) { + files.push(fileInfo); + cb(err); + return; + }); } } cb(err); @@ -67,11 +70,15 @@ module.exports = function (options) { redirect, finish = _.bind(function () { if (!--counter) { - _.each(files, function (fileInfo) { - this.initUrls(fileInfo); - this.emit('end', fileInfo); - }, this); - this.callback({files: files}, redirect); + async.each(files, function(fileInfo, cb) { + this.initUrls(fileInfo, function(err) { + this.emit('end', fileInfo); + cb(err); + }); + }, + function(err) { + this.callback({files: files}, redirect); + }); } }, this); @@ -175,15 +182,17 @@ module.exports = function (options) { }); }; - UploadHandler.prototype.initUrls = function (fileInfo) { + UploadHandler.prototype.initUrls = function (fileInfo, cb) { var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host')); fileInfo.setUrl(null, baseUrl + options.uploadUrl()); fileInfo.setUrl('delete', baseUrl + this.req.originalUrl); - _.each(options.imageVersions, function (value, version) { - if (fs.existsSync(options.uploadDir() + '/' + version + '/' + fileInfo.name)) { - fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version); - } - }, this); + async.each(Object.keys(options.imageVersions), function(version, cb) { + fs.exists(options.uploadDir() + '/' + version + '/' + fileInfo.name, function(exists) { + if (exists) fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version); + cb(null); + }) + }, + cb); }; return UploadHandler; From cc105a2924c5463855c5e39a27b12f25f9c81851 Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 18:29:49 +0900 Subject: [PATCH 09/38] binding this to some functions --- lib/uploadhandler.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 044d93f..526d5be 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -35,8 +35,8 @@ module.exports = function (options) { this.noCache(); var files = []; fs.readdir(options.uploadDir(), _.bind(function (err, list) { - async.each(list, function(name, cb) { - fs.stat(options.uploadDir() + '/' + name, function(err, stats) { + async.each(list, _.bind(function(name, cb) { + fs.stat(options.uploadDir() + '/' + name, _.bind(function(err, stats) { if (!err) { if (stats.isFile()) { fileInfo = new FileInfo({ @@ -51,8 +51,8 @@ module.exports = function (options) { } } cb(err); - }); - }, + }, this)); + }, this), function(err) { if (err) console.log(err); this.callback({files: files}); @@ -70,15 +70,15 @@ module.exports = function (options) { redirect, finish = _.bind(function () { if (!--counter) { - async.each(files, function(fileInfo, cb) { - this.initUrls(fileInfo, function(err) { + async.each(files, _.bind(function(fileInfo, cb) { + this.initUrls(fileInfo, _.bind(function(err) { this.emit('end', fileInfo); cb(err); - }); - }, - function(err) { + }, this)); + }, this), + _.bind(function(err) { this.callback({files: files}, redirect); - }); + }, this)); } }, this); From 499475e978b5be15bd529a00877802fe5dc48ca5 Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 19:38:17 +0900 Subject: [PATCH 10/38] fix missing parentheses --- lib/uploadhandler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 526d5be..4d9868d 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -123,7 +123,7 @@ module.exports = function (options) { dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, customArgs: opts.imageArgs || ['-auto-orient'] }, finish); - } + }); }); } } @@ -149,7 +149,7 @@ module.exports = function (options) { }); }); } - } + }); }) .on('aborted', function () { _.each(tmpFiles, function (file) { From 44087deea96f6b7834b7c2e9d788aa44fec5816b Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 19:39:52 +0900 Subject: [PATCH 11/38] increasing counter for each file before the end event fired --- lib/uploadhandler.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 4d9868d..12bda0d 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -100,21 +100,23 @@ module.exports = function (options) { } }) .on('file', function (name, file) { + counter++; var fileInfo = map[path.basename(file.path)]; fs.exists(file.path, function(exists) { if (exists) { fileInfo.size = file.size; if (!fileInfo.validate()) { fs.unlink(file.path); + counter--; return; } var generatePreviews = function () { if (options.imageTypes.test(fileInfo.name)) { _.each(options.imageVersions, function (value, version) { + counter++; // creating directory recursive mkdirp(options.uploadDir() + '/' + version + '/', function (err, made) { - counter++; var opts = options.imageVersions[version]; imageMagick.resize({ width: opts.width, @@ -129,7 +131,6 @@ module.exports = function (options) { } mkdirp(options.uploadDir() + '/', function(err, made) { - counter++; fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { if (!err) { generatePreviews(); @@ -149,6 +150,7 @@ module.exports = function (options) { }); }); } + else counter--; }); }) .on('aborted', function () { From 336598a777119e354483869f8cdeb813bf0910df Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 19:47:54 +0900 Subject: [PATCH 12/38] add the async module to the package.json --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ea657a4..bc17318 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "formidable": ">=1.0.11", "imagemagick": ">=0.1.2", "lodash": ">= 0.9.2", - "mkdirp": ">= 0.3.4" + "mkdirp": ">= 0.3.4", + "async": "*" }, "engines": { "node": ">= 0.8.8" From e36b88a04c7eec0e8753b89f9bc42c6407a6c657 Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 19:53:36 +0900 Subject: [PATCH 13/38] indentation --- lib/uploadhandler.js | 168 +++++++++++++++++++++---------------------- 1 file changed, 84 insertions(+), 84 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 12bda0d..baf3280 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -5,7 +5,7 @@ var EventEmitter = require('events').EventEmitter, imageMagick = require('imagemagick'), mkdirp = require('mkdirp'), _ = require('lodash'), - async = require('async'); + async = require('async'); module.exports = function (options) { @@ -35,28 +35,28 @@ module.exports = function (options) { this.noCache(); var files = []; fs.readdir(options.uploadDir(), _.bind(function (err, list) { - async.each(list, _.bind(function(name, cb) { - fs.stat(options.uploadDir() + '/' + name, _.bind(function(err, stats) { - if (!err) { - if (stats.isFile()) { - fileInfo = new FileInfo({ - name: name, - size: stats.size - }); - this.initUrls(fileInfo, function(err) { - files.push(fileInfo); - cb(err); - return; - }); - } - } - cb(err); - }, this)); - }, this), - function(err) { - if (err) console.log(err); - this.callback({files: files}); - }); + async.each(list, _.bind(function(name, cb) { + fs.stat(options.uploadDir() + '/' + name, _.bind(function(err, stats) { + if (!err) { + if (stats.isFile()) { + fileInfo = new FileInfo({ + name: name, + size: stats.size + }); + this.initUrls(fileInfo, function(err) { + files.push(fileInfo); + cb(err); + return; + }); + } + } + cb(err); + }, this)); + }, this), + function(err) { + if (err) console.log(err); + this.callback({files: files}); + }); }, this)); }; @@ -70,15 +70,15 @@ module.exports = function (options) { redirect, finish = _.bind(function () { if (!--counter) { - async.each(files, _.bind(function(fileInfo, cb) { + async.each(files, _.bind(function(fileInfo, cb) { this.initUrls(fileInfo, _.bind(function(err) { - this.emit('end', fileInfo); - cb(err); - }, this)); - }, this), - _.bind(function(err) { - this.callback({files: files}, redirect); - }, this)); + this.emit('end', fileInfo); + cb(err); + }, this)); + }, this), + _.bind(function(err) { + this.callback({files: files}, redirect); + }, this)); } }, this); @@ -100,58 +100,58 @@ module.exports = function (options) { } }) .on('file', function (name, file) { - counter++; + counter++; var fileInfo = map[path.basename(file.path)]; - fs.exists(file.path, function(exists) { - if (exists) { - fileInfo.size = file.size; - if (!fileInfo.validate()) { - fs.unlink(file.path); - counter--; - return; - } - - var generatePreviews = function () { - if (options.imageTypes.test(fileInfo.name)) { - _.each(options.imageVersions, function (value, version) { - counter++; - // creating directory recursive - mkdirp(options.uploadDir() + '/' + version + '/', function (err, made) { - var opts = options.imageVersions[version]; - imageMagick.resize({ - width: opts.width, - height: opts.height, - srcPath: options.uploadDir() + '/' + fileInfo.name, - dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, - customArgs: opts.imageArgs || ['-auto-orient'] - }, finish); - }); - }); + fs.exists(file.path, function(exists) { + if (exists) { + fileInfo.size = file.size; + if (!fileInfo.validate()) { + fs.unlink(file.path); + counter--; + return; } - } - - mkdirp(options.uploadDir() + '/', function(err, made) { - fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { - if (!err) { - generatePreviews(); - finish(); - } else { - var is = fs.createReadStream(file.path); - var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name); - is.on('end', function (err) { + + var generatePreviews = function () { + if (options.imageTypes.test(fileInfo.name)) { + _.each(options.imageVersions, function (value, version) { + counter++; + // creating directory recursive + mkdirp(options.uploadDir() + '/' + version + '/', function (err, made) { + var opts = options.imageVersions[version]; + imageMagick.resize({ + width: opts.width, + height: opts.height, + srcPath: options.uploadDir() + '/' + fileInfo.name, + dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, + customArgs: opts.imageArgs || ['-auto-orient'] + }, finish); + }); + }); + } + } + + mkdirp(options.uploadDir() + '/', function(err, made) { + fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { if (!err) { - fs.unlink(file.path); generatePreviews(); + finish(); + } else { + var is = fs.createReadStream(file.path); + var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name); + is.on('end', function (err) { + if (!err) { + fs.unlink(file.path); + generatePreviews(); + } + finish(); + }); + is.pipe(os); } - finish(); }); - is.pipe(os); - } - }); - }); - } - else counter--; - }); + }); + } + else counter--; + }); }) .on('aborted', function () { _.each(tmpFiles, function (file) { @@ -188,13 +188,13 @@ module.exports = function (options) { var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host')); fileInfo.setUrl(null, baseUrl + options.uploadUrl()); fileInfo.setUrl('delete', baseUrl + this.req.originalUrl); - async.each(Object.keys(options.imageVersions), function(version, cb) { - fs.exists(options.uploadDir() + '/' + version + '/' + fileInfo.name, function(exists) { - if (exists) fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version); - cb(null); - }) - }, - cb); + async.each(Object.keys(options.imageVersions), function(version, cb) { + fs.exists(options.uploadDir() + '/' + version + '/' + fileInfo.name, function(exists) { + if (exists) fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version); + cb(null); + }) + }, + cb); }; return UploadHandler; From b97e921a005cf28202d0e328f1d49655222e401c Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 20:13:40 +0900 Subject: [PATCH 14/38] fix duplicated call of callback --- lib/uploadhandler.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index baf3280..e217315 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -37,20 +37,18 @@ module.exports = function (options) { fs.readdir(options.uploadDir(), _.bind(function (err, list) { async.each(list, _.bind(function(name, cb) { fs.stat(options.uploadDir() + '/' + name, _.bind(function(err, stats) { - if (!err) { - if (stats.isFile()) { - fileInfo = new FileInfo({ - name: name, - size: stats.size - }); - this.initUrls(fileInfo, function(err) { - files.push(fileInfo); - cb(err); - return; - }); - } + if (!err && stats.isFile()) { + fileInfo = new FileInfo({ + name: name, + size: stats.size + }); + this.initUrls(fileInfo, function(err) { + files.push(fileInfo); + cb(err); + return; + }); } - cb(err); + else cb(err); }, this)); }, this), function(err) { From f2e62f841f63c1706dd303d9b64c9168a75557d7 Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 20:13:58 +0900 Subject: [PATCH 15/38] fix missing binding this --- lib/uploadhandler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index e217315..105f27b 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -51,10 +51,10 @@ module.exports = function (options) { else cb(err); }, this)); }, this), - function(err) { + _.bind(function(err) { if (err) console.log(err); this.callback({files: files}); - }); + }, this)); }, this)); }; From 3c8590a2f15253fa89c6da90f96729529b080795 Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 20:15:22 +0900 Subject: [PATCH 16/38] remove unnecessary spaces --- lib/uploadhandler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 105f27b..426e212 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -108,7 +108,7 @@ module.exports = function (options) { counter--; return; } - + var generatePreviews = function () { if (options.imageTypes.test(fileInfo.name)) { _.each(options.imageVersions, function (value, version) { @@ -127,7 +127,7 @@ module.exports = function (options) { }); } } - + mkdirp(options.uploadDir() + '/', function(err, made) { fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { if (!err) { From 1965c8ba6ec23572ef6adbfa66dc6f7a5db49ded Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 20:20:54 +0900 Subject: [PATCH 17/38] indent --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bc17318..eaaa4f5 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "imagemagick": ">=0.1.2", "lodash": ">= 0.9.2", "mkdirp": ">= 0.3.4", - "async": "*" + "async": "*" }, "engines": { "node": ">= 0.8.8" From f28f5e981a4667101b3edafedb92d0289cd1b26d Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 20:37:14 +0900 Subject: [PATCH 18/38] fix missing var. remove unnecessary code --- lib/uploadhandler.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 426e212..5039b3b 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -38,21 +38,19 @@ module.exports = function (options) { async.each(list, _.bind(function(name, cb) { fs.stat(options.uploadDir() + '/' + name, _.bind(function(err, stats) { if (!err && stats.isFile()) { - fileInfo = new FileInfo({ + var fileInfo = new FileInfo({ name: name, size: stats.size }); this.initUrls(fileInfo, function(err) { files.push(fileInfo); cb(err); - return; }); } else cb(err); }, this)); }, this), _.bind(function(err) { - if (err) console.log(err); this.callback({files: files}); }, this)); }, this)); From 80be7efe227a41929549abf48a9bfbc2c0a3b603 Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 20:38:28 +0900 Subject: [PATCH 19/38] change decrementing counter to calling finish --- lib/uploadhandler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 5039b3b..178859b 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -103,7 +103,7 @@ module.exports = function (options) { fileInfo.size = file.size; if (!fileInfo.validate()) { fs.unlink(file.path); - counter--; + finish(); return; } @@ -146,7 +146,7 @@ module.exports = function (options) { }); }); } - else counter--; + else finish(); }); }) .on('aborted', function () { From 3135dec0d47a71d8f5c64abb0358b7851ec1c3c0 Mon Sep 17 00:00:00 2001 From: peecky Date: Fri, 28 Mar 2014 20:48:35 +0900 Subject: [PATCH 20/38] indent --- lib/uploadhandler.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 178859b..8403595 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -113,14 +113,14 @@ module.exports = function (options) { counter++; // creating directory recursive mkdirp(options.uploadDir() + '/' + version + '/', function (err, made) { - var opts = options.imageVersions[version]; - imageMagick.resize({ - width: opts.width, - height: opts.height, - srcPath: options.uploadDir() + '/' + fileInfo.name, - dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, - customArgs: opts.imageArgs || ['-auto-orient'] - }, finish); + var opts = options.imageVersions[version]; + imageMagick.resize({ + width: opts.width, + height: opts.height, + srcPath: options.uploadDir() + '/' + fileInfo.name, + dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, + customArgs: opts.imageArgs || ['-auto-orient'] + }, finish); }); }); } From d513004c11794d88249fe8fdedf6adb7e7711355 Mon Sep 17 00:00:00 2001 From: Tony Spiro Date: Sat, 10 May 2014 20:53:10 -0500 Subject: [PATCH 21/38] Update README.md Added security measure to prevent access to file data except for when uploading. --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 0aaac0a..d9ba21e 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,38 @@ On the frontend: ``` +To prevent access to /upload except for post (for security) +```javascript +upload.configure({ + uploadDir: __dirname + '/public/uploads/', + uploadUrl: '/uploads' +}); + +/// Redirect all to home except post +app.get('/upload', function( req, res ){ + res.redirect('/'); +}); + +app.put('/upload', function( req, res ){ + res.redirect('/'); +}); + +app.delete('/upload', function( req, res ){ + res.redirect('/'); +}); + +app.use('/upload', function(req, res, next){ + upload.fileHandler({ + uploadDir: function () { + return __dirname + '/public/uploads/' + }, + uploadUrl: function () { + return '/uploads' + } + })(req, res, next); +}); +``` + Overriding global configuration ```javascript From 0bf2aef964c220b7b64a0919f75688d8d9d3dda9 Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Sun, 8 Jun 2014 20:21:08 +0300 Subject: [PATCH 22/38] 0.1.2 --- History.md | 5 +++++ README.md | 1 + package.json | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 70cbe56..a8ab1e0 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +0.1.1 / 2014-06-08 +================== + + * #31 merged in + 0.1.1 / 2014-02-04 ================== diff --git a/README.md b/README.md index 0aaac0a..717f223 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,7 @@ Other options and their default values: ## Contributors * [@soomtong](http://github.com/soomtong) + * [@gsarwohadi](https://github.com/gsarwohadi) ## License Copyright (c) 2012 [Aleksandr Guidrevitch](http://aguidrevitch.blogspot.com/) diff --git a/package.json b/package.json index 5d08788..0d3977a 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "express", "middleware" ], - "version": "0.1.1", + "version": "0.1.2", "dependencies": { "formidable": ">=1.0.11", "imagemagick": ">=0.1.2", @@ -28,6 +28,6 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "_id": "jquery-file-upload-middleware@0.1.1", + "_id": "jquery-file-upload-middleware@0.1.2", "license": "MIT" } From a3328bf2ab88dc3b857e1bdcdb39890a6e7e4bfb Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Sat, 5 Jul 2014 23:26:10 +0300 Subject: [PATCH 23/38] changelog updated --- History.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index a8ab1e0..808eeee 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,9 @@ -0.1.1 / 2014-06-08 +0.1.3 / 2014-07-05 +================== + + * #46 merged in (reduce synchronous disk IO operations) + +0.1.2 / 2014-06-08 ================== * #31 merged in From 48c05be69057a0c3cfd9dfc1143098ee121634dd Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Sat, 5 Jul 2014 23:26:38 +0300 Subject: [PATCH 24/38] v0.1.3 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index ab321d0..746488c 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "express", "middleware" ], - "version": "0.1.2", + "version": "0.1.3", "dependencies": { "formidable": ">=1.0.11", "imagemagick": ">=0.1.2", @@ -29,6 +29,6 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "_id": "jquery-file-upload-middleware@0.1.2", + "_id": "jquery-file-upload-middleware@0.1.3", "license": "MIT" } From 068e12529f7c0ea965fa6ee75712c1d5a77c73a9 Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Sun, 6 Jul 2014 18:23:02 +0300 Subject: [PATCH 25/38] contributors list updated --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 717f223..2be0099 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,7 @@ Other options and their default values: * [@soomtong](http://github.com/soomtong) * [@gsarwohadi](https://github.com/gsarwohadi) + * [@peecky](https://github.com/peecky) ## License Copyright (c) 2012 [Aleksandr Guidrevitch](http://aguidrevitch.blogspot.com/) From 63a3cd3a1b3bb1a5dd52c681ee9c00f66bf908c9 Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Sun, 6 Jul 2014 18:24:05 +0300 Subject: [PATCH 26/38] contributors list updated --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5ee93ef..6b6d821 100644 --- a/README.md +++ b/README.md @@ -279,6 +279,7 @@ Other options and their default values: * [@soomtong](http://github.com/soomtong) * [@gsarwohadi](https://github.com/gsarwohadi) * [@peecky](https://github.com/peecky) + * [@tonyspiro](https://github.com/tonyspiro) ## License Copyright (c) 2012 [Aleksandr Guidrevitch](http://aguidrevitch.blogspot.com/) From b1ac0a8290e35147a9947d3ecb5cb097bdb7caef Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Sun, 6 Jul 2014 18:25:36 +0300 Subject: [PATCH 27/38] v0.1.4 --- History.md | 5 +++++ package.json | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 808eeee..bd2e57b 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +0.1.4 / 2014-07-06 +================== + + * #53 documentation update + 0.1.3 / 2014-07-05 ================== diff --git a/package.json b/package.json index 746488c..85066a5 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "express", "middleware" ], - "version": "0.1.3", + "version": "0.1.4", "dependencies": { "formidable": ">=1.0.11", "imagemagick": ">=0.1.2", @@ -29,6 +29,6 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "_id": "jquery-file-upload-middleware@0.1.3", + "_id": "jquery-file-upload-middleware@0.1.4", "license": "MIT" } From 8235152d3efe683d503d4df17faffd5051b12e6e Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Sat, 2 Aug 2014 21:24:22 +0300 Subject: [PATCH 28/38] #31 request and response objects passed to event handlers --- History.md | 5 +++++ README.md | 10 +++++----- lib/filehandler.js | 11 ++++++----- package.json | 4 ++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/History.md b/History.md index bd2e57b..db5ea58 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +0.1.5 / 2014-08-02 +================== + + * #31 request and response objects passed to event handlers + 0.1.4 / 2014-07-06 ================== diff --git a/README.md b/README.md index 6b6d821..280d0bd 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ More sophisticated example - Events app.use('/upload', upload.fileHandler()); // events - upload.on('begin', function (fileInfo) { + upload.on('begin', function (fileInfo, req, res) { // fileInfo structure is the same as returned to browser // { // name: '3 (3).jpg', @@ -113,10 +113,10 @@ More sophisticated example - Events // thumbnail_url: 'http://youhost/uploads/thumbnail/3%20(3).jpg' // } }); - upload.on('abort', function (fileInfo) { ... }); - upload.on('end', function (fileInfo) { ... }); - upload.on('delete', function (fileInfo) { ... }); - upload.on('error', function (e) { + upload.on('abort', function (fileInfo, req, res) { ... }); + upload.on('end', function (fileInfo, req, res) { ... }); + upload.on('delete', function (fileInfo, req, res) { ... }); + upload.on('error', function (e, req, res) { console.log(e.message); }); ``` diff --git a/lib/filehandler.js b/lib/filehandler.js index bab6f89..2c75847 100644 --- a/lib/filehandler.js +++ b/lib/filehandler.js @@ -1,6 +1,7 @@ module.exports = function (middleware, options) { return function (req, res, next) { + this.req = req; res.set({ 'Access-Control-Allow-Origin': options.accessControl.allowOrigin, 'Access-Control-Allow-Methods': options.accessControl.allowMethods @@ -21,19 +22,19 @@ module.exports = function (middleware, options) { }); handler.on('begin', function (fileInfo) { - middleware.emit('begin', fileInfo); + middleware.emit('begin', fileInfo, req, res); }); handler.on('end', function (fileInfo) { - middleware.emit('end', fileInfo); + middleware.emit('end', fileInfo, req, res); }); handler.on('abort', function (fileInfo) { - middleware.emit('abort', fileInfo); + middleware.emit('abort', fileInfo, req, res); }); handler.on('error', function (e) { - middleware.emit('abort', e); + middleware.emit('abort', e, req, res); }); handler.on('delete', function (fileName) { - middleware.emit('delete', fileName); + middleware.emit('delete', fileName, req, res); }); switch (req.method) { diff --git a/package.json b/package.json index 85066a5..f2ca10e 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "express", "middleware" ], - "version": "0.1.4", + "version": "0.1.5", "dependencies": { "formidable": ">=1.0.11", "imagemagick": ">=0.1.2", @@ -29,6 +29,6 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "_id": "jquery-file-upload-middleware@0.1.4", + "_id": "jquery-file-upload-middleware@0.1.5", "license": "MIT" } From 73308d1df716d071c596cdb7be65fa34d2631c44 Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Sat, 2 Aug 2014 21:24:57 +0300 Subject: [PATCH 29/38] redundant code removed --- lib/filehandler.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/filehandler.js b/lib/filehandler.js index 2c75847..3b6b625 100644 --- a/lib/filehandler.js +++ b/lib/filehandler.js @@ -1,7 +1,6 @@ module.exports = function (middleware, options) { return function (req, res, next) { - this.req = req; res.set({ 'Access-Control-Allow-Origin': options.accessControl.allowOrigin, 'Access-Control-Allow-Methods': options.accessControl.allowMethods From e43100a92bceca398a5633ef522558dda7eb8f85 Mon Sep 17 00:00:00 2001 From: "Sebastian.Just" Date: Thu, 4 Sep 2014 14:27:49 -0400 Subject: [PATCH 30/38] Avoid path traversal with "../"-filenames --- lib/uploadhandler.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index ea4b2ec..71f665a 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -174,8 +174,20 @@ module.exports = function (options) { var self = this, fileName = path.basename(decodeURIComponent(this.req.url)); - fs.unlink(options.uploadDir() + '/' + fileName, function (ex) { + var filepath = path.join(options.uploadDir(), fileName); + if (filepath.indexOf(options.uploadDir()) !== 0) { + self.emit('delete', fileName); + self.callback({success: false}); + return; + } + fs.unlink(filepath, function (ex) { _.each(options.imageVersions, function (value, version) { + var versionfilepath = path.join(options.uploadDir(), version, fileName); + if (versionfilepath.indexOf(options.uploadDir()) !== 0) { + self.emit('delete', fileName); + self.callback({success: false}); + return; + } fs.unlink(options.uploadDir() + '/' + version + '/' + fileName); }); self.emit('delete', fileName); From fbc30647a864742e8ca8db6cc0c8ef9bbeb740e0 Mon Sep 17 00:00:00 2001 From: "Sebastian.Just" Date: Thu, 4 Sep 2014 14:27:49 -0400 Subject: [PATCH 31/38] Avoid path traversal with "../"-filenames --- lib/uploadhandler.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index ea4b2ec..3233e86 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -174,9 +174,21 @@ module.exports = function (options) { var self = this, fileName = path.basename(decodeURIComponent(this.req.url)); - fs.unlink(options.uploadDir() + '/' + fileName, function (ex) { + var filepath = path.join(options.uploadDir(), fileName); + if (filepath.indexOf(options.uploadDir()) !== 0) { + self.emit('delete', fileName); + self.callback({success: false}); + return; + } + fs.unlink(filepath, function (ex) { _.each(options.imageVersions, function (value, version) { - fs.unlink(options.uploadDir() + '/' + version + '/' + fileName); + var versionfilepath = path.join(options.uploadDir(), version, fileName); + if (versionfilepath.indexOf(options.uploadDir()) !== 0) { + self.emit('delete', fileName); + self.callback({success: false}); + return; + } + fs.unlink(versionfilepath); }); self.emit('delete', fileName); self.callback({success: !ex}); From 17b0a5f64b8fdfda4bcf30f814ae0f902e6758fd Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Thu, 4 Sep 2014 21:55:16 +0300 Subject: [PATCH 32/38] redundant checks removed --- lib/uploadhandler.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 3233e86..6a9fb34 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -177,18 +177,12 @@ module.exports = function (options) { var filepath = path.join(options.uploadDir(), fileName); if (filepath.indexOf(options.uploadDir()) !== 0) { self.emit('delete', fileName); - self.callback({success: false}); + self.callback({success: false}); return; } fs.unlink(filepath, function (ex) { _.each(options.imageVersions, function (value, version) { - var versionfilepath = path.join(options.uploadDir(), version, fileName); - if (versionfilepath.indexOf(options.uploadDir()) !== 0) { - self.emit('delete', fileName); - self.callback({success: false}); - return; - } - fs.unlink(versionfilepath); + fs.unlink(path.join(options.uploadDir(), version, fileName)); }); self.emit('delete', fileName); self.callback({success: !ex}); From abde574d030a79bca2d35435bf748e62a0954c3d Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Thu, 4 Sep 2014 21:57:24 +0300 Subject: [PATCH 33/38] changelog updated --- History.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/History.md b/History.md index db5ea58..371bece 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +0.1.6 / 2014-10-04 +================== + + * #56 avoid path traversal in DELETE requests + 0.1.5 / 2014-08-02 ================== From d15f5dc85fa34308bcd030f84d7dbcc007f2050d Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Thu, 4 Sep 2014 21:57:34 +0300 Subject: [PATCH 34/38] contributors updated --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 280d0bd..204c7a3 100644 --- a/README.md +++ b/README.md @@ -280,6 +280,7 @@ Other options and their default values: * [@gsarwohadi](https://github.com/gsarwohadi) * [@peecky](https://github.com/peecky) * [@tonyspiro](https://github.com/tonyspiro) + * [@derjust](https://github.com/derjust) ## License Copyright (c) 2012 [Aleksandr Guidrevitch](http://aguidrevitch.blogspot.com/) From 82843b150b020ed7d0c1d58eb799db8ca11ccc08 Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Thu, 4 Sep 2014 21:58:04 +0300 Subject: [PATCH 35/38] v0.1.6 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f2ca10e..f15d1ea 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "express", "middleware" ], - "version": "0.1.5", + "version": "0.1.6", "dependencies": { "formidable": ">=1.0.11", "imagemagick": ">=0.1.2", @@ -29,6 +29,6 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "_id": "jquery-file-upload-middleware@0.1.5", + "_id": "jquery-file-upload-middleware@0.1.6", "license": "MIT" } From feb57b257010654be7fb77af4c78834cd9a9b4cf Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Mon, 8 Sep 2014 16:21:35 +0300 Subject: [PATCH 36/38] IE fix --- lib/uploadhandler.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/uploadhandler.js b/lib/uploadhandler.js index 6a9fb34..070f018 100644 --- a/lib/uploadhandler.js +++ b/lib/uploadhandler.js @@ -26,9 +26,16 @@ module.exports = function (options) { UploadHandler.prototype.noCache = function () { this.res.set({ 'Pragma': 'no-cache', - 'Cache-Control': 'no-store, no-cache, must-revalidate', - 'Content-Disposition': 'inline; filename="files.json"' + 'Cache-Control': 'no-store, no-cache, must-revalidate' }); + if ((this.req.get("Accept") || "").indexOf("application/json") != -1) { + this.res.set({ + 'Content-Type': 'application/json', + 'Content-Disposition': 'inline; filename="files.json"' + }); + } else { + this.res.set({ 'Content-Type': 'text/plain' }); + } }; UploadHandler.prototype.get = function () { From efe60406ecaf90aed8fb9c815a867189850e248a Mon Sep 17 00:00:00 2001 From: Aleksandr Guidrevitch Date: Tue, 9 Sep 2014 23:11:57 +0300 Subject: [PATCH 37/38] fix for #58 --- History.md | 5 +++++ package.json | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 371bece..8a03709 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +0.1.7 / 2014-10-09 +================== + + * #58 improved support for IE9 + 0.1.6 / 2014-10-04 ================== diff --git a/package.json b/package.json index f15d1ea..dc11e0c 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "express", "middleware" ], - "version": "0.1.6", + "version": "0.1.7", "dependencies": { "formidable": ">=1.0.11", "imagemagick": ">=0.1.2", @@ -29,6 +29,6 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "_id": "jquery-file-upload-middleware@0.1.6", + "_id": "jquery-file-upload-middleware@0.1.7", "license": "MIT" } From 076644f6a7e360d63335a14530e17ec4b9b9afdf Mon Sep 17 00:00:00 2001 From: mberlanda Date: Thu, 25 Aug 2016 16:39:28 +0200 Subject: [PATCH 38/38] Update filehandler.js express deprecated res.json(status, obj): Use res.status(status).json(obj) instead node_modules/jquery-file-upload-middleware/lib/filehandler.js:19:21 --- lib/filehandler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/filehandler.js b/lib/filehandler.js index 3b6b625..e7e227a 100644 --- a/lib/filehandler.js +++ b/lib/filehandler.js @@ -16,7 +16,7 @@ module.exports = function (middleware, options) { ? 'application/json' : 'text/plain' }); - res.json(200, result); + res.status(200).json(result); } });