/
fileUploader.js
80 lines (73 loc) · 2.06 KB
/
fileUploader.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
import fs from 'fs'
import path from 'path'
function pathStr(key) {
const date = new Date()
const str = key
? path.join(__dirname, 'upload', 'temp', `${key}`)
: path.join(__dirname, 'upload', String(date.getFullYear()), String(date.getMonth() + 1), String(date.getDate()))
return str
}
export async function fileInfoUploader(body) {
try {
const { key } = body
const _path = pathStr(key)
await fs.mkdirSync(_path, { recursive: true })
return true
} catch (error) {
console.log(error)
return false
}
}
export async function fileEndUploader(body) {
try {
const { key, totalStep, filename } = body
const tempPath = pathStr(key)
const chunkPaths = []
for (let i = 1; i <= totalStep; i++) {
chunkPaths.push(path.join( tempPath, String(i) ))
}
// 采用Stream方式合并
const filePath = pathStr()
await fs.mkdirSync(filePath, { recursive: true })
const targetStream = fs.createWriteStream(path.join(filePath, `${key}_${filename}`))
const readStreamSingle = path => new Promise((reslove, reject) => {
const originStream = fs.createReadStream(path)
originStream.pipe(targetStream, { end: false })
originStream.on("end", async ()=> {
await fs.unlinkSync(path);
reslove()
})
})
for(let i = 0; i < chunkPaths.length; i++) {
await readStreamSingle(chunkPaths[i])
}
targetStream.end()
await fs.rmdirSync(tempPath);
return true
} catch (error) {
console.log(error)
return false
}
}
export async function fileDataUploader(body, key, step) {
try {
const data = Buffer.from(body.toString(), 'binary')
const _path = pathStr(key)
await fs.writeFileSync(path.join(_path, step), data)
return true
} catch (error) {
console.log(error)
return false
}
}
export async function fileUndoUploader(body) {
try {
const { filename, key } = body
const filePath = pathStr()
await fs.unlinkSync(path.join(filePath, `${key}_${filename}`))
return true
} catch (error) {
console.log(error)
return false
}
}