90 lines
3.7 KiB
JavaScript
90 lines
3.7 KiB
JavaScript
// Internal sync trigger routes for player-driven queue flushes.
|
|
|
|
const { verifyRequestAuth } = require('#src/request-auth');
|
|
const { collectFontLibrarySyncOperations } = require('#src/web/lib/media/font-library');
|
|
|
|
function requireRequestAuth(req, res, next) {
|
|
if (!verifyRequestAuth(req)) {
|
|
return res.status(401).json({ error: 'Request authentication required.' });
|
|
}
|
|
|
|
next();
|
|
}
|
|
|
|
module.exports = function registerInternalSyncRoutes(app, deps) {
|
|
const backgroundTaskQueue = deps && deps.backgroundTaskQueue;
|
|
const uploadSyncService = deps && deps.uploadSyncService;
|
|
const mediaDir = String(deps && deps.mediaDir || '').trim();
|
|
|
|
if (!backgroundTaskQueue || typeof backgroundTaskQueue.enqueueTask !== 'function' || !uploadSyncService || !mediaDir) {
|
|
throw new Error('registerInternalSyncRoutes requires the sync dependencies.');
|
|
}
|
|
|
|
app.post('/api/internal/sync/player-media', requireRequestAuth, async function (_req, res, next) {
|
|
try {
|
|
const requestBody = _req.body && typeof _req.body === 'object' && !Array.isArray(_req.body)
|
|
? _req.body
|
|
: {};
|
|
const playerIdentifier = String(requestBody.playerIdentifier || requestBody.deviceId || '').trim();
|
|
const playerPublicBaseUrl = String(requestBody.playerPublicBaseUrl || '').trim();
|
|
const playerInternalBaseUrl = String(requestBody.playerInternalBaseUrl || '').trim();
|
|
const task = await backgroundTaskQueue.enqueueTask({
|
|
key: 'player-media-sync',
|
|
title: 'Player media sync',
|
|
category: 'media-sync',
|
|
taskType: 'media-sync',
|
|
metadata: {
|
|
playerIdentifier: playerIdentifier || null,
|
|
playerPublicBaseUrl: playerPublicBaseUrl || null,
|
|
playerInternalBaseUrl: playerInternalBaseUrl || null,
|
|
playerLabel: playerIdentifier || playerPublicBaseUrl || playerInternalBaseUrl || null
|
|
},
|
|
payload: {
|
|
mode: 'initial',
|
|
uploadDir: mediaDir,
|
|
playerIdentifier: playerIdentifier,
|
|
playerPublicBaseUrl: playerPublicBaseUrl,
|
|
playerInternalBaseUrl: playerInternalBaseUrl
|
|
}
|
|
});
|
|
res.status(202).json({ ok: true, queued: true, task: task });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
app.post('/api/internal/sync/player-font', requireRequestAuth, async function (_req, res, next) {
|
|
try {
|
|
const requestBody = _req.body && typeof _req.body === 'object' && !Array.isArray(_req.body)
|
|
? _req.body
|
|
: {};
|
|
const playerIdentifier = String(requestBody.playerIdentifier || requestBody.deviceId || '').trim();
|
|
const playerPublicBaseUrl = String(requestBody.playerPublicBaseUrl || '').trim();
|
|
const playerInternalBaseUrl = String(requestBody.playerInternalBaseUrl || '').trim();
|
|
const operations = collectFontLibrarySyncOperations(mediaDir);
|
|
const task = await backgroundTaskQueue.enqueueTask({
|
|
key: 'player-font-sync',
|
|
title: 'Player font sync',
|
|
category: 'fonts',
|
|
taskType: 'font-sync',
|
|
metadata: {
|
|
playerIdentifier: playerIdentifier || null,
|
|
playerPublicBaseUrl: playerPublicBaseUrl || null,
|
|
playerInternalBaseUrl: playerInternalBaseUrl || null,
|
|
playerLabel: playerIdentifier || playerPublicBaseUrl || playerInternalBaseUrl || null
|
|
},
|
|
payload: {
|
|
mode: 'initial',
|
|
uploadDir: mediaDir,
|
|
operations: operations,
|
|
playerIdentifier: playerIdentifier,
|
|
playerPublicBaseUrl: playerPublicBaseUrl,
|
|
playerInternalBaseUrl: playerInternalBaseUrl
|
|
}
|
|
});
|
|
res.status(202).json({ ok: true, queued: true, task: task });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
}; |