1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-14 00:46:38 -05:00
shimapan/app/routes/view.js
2017-10-14 15:13:50 -04:00

41 lines
1.1 KiB
JavaScript

var express = require('express');
var router = express.Router();
var fs = require('fs');
var mongoose = require('mongoose');
var Upload = mongoose.model('Upload');
function addView(name) {
Upload.updateOne({name: name}, { $inc: { views: 1 } }, function(err) {
if (err) throw err;
});
}
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 {
addView(upload.name);
var disposition;
if (upload.file.mimetype.split('/')[0] === 'image')
disposition = 'inline';
else
disposition = 'attachment; filename="' + upload.file.originalname + '"';
res.set({
"Content-Disposition": disposition,
"Content-Type": upload.file.mimetype
});
fs.createReadStream(upload.file.path).pipe(res);
}
}
});
});
module.exports = router;