Permalink
Browse files

0.0.6

  • Loading branch information...
1 parent e992b2e commit 1b2a107c585765d752875a278aa7cd02bcb7d29d @aguidrevitch committed Dec 20, 2012
Showing with 321 additions and 111 deletions.
  1. +6 −0 History.md
  2. +74 −16 README.md
  3. +4 −92 index.js
  4. +53 −0 lib/filehandler.js
  5. +181 −0 lib/filemanager.js
  6. +1 −1 lib/uploadhandler.js
  7. +2 −2 package.json
View
@@ -1,3 +1,9 @@
+0.0.6 / 2012-12-20
+==================
+
+ * #6 hostname option added
+ * upload.fileManager() added, which allows moving file
+
0.0.5 / 2012-12-12
==================
View
@@ -113,18 +113,74 @@ Dynamic upload directory and url, isolating user files:
});
```
-Getting uploaded files mapped to their fs locations:
+Moving uploaded files to different dir:
```javascript
- app.use('/list', function (req, res, next) {
- upload.getFiles({
- uploadDir: function () {
- return __dirname + '/public/uploads/' + req.sessionID
+ app.use('/api', function (req, res, next) {
+ req.filemanager = upload.fileManager();
+ next();
+ });
+
+ app.use('/api/endpoint', function (req, res, next) {
+ // your real /api handler that will actually move the file
+ ...
+ // req.filemanager.move(filename, path, function (err, result))
+ req.filemanager.move('SomeFile.jpg', 'project1', function (err, result) {
+ // SomeFile.jpg gets moved from uploadDir/SomeFile.jpg to
+ // uploadDir/project1/SomeFile.jpg
+ // if path is relative (no leading slash), uploadUrl will
+ // be used to generate relevant urls,
+ // for absolute paths urls are not generated
+ if (!err) {
+ // result structure
+ // {
+ // filename: 'SomeFile.jpg',
+ // url: '/uploads/project1/SomeFile.jpg',
+ // thumbail_url : '/uploads/project1/thumbnail/SomeFile.jpg'
+ // }
+ ...
+ } else {
+ console.log(err);
+ }
+ });
+ });
+```
+
+Moving uploaded files out of uploadDir:
+
+```
+ app.use('/api', function (req, res, next) {
+ var user = db.find(...);
+
+ req.filemanager = upload.fileManager({
+ targetDir: __dirname + '/public/u/' + user._id,
+ targetUrl: '/u/' + user._id,
+ });
+
+ // or
+ req.filemanager = upload.fileManager({
+ targetDir: function () {
+ return __dirname + '/public/u/' + user._id
},
- uploadUrl: function () {
- return '/uploads/' + req.sessionID
+ targetUrl: function () {
+ return'/u/' + user._id
+ }
+ });
+ ...
+ req.filemanager.move(req.body.filename, 'profile', function (err, result) {
+ // file gets moved to __dirname + '/public/u/' + user._id + '/profile'
+ if (!err) {
+
}
- }, function (files) {
+ });
+ });
+```
+
+Getting uploaded files mapped to their fs locations:
+
+```javascript
+ app.use('/list', function (req, res, next) {
+ upload.fileManager().getFiles(function (files) {
// {
// "00001.MTS": {
// "path": "/home/.../public/uploads/ekE6k4j9PyrGtcg+SA6a5za3/00001.MTS"
@@ -137,22 +193,19 @@ Getting uploaded files mapped to their fs locations:
res.json(files);
});
});
-```
-Passing uploaded files down the request chain:
+ // with dynamic upload directories
-```javascript
- app.use('/api', function (req, res, next) {
- upload.getFiles({
+ app.use('/list', function (req, res, next) {
+ upload.fileManager({
uploadDir: function () {
return __dirname + '/public/uploads/' + req.sessionID
},
uploadUrl: function () {
return '/uploads/' + req.sessionID
}
- }, function (files) {
- res.jquploadfiles = files;
- next();
+ }).getFiles(function (files) {
+ res.json(files);
});
});
```
@@ -164,6 +217,11 @@ Other options and their default values:
tmpDir: '/tmp',
uploadDir: __dirname + '/public/uploads',
uploadUrl: '/uploads',
+ targetDir: uploadDir,
+ targetUrl: uploadUrl,
+ ssl: false,
+ hostname: null, // in case your reverse proxy doesn't set Host header
+ // eg 'google.com'
maxPostSize: 11000000000, // 11 GB
minFileSize: 1,
maxFileSize: 10000000000, // 10 GB
View
@@ -1,61 +1,6 @@
var _ = require('lodash'),
- fs = require('fs');
+ EventEmitter = require('events').EventEmitter;
-var FileHandler = function (middleware, options, callback) {
-
- return function (req, res, next) {
- res.set({
- 'Access-Control-Allow-Origin': options.accessControl.allowOrigin,
- 'Access-Control-Allow-Methods': options.accessControl.allowMethods
- });
- var UploadHandler = require('./lib/uploadhandler')(options);
- var handler = new UploadHandler(req, res, function (result, redirect) {
- if (redirect) {
- res.redirect(redirect.replace(/%s/, encodeURIComponent(JSON.stringify(result))));
- } else {
- res.set({
- 'Content-Type': (req.headers.accept || '').indexOf('application/json') !== -1
- ? 'application/json'
- : 'text/plain'
- });
- res.json(200, result);
- }
- });
-
- handler.on('begin', function (fileInfo) {
- middleware.emit('begin', fileInfo);
- });
- handler.on('end', function (fileInfo) {
- middleware.emit('end', fileInfo);
- });
- handler.on('abort', function (fileInfo) {
- middleware.emit('abort', fileInfo);
- });
- handler.on('error', function (e) {
- middleware.emit('abort', e);
- });
-
- switch (req.method) {
- case 'OPTIONS':
- res.end();
- break;
- case 'HEAD':
- case 'GET':
- handler.get();
- break;
- case 'POST':
- handler.post();
- break;
- case 'DELETE':
- handler.destroy();
- break;
- default:
- res.send(405);
- }
- }
-};
-
-var EventEmitter = require('events').EventEmitter;
var JqueryFileUploadMiddleware = function () {
EventEmitter.call(this);
// setting default options
@@ -102,44 +47,11 @@ JqueryFileUploadMiddleware.prototype.configure = function (options) {
};
JqueryFileUploadMiddleware.prototype.fileHandler = function (options) {
- return FileHandler(this, this.prepareOptions(_.extend( this.options, options )));
+ return require('./lib/filehandler')(this, this.prepareOptions(_.extend(this.options, options)));
};
-JqueryFileUploadMiddleware.prototype.getFiles = function (options, callback) {
- if (_.isFunction(options)) {
- callback = options;
- options = this.options;
- } else {
- options = this.prepareOptions(_.extend( this.options, options ));
- }
-
- var files = {};
- var counter = 1;
- var finish = function () {
- if (!--counter)
- callback(files);
- };
-
- fs.readdir(options.uploadDir(), _.bind(function (err, list) {
- _.each(list, function (name) {
- var stats = fs.statSync(options.uploadDir() + '/' + name);
- if (stats.isFile()) {
- files[name] = {
- path: options.uploadDir() + '/' + name
- };
- _.each(options.imageVersions, function (value, version) {
- counter++;
- fs.exists(options.uploadDir() + '/' + version + '/' + name, function (exists) {
- if (exists)
- files[name][version] = options.uploadDir() + '/' + version + '/' + name;
- finish();
- });
- });
- }
- }, this);
- finish();
- }, this));
+JqueryFileUploadMiddleware.prototype.fileManager = function (options) {
+ return require('./lib/filemanager')(this, this.prepareOptions(_.extend(this.options, options)));
};
module.exports = new JqueryFileUploadMiddleware();
-
View
@@ -0,0 +1,53 @@
+module.exports = function (middleware, options) {
+
+ return function (req, res, next) {
+ res.set({
+ 'Access-Control-Allow-Origin': options.accessControl.allowOrigin,
+ 'Access-Control-Allow-Methods': options.accessControl.allowMethods
+ });
+ var UploadHandler = require('./uploadhandler')(options);
+ var handler = new UploadHandler(req, res, function (result, redirect) {
+ if (redirect) {
+ res.redirect(redirect.replace(/%s/, encodeURIComponent(JSON.stringify(result))));
+ } else {
+ res.set({
+ 'Content-Type': (req.headers.accept || '').indexOf('application/json') !== -1
+ ? 'application/json'
+ : 'text/plain'
+ });
+ res.json(200, result);
+ }
+ });
+
+ handler.on('begin', function (fileInfo) {
+ middleware.emit('begin', fileInfo);
+ });
+ handler.on('end', function (fileInfo) {
+ middleware.emit('end', fileInfo);
+ });
+ handler.on('abort', function (fileInfo) {
+ middleware.emit('abort', fileInfo);
+ });
+ handler.on('error', function (e) {
+ middleware.emit('abort', e);
+ });
+
+ switch (req.method) {
+ case 'OPTIONS':
+ res.end();
+ break;
+ case 'HEAD':
+ case 'GET':
+ handler.get();
+ break;
+ case 'POST':
+ handler.post();
+ break;
+ case 'DELETE':
+ handler.destroy();
+ break;
+ default:
+ res.send(405);
+ }
+ }
+};
Oops, something went wrong.

0 comments on commit 1b2a107

Please sign in to comment.