mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 09:06:15 +01:00
refactor: thumbs.associate accepts both relative path and url in path arg
This commit is contained in:
@@ -130,8 +130,7 @@ Topics.addThumb = async (req, res) => {
|
|||||||
await Promise.all(files.map(async (fileObj) => {
|
await Promise.all(files.map(async (fileObj) => {
|
||||||
await topics.thumbs.associate({
|
await topics.thumbs.associate({
|
||||||
id: req.params.tid,
|
id: req.params.tid,
|
||||||
path: fileObj.path || null,
|
path: fileObj.path || fileObj.url,
|
||||||
url: fileObj.url,
|
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,28 +67,28 @@ async function getThumbs(set) {
|
|||||||
return thumbs.slice();
|
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
|
// 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));
|
||||||
let value = relativePath || url;
|
const isLocal = !path.startsWith('http');
|
||||||
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 (isLocal) {
|
||||||
value = value.replace(nconf.get('upload_path'), '');
|
path = path.replace(nconf.get('upload_path'), '');
|
||||||
}
|
}
|
||||||
const topics = require('.');
|
const topics = require('.');
|
||||||
await db.sortedSetAdd(set, numThumbs, value);
|
await db.sortedSetAdd(set, numThumbs, path);
|
||||||
if (!isDraft) {
|
if (!isDraft) {
|
||||||
await topics.setTopicField(id, 'numThumbs', numThumbs);
|
await topics.setTopicField(id, 'numThumbs', numThumbs);
|
||||||
}
|
}
|
||||||
cache.del(set);
|
cache.del(set);
|
||||||
|
|
||||||
// Associate thumbnails with the main pid (only on local upload)
|
// Associate thumbnails with the main pid (only on local upload)
|
||||||
if (!isDraft && relativePath) {
|
if (!isDraft && isLocal) {
|
||||||
const mainPid = (await topics.getMainPids([id]))[0];
|
const mainPid = (await topics.getMainPids([id]))[0];
|
||||||
posts.uploads.associate(mainPid, relativePath.replace('/files/', ''));
|
posts.uploads.associate(mainPid, path.replace('/files/', ''));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user