forked from briangreenery/share
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
77 lines (61 loc) · 1.85 KB
/
server.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 Busboy = require('busboy'),
debug = require('debug')('share'),
express = require('express'),
exphbs = require('express3-handlebars'),
fs = require('fs'),
path = require('path'),
Q = require('q'),
sanitize = require('sanitize-filename');
var tmpDir = path.join(__dirname, 'tmp'),
uploadsDir = path.join(__dirname, 'uploads'),
app = express(),
port = process.env.PORT || 3000;
function randomName(length) {
var i,
name = '',
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (i = 0; i < length; i++) {
name += alphabet[Math.floor(Math.random() * alphabet.length)];
}
return name;
}
if (!fs.existsSync(tmpDir))
fs.mkdirSync(tmpDir);
if (!fs.existsSync(uploadsDir))
fs.mkdirSync(uploadsDir);
app.disable('x-powered-by');
app.engine('hbs', exphbs({defaultLayout: 'main.hbs'}));
app.set('view engine', 'hbs');
app.use('/share', express.static(uploadsDir));
app.use('/share', express.static(path.join(__dirname, 'public')));
app.get('/share', function(req, res) {
res.render('home');
});
app.post('/share/upload', function(req, res) {
var folder = randomName(5),
name = '',
busboy = new Busboy({headers: req.headers});
busboy.on('file', function(fieldname, file, filename) {
name = sanitize(filename);
file.pipe(fs.createWriteStream(path.join(tmpDir, folder)));
});
busboy.on('finish', function() {
Q.nfcall(fs.mkdir, path.join(uploadsDir, folder))
.then(function() {
return Q.nfcall(fs.rename,
path.join(tmpDir, folder),
path.join(uploadsDir, folder, name));
})
.then(function() {
res.send(folder + '/' + encodeURIComponent(name));
})
.done();
});
req.pipe(busboy);
});
app.use(function(req, res, next) {
res.status(404);
res.render('404');
});
app.listen(port);
debug('Listening on port %d', port);