const TASK = { taskType: 'slide-thumbnail-refresh' }; async function fetchPlayerInternalBaseUrl(pool) { const [rows] = await pool.query( `SELECT internal_base_url FROM d_players WHERE device_id = '1' LIMIT 1` ); return String(rows && rows[0] && rows[0].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(); 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 playerInternalBaseUrl = await fetchPlayerInternalBaseUrl(pool); if (!playerInternalBaseUrl) { throw new Error('Player internal base URL is required.'); } return captureSlideThumbnail({ pool: pool, common: common, mediaDir: mediaDir, baseUrl: playerInternalBaseUrl, slideId: slideId, previousThumbnailPath: payload.previousThumbnailPath || null }); }); } module.exports = { registerSlideThumbnailRefreshTask };