-
-
Save jorinvo/6410640 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var IncomingForm = require('formidable'); | |
var util = require('util'); | |
var EventEmitter = require('events').EventEmitter; | |
var GridStore = require('mongodb').GridStore; | |
var pauseStream = require('pause-stream'); | |
var ObjectID = require('mongodb').ObjectID; | |
function IncomingFormGridFS(options) { | |
if(!(this instanceof IncomingFormGridFS)) return new IncomingFormGridFS(options); | |
EventEmitter.call(this); | |
// provide defaults for all options | |
var defaults = { | |
mongodbConnection: 'mongodb://localhost:27017/mylocation', | |
error: false, | |
ended: false, | |
maxFields: 1000, | |
maxFieldsSize: 2 * 1024 * 1024, | |
keepExtensions: false, | |
uploadDir: '/tmp', | |
encoding: 'utf-8', | |
headers: null, | |
type: null, | |
hash false | |
bytesReceived: null, | |
bytesExpected: null, | |
_parser: null, | |
_flushing: 0, | |
_fieldsSize: 0, | |
openedFiles: [] | |
}; | |
options = options || {}; | |
// extend "this" with options and | |
// overwrite defaults with provided options | |
for (var val in defaults) { | |
this[val] = options[val] || defaults[val]; | |
} | |
return this; | |
} | |
util.inherits(IncomingFormGridFS, IncomingForm); | |
IncomingFormGridFS.prototype.handlePart = function(part) { | |
var self=this; | |
if(!part.filename) return self.handlePart(part); | |
var pStream = pauseStream(); | |
part.pipe(pStream.pause()); | |
var fileMeta = { | |
name: part.filename, | |
path: 'void', // TODO | |
type: part.mime | |
}; | |
self.openedFiles.push(fileMeta); | |
self.emit('fileBegin', part.name, fileMeta); | |
new GridStore(self.mongodbConnection, new ObjectID(), fileMeta.name , 'w', { | |
metadata: { | |
filename: fileMeta.name | |
}, | |
content_type: part.mime | |
}).open(function(err, gridFile) { | |
self._flushing++; | |
pStream.on('data', function(buffer) { | |
pStream.pause(); | |
gridFile.write(buffer, function(err) { | |
if(err) console.error(err); | |
pStream.resume(); | |
}); | |
}); | |
pStream.on('end', function() { | |
gridFile.close(function(err, fileData) { | |
fileMeta.hash = fileData.md5; | |
fileMeta.length = fileData.length; | |
fileMeta._id = fileData._id; | |
fileMeta.uploadDate = fileData.uploadDate; | |
self._flushing--; | |
self.emit('file', part.name, fileMeta); | |
self._maybeEnd(); | |
}); | |
}); | |
pStream.resume(); | |
}); | |
}; | |
IncomingFormGridFS.prototype._error = function(err) { | |
if(this.error || this.ended) { | |
return; | |
} | |
console.log('gridfs incfrm _ERROR '); | |
this.error = err; | |
this.pause(); | |
this.emit('error', err); | |
// no need to delete files, GridFS kicks it when we dont close the stream probably | |
}; | |
module.exports = IncomingFormGridFS; |
return this
würde ich auch weglassen.
(https://gist.github.com/jorin-vogel/6410640#file-formidablegridfs-js-L43)
mongodbConnection: 'mongodb://localhost:27017/mylocation',
Ist falsch, GridStore braucht ja direkt ne connection-resource und nicht die URI
die anderen zwei sachen waren copy-paste von Formidable
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Versteh nicht ganz was das soll
EventEmitter.call(this);
.(https://gist.github.com/jorin-vogel/6410640#file-formidablegridfs-js-L11)
Kannste mich aufklären?