From 5ffc93b14793c72b5632e021583739ecebc3b408 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 14 Oct 2017 15:13:50 -0400 Subject: [PATCH] Add upload and view stats --- app/models/Upload.js | 6 +++++- app/models/User.js | 8 ++++++++ app/routes/upload.js | 24 +++++++++++++++++++----- app/routes/view.js | 12 ++++++++++-- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/app/models/Upload.js b/app/models/Upload.js index d1c9aeb..4b77826 100644 --- a/app/models/Upload.js +++ b/app/models/Upload.js @@ -6,7 +6,11 @@ var UploadSchema = mongoose.Schema({ unique: true, required: true }, - oname: String, + views: { + type: Number, + default: 0 + }, + uploader: String, date: Date, file: Object }); diff --git a/app/models/User.js b/app/models/User.js index bdfcd37..ef5501a 100644 --- a/app/models/User.js +++ b/app/models/User.js @@ -11,6 +11,14 @@ var UserSchema = mongoose.Schema({ unique: true, required: true }, + uploadCount: { + type: Number, + default: 0 + }, + uploadSize: { + type: Number, + default: 0 + }, scope: [String], hash: String, salt: String, diff --git a/app/routes/upload.js b/app/routes/upload.js index c29c0d8..4cf7650 100644 --- a/app/routes/upload.js +++ b/app/routes/upload.js @@ -2,17 +2,26 @@ var express = require('express'); var router = express.Router(); var mongoose = require('mongoose'); +var User = require('../models/User.js'); var Upload = require('../models/Upload.js'); var multer = require('multer'); var dest = multer({dest: 'uploads/'}); function fileNameExists(name) { - Upload.count({name: name}, function(err, count) { + Upload.count({name: name}, function (err, count) { return count !== 0; }); } +function updateStats(user, size) { + User.updateOne({username: user}, { $inc: { uploadCount: 1, uploadSize: size } }, function(err, res) { + if (err) { + throw err; + } + }); +} + function genFileName() { var charset = "abcdefghijklmnopqrstuvwxyz"; do { @@ -23,7 +32,7 @@ function genFileName() { return chars.join(''); } -router.post('/', dest.single('file'), function(req, res) { +router.post('/', dest.single('file'), function (req, res) { if (req.payload.scope.indexOf('file.upload') === -1) { res.status(403).json({'message': 'Permission error.'}); return; @@ -35,18 +44,23 @@ router.post('/', dest.single('file'), function(req, res) { return; } + updateStats(req.payload.username, req.file.size); + var entry = { name: genFileName(), - oname: req.file.originalname, + uploader: req.payload.username, created: Date.now(), file: req.file }; - Upload.create(entry, function(err, next) { + Upload.create(entry, function (err, next) { if (err) { next(err); } else { - res.send(entry); + res.send({ + name: entry.name, + url: 'https://shimapan.rocks/v/' + entry.name + }); } }); }); diff --git a/app/routes/view.js b/app/routes/view.js index b988282..2e60013 100644 --- a/app/routes/view.js +++ b/app/routes/view.js @@ -4,9 +4,15 @@ 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 + name: req.params.name }, function(err, upload) { if (err) { next(err); @@ -14,11 +20,13 @@ router.get('/:name', function(req, res, next) { 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.oname + '"'; + disposition = 'attachment; filename="' + upload.file.originalname + '"'; res.set({ "Content-Disposition": disposition,