65 lines
2.2 KiB
JavaScript
65 lines
2.2 KiB
JavaScript
const TASK = {
|
|
taskType: 'template-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 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();
|
|
|
|
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 playerInternalBaseUrl = await fetchPlayerInternalBaseUrl(pool);
|
|
if (!playerInternalBaseUrl) {
|
|
throw new Error('Player internal 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: playerInternalBaseUrl,
|
|
slideId: slideId,
|
|
previousThumbnailPath: slide && slide.thumbnail_path ? slide.thumbnail_path : null
|
|
});
|
|
}
|
|
|
|
return { templateId: templateId, slideCount: Array.isArray(slides) ? slides.length : 0 };
|
|
});
|
|
}
|
|
|
|
module.exports = { registerTemplateSlideThumbnailRefreshTask }; |