1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-14 08:49:51 -05:00
shimapan/app/routes/view.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-10-09 22:01:02 -04:00
var express = require('express');
var router = express.Router();
var fs = require('fs');
2017-10-11 10:15:19 -04:00
var mongoose = require('mongoose');
var Upload = mongoose.model('Upload');
2017-10-09 22:01:02 -04:00
2017-10-14 15:13:50 -04:00
function addView(name) {
Upload.updateOne({name: name}, { $inc: { views: 1 } }, function(err) {
if (err) throw err;
});
}
2017-10-09 22:01:02 -04:00
router.get('/:name', function(req, res, next) {
Upload.findOne({
2017-10-14 15:13:50 -04:00
name: req.params.name
2017-10-09 22:01:02 -04:00
}, function(err, upload) {
if (err) {
next(err);
} else {
if (!upload) {
res.sendStatus(404);
} else {
2017-10-14 15:13:50 -04:00
addView(upload.name);
2017-10-09 22:01:02 -04:00
var disposition;
if (upload.file.mimetype.split('/')[0] === 'image')
disposition = 'inline';
else
2017-10-14 15:13:50 -04:00
disposition = 'attachment; filename="' + upload.file.originalname + '"';
2017-10-09 22:01:02 -04:00
res.set({
"Content-Disposition": disposition,
"Content-Type": upload.file.mimetype
});
fs.createReadStream(upload.file.path).pipe(res);
}
}
});
});
module.exports = router;