-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadFile.js
44 lines (39 loc) · 1.11 KB
/
uploadFile.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
const fs = require('fs')
const { STATICPATH } = require('./config')
/**
* 处理上传文件的接口
* @param {*} ctx
* @param {*} files 文件
*/
const uploadFile = (ctx, files) => {
const fileList = Object.values(files)
const { chunkIndex, fileHash } = ctx.request.body
let responseMsg = '', status = 200
if(fileList && fileList.length) {
// 对文件重命名
fileList.forEach(file => {
const oldNamePath = file.filepath.replace(/\\/g, '/')
let newNamePath = ''
if(chunkIndex !== undefined) {
// 大文件处理
const bigFileDirPath = `${STATICPATH}/${fileHash}`
newNamePath = `${bigFileDirPath}/${fileHash}_${chunkIndex}`
if(!fs.existsSync(bigFileDirPath)) {
fs.mkdirSync(bigFileDirPath)
}
}else {
newNamePath = `${STATICPATH}/${file.originalFilename}`
}
fs.renameSync(oldNamePath, newNamePath)
})
responseMsg = 'file upload success!'
}else {
status = 400
responseMsg = 'no file!'
}
ctx.response.status = status
ctx.body = {
msg: responseMsg
}
}
module.exports = uploadFile