const { resolvePlayerRegistration } = require('#src/data/player-registry'); const TASK = { taskType: 'slide-thumbnail-refresh' }; async function fetchPlayerInternalBaseUrl(pool, configuredPlayerInternalBaseUrl) { const configured = String(configuredPlayerInternalBaseUrl || '').trim().replace(/\/$/, ''); if (configured) { return configured; } const { getConfiguredPlayerIdentifier } = require('#src/data/player-registry'); const player = await resolvePlayerRegistration(pool, getConfiguredPlayerIdentifier()); return String(player && player.internal_base_url || '').trim().replace(/\/$/, '') || null; } function registerSlideThumbnailRefreshTask(options) { const backgroundTaskQueue = options && options.backgroundTaskQueue; const captureSlideThumbnail = options && options.captureSlideThumbnail; const pool = options && options.pool; const common = options && options.common; const mediaDir = String(options && options.mediaDir || '').trim(); const configuredWebBaseUrl = String(options && options.webBaseUrl || '').trim().replace(/\/$/, ''); if (!backgroundTaskQueue || typeof captureSlideThumbnail !== 'function' || !pool || !common || !mediaDir) { throw new Error('registerSlideThumbnailRefreshTask requires the slide thumbnail dependencies.'); } // Refresh the slide thumbnail for one slide id. backgroundTaskQueue.setTaskHandler(TASK.taskType, async function (task) { const payload = task && task.payload ? task.payload : {}; const slideId = Number(payload.slideId || 0); if (!Number.isFinite(slideId) || slideId <= 0) { throw new Error('Slide id is required.'); } const webBaseUrl = configuredWebBaseUrl || `http://127.0.0.1:${Number(process.env.WEB_PORT || 8080)}`; if (!webBaseUrl) { throw new Error('Web base URL is required.'); } return captureSlideThumbnail({ pool: pool, common: common, mediaDir: mediaDir, baseUrl: webBaseUrl, slideId: slideId, previousThumbnailPath: payload.previousThumbnailPath || null, fontStylesheetHref: payload.fontStylesheetHref || '' }); }); } module.exports = { registerSlideThumbnailRefreshTask };