From 822bbb015a565c78bc7f9b01ae35c01b3a1c303f Mon Sep 17 00:00:00 2001 From: Jack Foltz Date: Sat, 15 Sep 2018 16:20:14 -0400 Subject: [PATCH] Change id in Upload schema to uid --- app/models/Upload.js | 2 +- app/routes/api/upload.js | 6 +++--- app/routes/api/view.js | 10 +++++----- test/api.js | 18 +++++++++--------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/models/Upload.js b/app/models/Upload.js index 74e60da..278c42e 100644 --- a/app/models/Upload.js +++ b/app/models/Upload.js @@ -1,7 +1,7 @@ var mongoose = require('mongoose'); var UploadSchema = mongoose.Schema({ - id: { + uid: { type: String, unique: true, required: true diff --git a/app/routes/api/upload.js b/app/routes/api/upload.js index ee86b6b..5faedbc 100644 --- a/app/routes/api/upload.js +++ b/app/routes/api/upload.js @@ -12,7 +12,7 @@ const wrap = require('../../util/wrap.js'); router.post('/', uploadMultipart, wrap(async (req, res) => { const upload = { - id: req.file.name, + uid: req.file.name, uploader: req.username, uploaderKey: req.key, date: Date.now(), @@ -26,8 +26,8 @@ router.post('/', uploadMultipart, wrap(async (req, res) => { res.status(200).json({ message: 'File uploaded.', - id: req.file.name, - url: config.get('Server.hostname') + '/v/' + upload.id + uid: req.file.name, + url: config.get('Server.hostname') + '/v/' + upload.uid }); })); diff --git a/app/routes/api/view.js b/app/routes/api/view.js index d6e99e0..5a42e69 100644 --- a/app/routes/api/view.js +++ b/app/routes/api/view.js @@ -9,17 +9,17 @@ const Upload = require(ModelPath + 'Upload.js'); const wrap = require('../../util/wrap.js'); -const incrementViews = async id => - Upload.updateOne({id: id}, {$inc: {views: 1}}); +const incrementViews = async uid => + Upload.updateOne({uid: uid}, {$inc: {views: 1}}); -router.get('/:id', wrap(async (req, res) => { - const upload = await Upload.findOne({id: req.params.id}); +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.id); + await incrementViews(req.params.uid); // Whether the file should be an attachment or displayed inline on the page const mimetype = upload.file.mime.split('/'); diff --git a/test/api.js b/test/api.js index e296af1..e824b20 100644 --- a/test/api.js +++ b/test/api.js @@ -276,13 +276,13 @@ describe('Uploading', () => { res.body.should.be.a('object'); res.body.should.have.property('url'); 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 - 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; 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 const uploadHash = await util.fileHash(uploadFile); @@ -444,10 +444,10 @@ describe('Uploading', () => { }); describe('Viewing', () => { - async function verifyView(file, id, disposition) { - const viewsBefore = (await Upload.findOne({id: id})).views; + async function verifyView(file, uid, disposition) { + 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); res.should.have.status(200); @@ -459,7 +459,7 @@ describe('Viewing', () => { ]); 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.'); } @@ -469,7 +469,7 @@ describe('Viewing', () => { util.createTestFile(2048, 'test.bin') ]); 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'); }); @@ -479,7 +479,7 @@ describe('Viewing', () => { util.createTestFile(2048, 'test.jpg') ]); 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'); });