Created
July 3, 2019 21:43
-
-
Save kmaher9/a5260e29d39ee3c3e8fc7fe4a7a558e2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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) | |
} | |
exports.getFiles = async function (request, response) { | |
let files = await File.find({}) | |
if (!files) { | |
return response.status(404).json({ | |
"data": { | |
"message": "There are no uploaded files" | |
} | |
}) | |
} | |
return response.status(200).json({ | |
"data": { | |
"files": files | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment