Skip to content

Node.js based forum software built for the modern web

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

Julian Lam3e6640efb2ae
diff --git a/src/controllers/write/topics.js b/src/controllers/write/topics.js
index 23ade7262d..dc9b62fc93 100644
--- a/src/controllers/write/topics.js
+++ b/src/controllers/write/topics.js
@@ -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,
});
}));
}
diff --git a/src/topics/thumbs.js b/src/topics/thumbs.js
index bf174cb918..fd417189d2 100644
--- a/src/topics/thumbs.js
+++ b/src/topics/thumbs.js
@@ -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/', ''));
}
};