[nodebb-plugin-autothumb] How can I associate the topic to the thumbnail
-
I want to make a plugin that works as below.
- The topic content has an image markdown tag and extracts the first image path found.
- If the user has not attached a thumbnail to the topic, download the above image path.
- Set the downloaded image as a thumbnail for the topic, and save the image in the thumbnail file path.
- The thumbnail is shown to the user when showing nodebb topics.
Please advise me on how to handle step 3.
It's the source I've worked on so far
library.js'use strict'; const axios = require('axios'); const fs = require('fs'); const path = require('path'); const os = require('os'); const { mkdirSync } = require('fs'); const topics = require.main.require('./src/topics'); const plugin = module.exports; var IMAGE_DIR = '/tmp/nodebb-images' plugin.init = async function (params) { //const { router /* , middleware , controllers */ } = params; IMAGE_DIR = await path.join(os.tmpdir(), 'nodebb-images'); const hasImgDir = await fs.existsSync(IMAGE_DIR); if (!hasImgDir) { await mkdirSync(IMAGE_DIR); } }; plugin.onTopicCreate = async function(hookData) { //await console.log("----------------------------- onTopicCreate -----------------------------------"); //console.log(hookData); await setTopicThumbnail(hookData.topic.tid, hookData.data.content); return hookData; }; plugin.onTopicEdit = async function(hookData) { await console.log("----------------------------- setTopicThumbnail -----------------------------------"); console.log(hookData); await setTopicThumbnail(hookData.topic.tid, hookData.data.content); return hookData; }; async function setTopicThumbnail(tid, content) { const imageUrl = extractImageUrl(content); if (!imageUrl) { //await console.log("Image URL Not found in markdown content"); return; } if (!imageUrl.startsWith('http')) { return; } try { const imagePath = await downloadFile(imageUrl, tid); await topics.setThumbnail(tid, imagePath); } catch (err) { console.error('Error cound not download image:', err); } }; async function downloadFile(url, tid) { try { const response = await axios({ method: 'get', url: url, responseType: 'stream' }); const ext = path.extname(url); const fileName = `${tid}${ext}`; const filePath = path.join(IMAGE_DIR, fileName); return new Promise((resolve, reject) => { const writer = fs.createWriteStream(filePath); response.data.pipe(writer); writer.on('finish', () => resolve(filePath)); writer.on('error', reject); }); } catch (err) { throw new Error('이미지 다운로드 실패: ' + err.message); } }; function extractImageUrl(content) { const regex = /!\[.*?\]\((.*?)\)/; const match = content.match(regex); return match ? match[1] : null; };
plugin.json
{ "id": "nodebb-plugin-autothumb", "name": "Auto Image Thumbnail Plugin", "version": "0.1.0", "description": "Automatically sets the first image in a post as the topic thumbnail.", "library": "./library.js", "license": "MIT", "hooks": [ { "hook": "static:app.load", "method": "init" }, { "hook": "filter:topic.create", "method": "onTopicCreate" }, { "hook": "filter:topic.edit", "method": "onTopicEdit" } ], "dependencies": { "axios": "^0.21.1" } }
-
@busker this is now on-track to be included in NodeBB v4.
feat: append uploaded images into thumbnail set · NodeBB/NodeBB@cc65214
Node.js based forum software built for the modern web - feat: append uploaded images into thumbnail set · NodeBB/NodeBB@cc65214
GitHub (github.com)
Uploaded images are now automatically added to the thumbnail set.
-
@busker Keep in mind that this encompasses uploaded media only. Externally linked media is excluded (so, giphy embeds would be an example that would not be included).
Edit:
filter:topics.hasThumbs
has been added (in addition to already-addedfilter:topics.getThumbs
) so that plugins can append images to the thumbnail list.
Copyright © 2024 NodeBB | Contributors