-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.js
26 lines (25 loc) · 805 Bytes
/
files.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
'use static'
let formParse = require('co-busboy');
let fs = require('fs');
let path = require('path');
let config = require('../config/config.js');
let qiniu = require('../util/qiniu.js');
module.exports.upload = function *() {
let part;
let parts = formParse(this, {autoFields: true});
let filesPath = [];
while (part = yield parts) {
let fileName = part.filename;
let tmpPath = path.resolve(__dirname, `../${config.uploadDir}`) + '/' + fileName;
let stream = fs.createWriteStream(tmpPath); //stream.path
part.pipe(stream);
try {
let result = yield qiniu.uploadFile(fileName, stream.path);
fs.unlink(tmpPath);
filesPath.push(result.key);
} catch (error) {
console.log(error);
}
}
this.body = filesPath;
}