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:
parent
18d33de0c5
commit
822bbb015a
@ -1,7 +1,7 @@
|
||||
var mongoose = require('mongoose');
|
||||
|
||||
var UploadSchema = mongoose.Schema({
|
||||
id: {
|
||||
uid: {
|
||||
type: String,
|
||||
unique: true,
|
||||
required: true
|
||||
|
@ -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
|
||||
});
|
||||
}));
|
||||
|
||||
|
@ -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('/');
|
||||
|
18
test/api.js
18
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');
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user