refactor: thumbs.associate accepts both relative path and url in path arg

v1.18.x
Julian Lam 4 years ago
parent 36e8d251c8
commit 3e6640efb2

@ -130,8 +130,7 @@ Topics.addThumb = async (req, res) => {
await Promise.all(files.map(async (fileObj) => {
await topics.thumbs.associate({
id: req.params.tid,
path: fileObj.path || null,
url: fileObj.url,
path: fileObj.path || fileObj.url,
});
}));
}

@ -67,28 +67,28 @@ async function getThumbs(set) {
return thumbs.slice();
}
Thumbs.associate = async function ({ id, path: relativePath, url }) {
Thumbs.associate = async function ({ id, path }) {
// Associates a newly uploaded file as a thumb to the passed-in draft or topic
const isDraft = validator.isUUID(String(id));
let value = relativePath || url;
const isLocal = !path.startsWith('http');
const set = `${isDraft ? 'draft' : 'topic'}:${id}:thumbs`;
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)
if (relativePath) {
value = value.replace(nconf.get('upload_path'), '');
if (isLocal) {
path = path.replace(nconf.get('upload_path'), '');
}
const topics = require('.');
await db.sortedSetAdd(set, numThumbs, value);
await db.sortedSetAdd(set, numThumbs, path);
if (!isDraft) {
await topics.setTopicField(id, 'numThumbs', numThumbs);
}
cache.del(set);
// Associate thumbnails with the main pid (only on local upload)
if (!isDraft && relativePath) {
if (!isDraft && isLocal) {
const mainPid = (await topics.getMainPids([id]))[0];
posts.uploads.associate(mainPid, relativePath.replace('/files/', ''));
posts.uploads.associate(mainPid, path.replace('/files/', ''));
}
};

Loading…
Cancel
Save