mirror of
https://github.com/Foltik/Shimapan
synced 2024-11-27 21:19:09 -05:00
Add upload and view stats
This commit is contained in:
parent
9a090d794d
commit
5ffc93b147
@ -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
|
||||
});
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user