waibu 1.2.8 → 1.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/handle-download.js +17 -0
- package/package.json +1 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
|
|
3
|
+
async function handleDownload (input, req, reply) {
|
|
4
|
+
const { importPkg } = this.app.bajo
|
|
5
|
+
const { isFunction } = this.lib._
|
|
6
|
+
const mime = await importPkg('waibu:mime')
|
|
7
|
+
const { fs } = this.lib
|
|
8
|
+
const file = isFunction(input) ? (await input.call(this, req)) : input
|
|
9
|
+
if (!fs.existsSync(file)) throw this.error('_notFound')
|
|
10
|
+
const mimeType = mime.getType(path.extname(file))
|
|
11
|
+
reply.header('Content-Type', mimeType)
|
|
12
|
+
const stream = fs.createReadStream(file)
|
|
13
|
+
reply.send(stream)
|
|
14
|
+
return reply
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default handleDownload
|