const TASK = { key: 'font-sweep', title: 'Font sweep', category: 'cleanup', trigger: 'recurring scheduled task, daily', purpose: 'reconcile managed fonts on the player and remove stale font files.', taskType: 'recurring-run', intervalMs: 24 * 60 * 60 * 1000 }; const { collectFontLibraryDirectoryUploadPaths, collectFontLibrarySyncOperations } = require('../../media/font-library'); function registerFontSweepTask(options) { const backgroundTaskQueue = options && options.backgroundTaskQueue; const uploadSyncService = options && options.uploadSyncService; const pushUploadFileToPlayer = uploadSyncService && uploadSyncService.pushUploadFileToPlayer; const removeUploadFileFromPlayer = uploadSyncService && uploadSyncService.removeUploadFileFromPlayer; const mediaDir = String(options && options.mediaDir || '').trim(); if (!backgroundTaskQueue || typeof pushUploadFileToPlayer !== 'function' || typeof removeUploadFileFromPlayer !== 'function' || !mediaDir) { throw new Error('registerFontSweepTask requires the font sweep dependencies.'); } backgroundTaskQueue.registerRecurringTask({ key: TASK.key, title: TASK.title, category: TASK.category, intervalMs: TASK.intervalMs, metadata: { mediaDir: mediaDir }, run: async function () { const desiredOperations = collectFontLibrarySyncOperations(mediaDir); const desiredUploadPaths = new Set(desiredOperations.map(function (operation) { return operation && operation.uploadPath ? operation.uploadPath : ''; }).filter(Boolean)); const currentUploadPaths = await collectFontLibraryDirectoryUploadPaths(mediaDir); for (let i = 0; i < desiredOperations.length; i += 1) { const operation = desiredOperations[i] || {}; const uploadPath = String(operation.uploadPath || '').trim(); if (!uploadPath) { continue; } if (String(operation.type || '').trim().toLowerCase() === 'delete') { await removeUploadFileFromPlayer(uploadPath, mediaDir); } else { await pushUploadFileToPlayer(uploadPath, mediaDir); } } for (let i = 0; i < currentUploadPaths.length; i += 1) { const uploadPath = String(currentUploadPaths[i] || '').trim(); if (!uploadPath || desiredUploadPaths.has(uploadPath)) { continue; } await removeUploadFileFromPlayer(uploadPath, mediaDir); } } }); } module.exports = { registerFontSweepTask };