Files
pulse-signage/src/web/lib/background-tasks/tasks-adhoc/thumbnail-refresh-template.js
T

68 lines
2.6 KiB
JavaScript

const { resolvePlayerRegistration } = require('#src/data/player-registry');
const TASK = {
taskType: 'template-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 registerTemplateSlideThumbnailRefreshTask(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('registerTemplateSlideThumbnailRefreshTask requires the template thumbnail dependencies.');
}
// Walk every slide in the template and regenerate its thumbnail.
backgroundTaskQueue.setTaskHandler(TASK.taskType, async function (task) {
const payload = task && task.payload ? task.payload : {};
const templateId = Number(payload.templateId || 0);
if (!Number.isFinite(templateId) || templateId <= 0) {
throw new Error('Template 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.');
}
const [slides] = await pool.query(
'SELECT id, thumbnail_path FROM c_slides WHERE template_id = ? ORDER BY id ASC',
[templateId]
);
for (const slide of slides || []) {
const slideId = Number(slide && slide.id || 0);
if (!Number.isFinite(slideId) || slideId <= 0) {
continue;
}
await captureSlideThumbnail({
pool: pool,
common: common,
mediaDir: mediaDir,
baseUrl: webBaseUrl,
slideId: slideId,
previousThumbnailPath: slide && slide.thumbnail_path ? slide.thumbnail_path : null
});
}
return { templateId: templateId, slideCount: Array.isArray(slides) ? slides.length : 0 };
});
}
module.exports = { registerTemplateSlideThumbnailRefreshTask };