This repository has been archived by the owner on Jun 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fineUploaderLocalMiddleware.js
75 lines (65 loc) · 1.88 KB
/
fineUploaderLocalMiddleware.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
const config = require('./conf.js'),
concat = require('concat-files'),
fs = require('fs'),
express = require('express'),
mkdirp = require('mkdirp'),
multer = require('multer'),
bodyParser = require('body-parser');
function directory(req) {
return `${config.UPLOAD_DESTINATION}/${req.body.uuid}`;
}
function filename(req, i) {
return `${req.body.qqfilename}.${i !== undefined ? i : req.body.qqpartindex}`;
}
function combineChunks(req, res) {
let filenames = [];
for (let i = 0; i < req.body.qqtotalparts; i++)
filenames.push(directory(req) + "/" + filename(req, i));
console.log('combine', filenames);
concat(filenames,
directory(req) + "/" + req.body.qqfilename,
function(err) {
if (err)
res.json({'error': 'failed to combine chunks'});
else {
for (let fn of filenames)
fs.unlink(fn, console.log);
res.send(200);
}
});
}
var storage = multer.diskStorage({
destination: function (req, file, cb) {
const dir = directory(req);
mkdirp(dir, function(err) {
if (err)
cb(err, dir);
else
cb(null, dir);
});
},
filename: function (req, file, cb) {
cb(null, filename(req));
}
});
const upload = multer({ storage }).single('qqfile');
function fineUploaderLocalMiddleware(options) {
var router = express.Router();
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());
router.post('/', function (req, res) {
upload(req, res, function (err) {
console.log('upload', req.body);
if (err) {
console.log(err);
// TODO specify error
res.json({success: false});
} else {
res.json({success: true});
}
})
});
router.post('/success', combineChunks);
return router;
}
module.exports = fineUploaderLocalMiddleware;