-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·77 lines (60 loc) · 2.06 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var express = require('express'),
morgan = require('morgan'),
busboy = require('connect-busboy'),
fs = require('fs'),
sleep = require('sleep'),
ImageTypes = require('./ImageTypes'),
multiparty = require('multiparty'),
exec = require('child_process').exec,
util = require('util'),
moment = require('moment');
var app = express(),
PORT = 3000;
/* hack but I'm not going to waist time on mkdirs */
exec ('mkdir -p ./uploads', function (error, stdout, stderr) {
if (error !== null) {
console.error('exec error: ' + error);
}
});
app.use(express.static('public'))
app.use(morgan('dev'));
app.set('view engine', 'dot' );
app.engine('dot', require('express-dot').__express );
app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
});
app.route('/')
.get(function(req, res, next) {
res.render('form', { layout: false });
});
app.route('/upload')
.post(handleUpload);
app.route('/upload/:user')
.post(handleUpload);
app.route('//upload/:user')
.post(handleUpload);
function handleUpload(req, res, next){
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
files.file.forEach (function (file) {
saveFile(file, req.params.user);
});
// sleep.sleep(4);
res.setHeader('Content-Type', 'text/plain');
// res.render('success', { layout: false});
res.end(util.inspect({fields: fields, files: files}));
});
}
function saveFile(file, name) {
var fileData = fs.readFileSync(file.path),
savePath = getSavePath(file, name);
console.log("Saving in " + savePath);
fs.writeFileSync(savePath, fileData);
console.log("...done.");
}
function getSavePath(file, name) {
return (__dirname + '/uploads/' + name + '-' + moment().format("YYYY-MM-DD-HH-mm") + '-' + file.originalFilename);
}
app.listen(PORT);
console.log('Listening on port ' + PORT);