mirror of
https://github.com/Foltik/Shimapan
synced 2025-04-22 15:57:11 -04:00
Add View model for logging views in the db
This commit is contained in:
parent
c1a3882097
commit
f892b5676c
@ -5,21 +5,28 @@ const fs = require('fs');
|
||||
|
||||
const ModelPath = '../../models/';
|
||||
const Upload = require(ModelPath + 'Upload.js');
|
||||
const View = require(ModelPath + 'View.js');
|
||||
|
||||
const wrap = require('../../util/wrap.js');
|
||||
|
||||
|
||||
const incrementViews = async uid =>
|
||||
Upload.updateOne({uid: uid}, {$inc: {views: 1}});
|
||||
|
||||
const insertView = async (req, upload) =>
|
||||
Promise.all([
|
||||
View.create({
|
||||
uid: upload.uid,
|
||||
uploader: upload.uploader,
|
||||
remoteAddress: req.ip,
|
||||
userAgent: req.headers['user-agent']
|
||||
}),
|
||||
Upload.updateOne({uid: upload.uid}, {$inc: {views: 1}})
|
||||
]);
|
||||
|
||||
router.get('/:uid', wrap(async (req, res) => {
|
||||
const upload = await Upload.findOne({uid: req.params.uid});
|
||||
if (!upload)
|
||||
return res.status(404).json({message: 'File not found.'});
|
||||
|
||||
// Increment the file's view counter
|
||||
await incrementViews(req.params.uid);
|
||||
// Increment the file's view counter and insert a a view record
|
||||
await insertView(req, upload);
|
||||
|
||||
// Whether the file should be an attachment or displayed inline on the page
|
||||
const mimetype = upload.file.mime.split('/');
|
||||
|
10
test/api.js
10
test/api.js
@ -10,6 +10,7 @@ const User = require(ModelPath + 'User.js');
|
||||
const Upload = require(ModelPath + 'Upload.js');
|
||||
const Key = require(ModelPath + 'Key.js');
|
||||
const Invite = require(ModelPath + 'Invite.js');
|
||||
const View = require(ModelPath + 'View.js');
|
||||
|
||||
const util = require('./testUtil.js');
|
||||
const canonicalize = require('../app/util/canonicalize').canonicalize;
|
||||
@ -445,7 +446,8 @@ describe('Uploading', () => {
|
||||
|
||||
describe('Viewing', () => {
|
||||
async function verifyView(file, uid, disposition) {
|
||||
const viewsBefore = (await Upload.findOne({uid: uid})).views;
|
||||
const uploadViewsBefore = (await Upload.findOne({uid: uid})).views;
|
||||
const viewsBefore = await View.countDocuments();
|
||||
|
||||
const res = await util.view(uid, agent)
|
||||
.parse(util.binaryFileParser);
|
||||
@ -459,8 +461,10 @@ describe('Viewing', () => {
|
||||
]);
|
||||
downloadHash.should.equal(uploadHash, 'Uploaded file and downloaded hash should match');
|
||||
|
||||
const viewsAfter = (await Upload.findOne({uid: uid})).views;
|
||||
viewsAfter.should.equal(viewsBefore + 1, 'The files views should be incremented.');
|
||||
const viewsAfter = await View.countDocuments();
|
||||
const uploadViewsAfter = (await Upload.findOne({uid: uid})).views;
|
||||
uploadViewsAfter.should.equal(uploadViewsBefore + 1, 'The files views should be incremented.');
|
||||
viewsAfter.should.equal(viewsBefore + 1, 'A view object should have been inserted to the database.');
|
||||
}
|
||||
|
||||
it('must return an uploaded binary file', async () => {
|
||||
|
Loading…
Reference in New Issue
Block a user