feat: associate topic thumbs with post uploads (for the mainPid)

v1.18.x
Julian Lam 4 years ago
parent 08736b1812
commit 1c5cdb5121

@ -8,6 +8,7 @@ const validator = require('validator');
const db = require('../database'); const db = require('../database');
const file = require('../file'); const file = require('../file');
const plugins = require('../plugins'); const plugins = require('../plugins');
const posts = require('../posts');
const Thumbs = {}; const Thumbs = {};
module.exports = Thumbs; module.exports = Thumbs;
@ -36,13 +37,20 @@ Thumbs.get = async function (tids) {
return singular ? response.pop() : response; return singular ? response.pop() : response;
}; };
Thumbs.associate = async function (id, path) { Thumbs.associate = async function (id, relativePath) {
// 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 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);
path = path.replace(nconf.get('upload_path'), ''); relativePath = relativePath.replace(nconf.get('upload_path'), '');
db.sortedSetAdd(set, numThumbs, path); db.sortedSetAdd(set, numThumbs, relativePath);
// Associate thumbnails with the main pid
if (!isDraft) {
const topics = require('.');
const mainPid = (await topics.getMainPids([id]))[0];
posts.uploads.associate(mainPid, relativePath.replace('/files/', ''));
}
}; };
Thumbs.migrate = async function (uuid, id) { Thumbs.migrate = async function (uuid, id) {
@ -68,5 +76,12 @@ Thumbs.delete = async function (id, relativePath) {
if (existsOnDisk) { if (existsOnDisk) {
await file.delete(absolutePath); await file.delete(absolutePath);
} }
// Dissociate thumbnails with the main pid
if (!isDraft) {
const topics = require('.');
const mainPid = (await topics.getMainPids([id]))[0];
posts.uploads.dissociate(mainPid, relativePath.replace('/files/', ''));
}
} }
}; };

Loading…
Cancel
Save