1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-28 05:26:44 -05:00

Add upload and view stats

This commit is contained in:
Jack 2017-10-14 15:13:50 -04:00
parent 9a090d794d
commit 5ffc93b147
Signed by: foltik
GPG Key ID: 303F88F996E95541
4 changed files with 42 additions and 8 deletions

View File

@ -6,7 +6,11 @@ var UploadSchema = mongoose.Schema({
unique: true, unique: true,
required: true required: true
}, },
oname: String, views: {
type: Number,
default: 0
},
uploader: String,
date: Date, date: Date,
file: Object file: Object
}); });

View File

@ -11,6 +11,14 @@ var UserSchema = mongoose.Schema({
unique: true, unique: true,
required: true required: true
}, },
uploadCount: {
type: Number,
default: 0
},
uploadSize: {
type: Number,
default: 0
},
scope: [String], scope: [String],
hash: String, hash: String,
salt: String, salt: String,

View File

@ -2,17 +2,26 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
var mongoose = require('mongoose'); var mongoose = require('mongoose');
var User = require('../models/User.js');
var Upload = require('../models/Upload.js'); var Upload = require('../models/Upload.js');
var multer = require('multer'); var multer = require('multer');
var dest = multer({dest: 'uploads/'}); var dest = multer({dest: 'uploads/'});
function fileNameExists(name) { function fileNameExists(name) {
Upload.count({name: name}, function(err, count) { Upload.count({name: name}, function (err, count) {
return count !== 0; 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() { function genFileName() {
var charset = "abcdefghijklmnopqrstuvwxyz"; var charset = "abcdefghijklmnopqrstuvwxyz";
do { do {
@ -23,7 +32,7 @@ function genFileName() {
return chars.join(''); 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) { if (req.payload.scope.indexOf('file.upload') === -1) {
res.status(403).json({'message': 'Permission error.'}); res.status(403).json({'message': 'Permission error.'});
return; return;
@ -35,18 +44,23 @@ router.post('/', dest.single('file'), function(req, res) {
return; return;
} }
updateStats(req.payload.username, req.file.size);
var entry = { var entry = {
name: genFileName(), name: genFileName(),
oname: req.file.originalname, uploader: req.payload.username,
created: Date.now(), created: Date.now(),
file: req.file file: req.file
}; };
Upload.create(entry, function(err, next) { Upload.create(entry, function (err, next) {
if (err) { if (err) {
next(err); next(err);
} else { } else {
res.send(entry); res.send({
name: entry.name,
url: 'https://shimapan.rocks/v/' + entry.name
});
} }
}); });
}); });

View File

@ -4,9 +4,15 @@ var fs = require('fs');
var mongoose = require('mongoose'); var mongoose = require('mongoose');
var Upload = mongoose.model('Upload'); 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) { router.get('/:name', function(req, res, next) {
Upload.findOne({ Upload.findOne({
'name': req.params.name name: req.params.name
}, function(err, upload) { }, function(err, upload) {
if (err) { if (err) {
next(err); next(err);
@ -14,11 +20,13 @@ router.get('/:name', function(req, res, next) {
if (!upload) { if (!upload) {
res.sendStatus(404); res.sendStatus(404);
} else { } else {
addView(upload.name);
var disposition; var disposition;
if (upload.file.mimetype.split('/')[0] === 'image') if (upload.file.mimetype.split('/')[0] === 'image')
disposition = 'inline'; disposition = 'inline';
else else
disposition = 'attachment; filename="' + upload.oname + '"'; disposition = 'attachment; filename="' + upload.file.originalname + '"';
res.set({ res.set({
"Content-Disposition": disposition, "Content-Disposition": disposition,