Skip to content

Commit

Permalink
0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
aguidrevitch committed Dec 20, 2012
1 parent e992b2e commit 1b2a107
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 111 deletions.
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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
==================

Expand Down
90 changes: 74 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
});
});
```
Expand All @@ -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
Expand Down
96 changes: 4 additions & 92 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();

53 changes: 53 additions & 0 deletions lib/filehandler.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
Loading

0 comments on commit 1b2a107

Please sign in to comment.