1
0
mirror of https://github.com/Foltik/Shimapan synced 2025-01-20 22:06:57 -05:00

Change id in Upload schema to uid

This commit is contained in:
Jack Foltz 2018-09-15 16:20:14 -04:00
parent 18d33de0c5
commit 822bbb015a
Signed by: foltik
GPG Key ID: 303F88F996E95541
4 changed files with 18 additions and 18 deletions

View File

@ -1,7 +1,7 @@
var mongoose = require('mongoose'); var mongoose = require('mongoose');
var UploadSchema = mongoose.Schema({ var UploadSchema = mongoose.Schema({
id: { uid: {
type: String, type: String,
unique: true, unique: true,
required: true required: true

View File

@ -12,7 +12,7 @@ const wrap = require('../../util/wrap.js');
router.post('/', uploadMultipart, wrap(async (req, res) => { router.post('/', uploadMultipart, wrap(async (req, res) => {
const upload = { const upload = {
id: req.file.name, uid: req.file.name,
uploader: req.username, uploader: req.username,
uploaderKey: req.key, uploaderKey: req.key,
date: Date.now(), date: Date.now(),
@ -26,8 +26,8 @@ router.post('/', uploadMultipart, wrap(async (req, res) => {
res.status(200).json({ res.status(200).json({
message: 'File uploaded.', message: 'File uploaded.',
id: req.file.name, uid: req.file.name,
url: config.get('Server.hostname') + '/v/' + upload.id url: config.get('Server.hostname') + '/v/' + upload.uid
}); });
})); }));

View File

@ -9,17 +9,17 @@ const Upload = require(ModelPath + 'Upload.js');
const wrap = require('../../util/wrap.js'); const wrap = require('../../util/wrap.js');
const incrementViews = async id => const incrementViews = async uid =>
Upload.updateOne({id: id}, {$inc: {views: 1}}); Upload.updateOne({uid: uid}, {$inc: {views: 1}});
router.get('/:id', wrap(async (req, res) => { router.get('/:uid', wrap(async (req, res) => {
const upload = await Upload.findOne({id: req.params.id}); const upload = await Upload.findOne({uid: req.params.uid});
if (!upload) if (!upload)
return res.status(404).json({message: 'File not found.'}); return res.status(404).json({message: 'File not found.'});
// Increment the file's view counter // Increment the file's view counter
await incrementViews(req.params.id); await incrementViews(req.params.uid);
// Whether the file should be an attachment or displayed inline on the page // Whether the file should be an attachment or displayed inline on the page
const mimetype = upload.file.mime.split('/'); const mimetype = upload.file.mime.split('/');

View File

@ -276,13 +276,13 @@ describe('Uploading', () => {
res.body.should.be.a('object'); res.body.should.be.a('object');
res.body.should.have.property('url'); res.body.should.have.property('url');
const idLength = config.get('Upload.idLength'); const idLength = config.get('Upload.idLength');
res.body.should.have.property('id').length(idLength, 'The ID should be a ' + idLength + ' letter lowercase string.'); res.body.should.have.property('uid').length(idLength, 'The UID should be a ' + idLength + ' letter lowercase string.');
// Find the uploaded file in the database // Find the uploaded file in the database
const upload = await Upload.findOne({id: res.body.id}, {_id: 0, id: 1, file: 1}); const upload = await Upload.findOne({uid: res.body.uid}, {_id: 0, uid: 1, file: 1});
const uploadFile = upload.file.path; const uploadFile = upload.file.path;
upload.should.be.a('object'); upload.should.be.a('object');
upload.id.should.equal(res.body.id, 'The uploaded file in the database should exist and match the reponse ID.'); upload.uid.should.equal(res.body.uid, 'The uploaded file in the database should exist and match the reponse ID.');
// Verify the uploaded file is the same as the file now on disk // Verify the uploaded file is the same as the file now on disk
const uploadHash = await util.fileHash(uploadFile); const uploadHash = await util.fileHash(uploadFile);
@ -444,10 +444,10 @@ describe('Uploading', () => {
}); });
describe('Viewing', () => { describe('Viewing', () => {
async function verifyView(file, id, disposition) { async function verifyView(file, uid, disposition) {
const viewsBefore = (await Upload.findOne({id: id})).views; const viewsBefore = (await Upload.findOne({uid: uid})).views;
const res = await util.view(id, agent) const res = await util.view(uid, agent)
.parse(util.binaryFileParser); .parse(util.binaryFileParser);
res.should.have.status(200); res.should.have.status(200);
@ -459,7 +459,7 @@ describe('Viewing', () => {
]); ]);
downloadHash.should.equal(uploadHash, 'Uploaded file and downloaded hash should match'); downloadHash.should.equal(uploadHash, 'Uploaded file and downloaded hash should match');
const viewsAfter = (await Upload.findOne({id: id})).views; const viewsAfter = (await Upload.findOne({uid: uid})).views;
viewsAfter.should.equal(viewsBefore + 1, 'The files views should be incremented.'); viewsAfter.should.equal(viewsBefore + 1, 'The files views should be incremented.');
} }
@ -469,7 +469,7 @@ describe('Viewing', () => {
util.createTestFile(2048, 'test.bin') util.createTestFile(2048, 'test.bin')
]); ]);
const upload = await util.upload('test.bin', agent); const upload = await util.upload('test.bin', agent);
await verifyView('test.bin', upload.body.id, 'attachment; filename="test.bin"'); await verifyView('test.bin', upload.body.uid, 'attachment; filename="test.bin"');
return util.deleteFile('test.bin'); return util.deleteFile('test.bin');
}); });
@ -479,7 +479,7 @@ describe('Viewing', () => {
util.createTestFile(2048, 'test.jpg') util.createTestFile(2048, 'test.jpg')
]); ]);
const upload = await util.upload('test.jpg', agent); const upload = await util.upload('test.jpg', agent);
await verifyView('test.jpg', upload.body.id, 'inline'); await verifyView('test.jpg', upload.body.uid, 'inline');
return util.deleteFile('test.jpg'); return util.deleteFile('test.jpg');
}); });