-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextract.js
67 lines (57 loc) · 2.01 KB
/
extract.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
const fs = require('fs')
const path = require('path')
const Busboy = require('busboy')
const appendField = require('append-field')
module.exports = function (req, dest, fnDestFilename, opts = {}) {
return new Promise((resolve, reject) => {
let files = []
let fields = {}
let busboy = Busboy(Object.assign({}, opts, {headers: req.headers}))
busboy.on('file', (fieldname, fileStream, info) => {
const { filename, encoding, mimeType } = info;
if (!filename) return fileStream.resume()
if (opts.acceptMimeTypes.length > 0 && opts.acceptMimeTypes.indexOf(mimetype) < 0) {
appendField(fields, "uploadError", `UNSUPPORTED_MIMETYPE: ${mimetype}`)
return fileStream.resume();
}
files.push(new Promise(function (resolve, reject) {
let tmpName = fnDestFilename(fieldname, filename)
let tmpPath = path.join(dest, path.basename(tmpName))
fileStream.pipe(fs.createWriteStream(tmpPath))
.on('error', reject)
.on('close', () => {
let rs = fs.createReadStream(tmpPath)
rs.fieldname = fieldname
rs.filename = filename
rs.encoding = encoding
rs.mimetype = mimeType
resolve(rs)
})
}))
})
busboy.on('field', (name, value) => {
if (Object.getOwnPropertyDescriptor(Object.prototype, name)) {
name = '_' + name
}
appendField(fields, name, value)
})
busboy.on('close', () => {
if (files.length) {
Promise.all(files)
.then(files => resolve({ fields, files }))
.catch(reject)
} else {
resolve({ fields, files })
}
})
busboy.on('error', reject)
busboy.on('partsLimit', () => reject(new Error('LIMIT_PART_COUNT')))
busboy.on('filesLimit', () => reject(new Error('LIMIT_FILE_COUNT')))
busboy.on('fieldsLimit', () => reject(new Error('LIMIT_FIELD_COUNT')))
if (req.rawBody) {
busboy.end(req.rawBody)
} else {
req.pipe(busboy);
}
})
}