Created
September 28, 2011 14:19
-
-
Save diversario/1248061 to your computer and use it in GitHub Desktop.
connect-formaline example 2
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
#!/usr/bin/env node | |
var express = require('express'), | |
app = express.createServer(), // actual application | |
UPLOAD_LIMIT = 20 * 1024 * 1024, | |
form = require('./connect-formaline'), | |
formaline_config = ({"uploadRootDir": '/tmp/', "emitProgress": true, "requestTimeOut": 600000, "resumeRequestOnError": false, "maxFileSize": UPLOAD_LIMIT, "checkContentLength": true, "holdFilesExtensions": true, "removeIncompleteFiles": true, "logging": 'debug:on,1:on,2:on,3:off,4:off,console:on,file:off,record:off'}); | |
app | |
.use(express.cookieParser()) | |
.use(express.session({ secret : 'yay', cookie: {path: '/', httpOnly: true, maxAge: 604800000}})) | |
.use(express.bodyParser()) | |
.use(express.static(publicdir, { maxAge : 864000000 })) | |
.use(form(formaline_config)) | |
.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
app.post('/upload', function (req, res, next){ | |
var complete = function (json, res){ | |
console.log(json); | |
if (json.files.length > 0){ //if there are files to work with | |
var file = json.files[0].value[0]; | |
validateUpload(file, function(r, mime, type){ | |
if (r){ | |
console.log('\nuploaded %s to %s', file.name, file.path); | |
console.log('done uploading'); | |
/* flattening fields array into one object */ | |
var fields = {}; | |
for (var i =0; i<json.fields.length; i++){ | |
fields[json.fields[i].name] = json.fields[i].value[0]; | |
} | |
try { | |
fs.readFile(file.path, function (err, data) { | |
var body = { | |
"image" : data, | |
"path" : file.path, | |
"mime" : mime, | |
"type": type, | |
"eid": req.params.eid, | |
"fields": fields | |
}; | |
db.uploadFile(body, res); | |
}); | |
} catch (error) { | |
res.statusCode = 500; | |
res.end('{"error":"' + error + '", "status_code": 500}'); | |
} | |
} else { | |
res.statusCode = 400; | |
res.end('{"error":"uploaded file is not of allowed type", "status_code": 400}'); | |
} | |
}); | |
} else { | |
res.end('{"error":"upload failed", "status_code": 500}'); | |
//console.log('why is this shit!'); | |
} | |
} | |
req.form.on('error', function(json){ | |
console.log('\033[1;31merror\033[0m', json); | |
}); | |
req.form.on('loadstart', function(json){ | |
console.log(json); | |
}); | |
req.form.on('message', function(message){ | |
console.log('\033[1;31mmessage\033[0m', message); | |
if (message.type === 'fileremoved'){ | |
res.end('File size exceeds maximum allowed size of ' + (UPLOAD_LIMIT/(1024*1024)).toFixed(2) + ' MB.'); | |
} | |
}); | |
req.form.on('progress', function(json){ | |
console.log(json.ratio, " | ", json.bytes); | |
}); | |
req.form.on('loadend', function(json, res){ | |
console.log('loadend emmited'); | |
complete(json, res); | |
}); | |
}); | |
app.listen(80, "127.0.0.1"); |
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 formaline = require('formaline'); | |
/** | |
* Setup form with the given `options`. | |
* | |
* For options refer to formaline documentation. | |
* | |
* @param {Object} options Formaline options | |
* @return {Function} | |
* @api public | |
*/ | |
module.exports = function (options){ | |
return function(req, res, next){ | |
if (formRequest(req)){ | |
var form = req.form = new formaline(options); | |
//form.on( 'loadend', function ( req, res, next ) { | |
//next(); | |
//} ); | |
form.parse(req, res); | |
next(); | |
} else next(); | |
} | |
}; | |
/** | |
* Check if `req` is a valid form request. | |
* | |
* @param {IncomingMessage} req | |
* @return {Boolean} | |
* @api private | |
* @author TJ Holowaychuk <tj@vision-media.ca> | |
*/ | |
function formRequest(req) { | |
var contentType = req.headers['content-type']; | |
if (!contentType) return; | |
return req.body === undefined | |
&& (req.method === 'POST' | |
|| req.method === 'PUT') | |
&& (~contentType.indexOf('multipart/form-data') | |
|| ~contentType.indexOf('urlencoded')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment