1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-10 23:53:31 -05:00
shimapan/app/routes/view.js
2017-10-11 20:26:07 -04:00

33 lines
974 B
JavaScript

var express = require('express');
var router = express.Router();
var fs = require('fs');
var mongoose = require('mongoose');
var Upload = mongoose.model('Upload');
router.get('/:name', function(req, res, next) {
Upload.findOne({
'name': req.params.name
}, function(err, upload) {
if (err) {
next(err);
} else {
if (!upload) {
res.sendStatus(404);
} else {
var disposition;
if (upload.file.mimetype.split('/')[0] === 'image')
disposition = 'inline';
else
disposition = 'attachment; filename="' + upload.oname + '"';
res.set({
"Content-Disposition": disposition,
"Content-Type": upload.file.mimetype
});
fs.createReadStream(upload.file.path).pipe(res);
}
}
});
});
module.exports = router;