42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
const TASK = {
|
|
key: 'unused-upload-sweep',
|
|
title: 'Unused upload sweep',
|
|
category: 'cleanup',
|
|
trigger: 'recurring scheduled task, daily',
|
|
purpose: 'remove uploaded media files that are no longer referenced.',
|
|
taskType: 'recurring-run',
|
|
intervalMs: 24 * 60 * 60 * 1000
|
|
};
|
|
|
|
function registerUnusedUploadSweepTask(options) {
|
|
const backgroundTaskQueue = options && options.backgroundTaskQueue;
|
|
const uploadSyncService = options && options.uploadSyncService;
|
|
const collectUploadPathsFromDirectory = uploadSyncService && uploadSyncService.collectUploadPathsFromDirectory;
|
|
const removeUnusedUploadFiles = uploadSyncService && uploadSyncService.removeUnusedUploadFiles;
|
|
const pool = options && options.pool;
|
|
const mediaDir = String(options && options.mediaDir || '').trim();
|
|
|
|
if (!backgroundTaskQueue || typeof collectUploadPathsFromDirectory !== 'function' || typeof removeUnusedUploadFiles !== 'function' || !pool || !mediaDir) {
|
|
throw new Error('registerUnusedUploadSweepTask requires the unused upload sweep dependencies.');
|
|
}
|
|
|
|
backgroundTaskQueue.registerRecurringTask({
|
|
key: TASK.key,
|
|
title: TASK.title,
|
|
category: TASK.category,
|
|
intervalMs: TASK.intervalMs,
|
|
metadata: {
|
|
mediaDir: mediaDir
|
|
},
|
|
run: async function () {
|
|
const uploadPaths = await collectUploadPathsFromDirectory(mediaDir);
|
|
if (!uploadPaths.length) {
|
|
return;
|
|
}
|
|
|
|
await removeUnusedUploadFiles(pool, mediaDir, uploadPaths);
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = { registerUnusedUploadSweepTask }; |