Created
July 3, 2019 21:11
-
-
Save kmaher9/5c2793b0c8751bbaa5fc8920fdb2f2dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const File = require('../models/file') | |
const fs = require('fs') | |
exports.getFile = async function (request, response) { | |
let file = null | |
try { file = await File.findById(request.params.id) } catch (err) {} | |
if (!file) { | |
return response.status(404).json({ | |
"data": { | |
"error": "unable to find requested file" | |
} | |
}) | |
} | |
var stat = fs.statSync(file.location) | |
response.writeHead(200, { | |
'Content-Type': file.mime, | |
'Content-Length': stat.size, | |
'Content-Disposition': `attachment;filename=${file.name}` | |
}) | |
let stream = fs.createReadStream(file.location) | |
stream.pipe(response) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment