-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.js
174 lines (156 loc) · 6.39 KB
/
index.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const debug = require('debug')('qcloud-sdk[CosUploader]')
const multiparty = require('multiparty')
const readChunk = require('read-chunk')
const shortid = require('shortid')
const fs = require('fs')
const fileType = require('file-type')
const CosSdk = require('cos-nodejs-sdk-v5')
const ERRORS = require('../constants').ERRORS
const config = require('../../config')
const regionMap = {
'ap-beijing-1': 'tj',
'ap-beijing': 'bj',
'ap-shanghai': 'sh',
'ap-guangzhou': 'gz',
'ap-chengdu': 'cd',
'ap-singapore': 'sgp',
'ap-hongkong': 'hk',
'na-toronto': 'ca',
'eu-frankfurt': 'ger'
}
/**
* 对象上传 API
* @param {express request} req
* @return {Promise} 上传任务的 Promise 对象
*/
module.exports = (req) => {
// 初始化 sdk
const cos = new CosSdk({
AppId: config.qcloudAppId,
SecretId: config.qcloudSecretId,
SecretKey: config.qcloudSecretKey,
Domain: `http://${config.cos.fileBucket}-${config.qcloudAppId}.cos.${config.cos.region}.myqcloud.com/`
})
const maxSize = config.cos.maxSize ? config.cos.maxSize : 10
const fieldName = config.cos.fieldName ? config.cos.fieldName : 'file'
debug('Cos sdk init finished')
// 初始化 multiparty
const form = new multiparty.Form({
encoding: 'utf8',
maxFilesSize: maxSize * 1024 * 1024,
autoFiles: true,
uploadDir: '/tmp'
})
return new Promise((resolve, reject) => {
// 从 req 读取文件
form.parse(req, (err, fields = {}, files = {}) => {
err ? reject(err) : resolve({fields, files})
})
}).then(({files}) => {
if (!(fieldName in files)) {
debug('%s: 请求中没有名称为 %s 的field,请检查请求或 SDK 初始化配置', ERRORS.ERR_REQUEST_LOST_FIELD, fieldName)
throw new Error(ERRORS.ERR_REQUEST_LOST_FIELD)
}
const imageFile = files.file[0]
/**
* 判断文件类型
* 为保证安全默认支持的文件类型有:
* 图片:jpg jpg2000 git bmp png
* 音频:mp3 m4a
* 文件:pdf
*/
const buffer = readChunk.sync(imageFile.path, 0, 262)
let resultType = fileType(buffer)
// 如果无法获取文件的 MIME TYPE 就取 headers 里面的 content-type
if (!resultType && imageFile.headers && imageFile.headers['content-type']) {
const tmpPathArr = imageFile.path ? imageFile.path.split('.') : []
const extName = tmpPathArr.length > 0 ? tmpPathArr[tmpPathArr.length - 1] : ''
resultType = {
mime: imageFile.headers['content-type'],
ext: extName
}
}
const allowMimeTypes = config.cos.mimetypes
? config.cos.mimetypes
: ['image/jpeg', 'image/jp2', 'image/jpm', 'image/jpx', 'image/gif', 'image/bmp', 'image/png', 'audio/mpeg', 'audio/mp3', 'audio/m4a', 'application/pdf']
if (!resultType || !allowMimeTypes.includes(resultType.mime)) {
debug('%s: 不支持类型的文件', ERRORS.ERR_UNSUPPORT_FILE_TYPE, imageFile)
throw new Error(ERRORS.ERR_UNSUPPORT_FILE_TYPE)
}
// 生成上传参数
const srcpath = imageFile.path
const imgKey = `${Date.now()}-${shortid.generate()}` + (resultType.ext ? `.${resultType.ext}` : '')
const uploadFolder = config.cos.uploadFolder ? config.cos.uploadFolder + '/' : ''
const params = {
Bucket: config.cos.fileBucket,
Region: config.cos.region,
Key: `${uploadFolder}${imgKey}`,
Body: fs.createReadStream(srcpath),
ContentLength: imageFile.size
}
return new Promise((resolve, reject) => {
// 检查 bucket 是否存在,不存在则创建 bucket
cos.getService(params, (err, data) => {
if (err) {
reject(err)
// remove uploaded file
fs.unlink(srcpath, () => {})
return
}
// 检查提供的 Bucket 是否存在
const hasBucket = data.Buckets && data.Buckets.reduce((pre, cur) => {
return pre || cur.Name === `${config.cos.fileBucket}-${config.qcloudAppId}`
}, false)
if (data.Buckets && !hasBucket) {
cos.putBucket({
Bucket: config.cos.fileBucket,
Region: config.cos.region,
ACL: 'public-read'
}, function (err, data) {
if (err) {
reject(err)
// remove uploaded file
fs.unlink(srcpath, () => {})
return
}
resolve()
})
}
resolve()
})
}).then(() => {
return new Promise((resolve, reject) => {
// 上传图片
cos.putObject(params, (err, data) => {
if (err) {
reject(err)
// remove uploaded file
fs.unlink(srcpath, () => {})
return
}
resolve({
imgUrl: `https://${config.cos.fileBucket}-${config.qcloudAppId}.cos.${config.cos.region}.myqcloud.com/${uploadFolder}${imgKey}`,
imgUrlv4: `http://${config.cos.fileBucket}-${config.qcloudAppId}.cos${regionMap[config.cos.region]}.myqcloud.com/${uploadFolder}${imgKey}`,
size: imageFile.size,
mimeType: resultType.mime,
name: imgKey,
fileBucket: config.cos.fileBucket,
qcloudAppId: config.qcloudAppId,
region: config.cos.region,
uploadFolder,
imgKey
})
// remove uploaded file
fs.unlink(srcpath, () => {})
})
})
})
}).catch(e => {
if (e.statusCode === 413) {
debug('%s: %o', ERRORS.ERR_FILE_EXCEEDS_MAX_SIZE, e)
throw new Error(`${ERRORS.ERR_FILE_EXCEEDS_MAX_SIZE}\n${e}`)
} else {
throw e
}
})
}