fix: bad topic thumbs logic on local thumb upload

v1.18.x
Julian Lam 4 years ago
parent ce8057f389
commit e83baa97a0

@ -42,13 +42,13 @@ Thumbs.get = async function (tids) {
Thumbs.associate = async function ({ id, path: relativePath, url }) { Thumbs.associate = async function ({ id, path: relativePath, url }) {
// Associates a newly uploaded file as a thumb to the passed-in draft or topic // Associates a newly uploaded file as a thumb to the passed-in draft or topic
const isDraft = validator.isUUID(String(id)); const isDraft = validator.isUUID(String(id));
const value = relativePath || url; let value = relativePath || url;
const set = `${isDraft ? 'draft' : 'topic'}:${id}:thumbs`; const set = `${isDraft ? 'draft' : 'topic'}:${id}:thumbs`;
const numThumbs = await db.sortedSetCard(set); const numThumbs = await db.sortedSetCard(set);
// Normalize the path to allow for changes in upload_path (and so upload_url can be appended if needed) // Normalize the path to allow for changes in upload_path (and so upload_url can be appended if needed)
if (relativePath) { if (relativePath) {
relativePath = relativePath.replace(nconf.get('upload_path'), ''); value = value.replace(nconf.get('upload_path'), '');
} }
db.sortedSetAdd(set, numThumbs, value); db.sortedSetAdd(set, numThumbs, value);

@ -29,6 +29,7 @@ describe('Topic thumbs', () => {
let fooCSRF; let fooCSRF;
let fooUid; let fooUid;
const thumbPaths = [`${nconf.get('upload_path')}/files/test.png`, `${nconf.get('upload_path')}/files/test2.png`, 'https://example.org']; const thumbPaths = [`${nconf.get('upload_path')}/files/test.png`, `${nconf.get('upload_path')}/files/test2.png`, 'https://example.org'];
const relativeThumbPaths = thumbPaths.map(path => path.replace(nconf.get('upload_path'), ''));
const uuid = utils.generateUUID(); const uuid = utils.generateUUID();
function createFiles() { function createFiles() {
@ -112,7 +113,7 @@ describe('Topic thumbs', () => {
path: thumbPaths[0], path: thumbPaths[0],
}); });
const exists = await db.isSortedSetMember(`topic:2:thumbs`, thumbPaths[0]); const exists = await db.isSortedSetMember(`topic:2:thumbs`, relativeThumbPaths[0]);
assert(exists); assert(exists);
}); });
@ -122,7 +123,7 @@ describe('Topic thumbs', () => {
path: thumbPaths[1], path: thumbPaths[1],
}); });
const exists = await db.isSortedSetMember(`draft:${uuid}:thumbs`, thumbPaths[1]); const exists = await db.isSortedSetMember(`draft:${uuid}:thumbs`, relativeThumbPaths[1]);
assert(exists); assert(exists);
}); });
@ -132,7 +133,7 @@ describe('Topic thumbs', () => {
path: thumbPaths[2], path: thumbPaths[2],
}); });
const exists = await db.isSortedSetMember(`topic:2:thumbs`, thumbPaths[2]); const exists = await db.isSortedSetMember(`topic:2:thumbs`, relativeThumbPaths[2]);
assert(exists); assert(exists);
}); });
}); });
@ -147,7 +148,7 @@ describe('Topic thumbs', () => {
{ {
id: 2, id: 2,
name: 'test.png', name: 'test.png',
url: `${nconf.get('upload_url')}${thumbPaths[0]}`, url: `${nconf.get('upload_url')}${relativeThumbPaths[0]}`,
}, },
{ {
id: 2, id: 2,
@ -157,7 +158,7 @@ describe('Topic thumbs', () => {
{ {
id: 2, id: 2,
name: 'test2.png', name: 'test2.png',
url: `${nconf.get('upload_url')}${thumbPaths[1]}`, url: `${nconf.get('upload_url')}${relativeThumbPaths[1]}`,
}, },
]); ]);
}); });
@ -169,10 +170,10 @@ describe('Topic thumbs', () => {
id: 1, id: 1,
path: thumbPaths[0], path: thumbPaths[0],
}); });
await topics.thumbs.delete(1, thumbPaths[0]); await topics.thumbs.delete(1, relativeThumbPaths[0]);
assert.strictEqual(await db.isSortedSetMember('topic:1:thumbs', thumbPaths[0]), false); assert.strictEqual(await db.isSortedSetMember('topic:1:thumbs', relativeThumbPaths[0]), false);
assert.strictEqual(await file.exists(`${nconf.get('upload_path')}${thumbPaths[0]}`), false); assert.strictEqual(await file.exists(thumbPaths[0]), false);
}); });
it('should also work with UUIDs', async () => { it('should also work with UUIDs', async () => {
@ -180,10 +181,10 @@ describe('Topic thumbs', () => {
id: uuid, id: uuid,
path: thumbPaths[1], path: thumbPaths[1],
}); });
await topics.thumbs.delete(uuid, thumbPaths[1]); await topics.thumbs.delete(uuid, relativeThumbPaths[1]);
assert.strictEqual(await db.isSortedSetMember(`draft:${uuid}:thumbs`, thumbPaths[1]), false); assert.strictEqual(await db.isSortedSetMember(`draft:${uuid}:thumbs`, relativeThumbPaths[1]), false);
assert.strictEqual(await file.exists(`${nconf.get('upload_path')}${thumbPaths[1]}`), false); assert.strictEqual(await file.exists(thumbPaths[1]), false);
}); });
it('should also work with URLs', async () => { it('should also work with URLs', async () => {
@ -191,15 +192,15 @@ describe('Topic thumbs', () => {
id: uuid, id: uuid,
path: thumbPaths[2], path: thumbPaths[2],
}); });
await topics.thumbs.delete(uuid, thumbPaths[2]); await topics.thumbs.delete(uuid, relativeThumbPaths[2]);
assert.strictEqual(await db.isSortedSetMember(`draft:${uuid}:thumbs`, thumbPaths[2]), false); assert.strictEqual(await db.isSortedSetMember(`draft:${uuid}:thumbs`, relativeThumbPaths[2]), false);
}); });
it('should not delete the file from disk if not associated with the tid', async () => { it('should not delete the file from disk if not associated with the tid', async () => {
createFiles(); createFiles();
await topics.thumbs.delete(uuid, thumbPaths[0]); await topics.thumbs.delete(uuid, thumbPaths[0]);
assert.strictEqual(await file.exists(`${nconf.get('upload_path')}${thumbPaths[0]}`), true); assert.strictEqual(await file.exists(thumbPaths[0]), true);
}); });
}); });

Loading…
Cancel
Save