-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.js
More file actions
79 lines (75 loc) · 2.03 KB
/
file.js
File metadata and controls
79 lines (75 loc) · 2.03 KB
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
const path = require('path')
const fs = require('fs')
const mime = require('mime')
const PUBLIC_DIR = path.join(__dirname, 'public')
exports.send = function (filePath, res) {
let fileName
try {
fileName = decodeURIComponent(path.basename(filePath))
} catch (error) {
res.statusCode = 400
res.setHeader('Content-Type', ' text/html; charset=utf-8')
res.end('Bad request')
}
const pathDir = path.dirname(filePath)
let fileLocation
let attachment = false
if (pathDir.indexOf('/download') === 0) {
fileLocation = `./files/${fileName}`
attachment = true
} else {
fileLocation = path.normalize(path.join(PUBLIC_DIR, filePath))
if (fileLocation.indexOf(PUBLIC_DIR) !== 0) {
res.statusCode = 400
res.setHeader('Content-Type', ' text/html; charset=utf-8')
res.end('Bad request')
}
}
try {
sendFile(fileLocation, fileName, attachment, res)
} catch (error) {
console.log(error)
res.statusCode = 400
res.setHeader('Content-Type', ' text/html; charset=utf-8')
res.end('Bad request')
}
}
function sendFile (filePath, fileName, attachment, res) {
const file = new fs.ReadStream(filePath)
file.pipe(res)
file.on('open', () => {
// console.log('open')
const fileMime = mime.getType(filePath)
res.writeHead(200, {
'Content-Type': fileMime + '; charset=utf-8'
})
if (attachment) {
res.writeHead(200, {
'Content-Disposition': 'attachment; filename=' + fileName
})
}
})
file.on('error', (err) => {
if (err.code === 'ENOENT') {
res.statusCode = 404
res.setHeader('Content-Type', ' text/html; charset=utf-8')
res.end(`File ${fileName} not found`)
console.log(err.code)
} else {
res.statusCode = 500
res.end('Server Error')
console.log(err.code)
}
})
file.on('close', () => {
// console.log('file close')
})
res.on('close', () => {
// console.log('res close')
file.destroy()
})
res.on('error', () => {
console.log('res error')
file.destroy()
})
}