ditto/src/cron.ts

30 lines
929 B
TypeScript
Raw Normal View History

import { deleteUnattachedMediaByUrl, getUnattachedMedia } from '@/db/unattached-media.ts';
import { cron } from '@/deps.ts';
import { Time } from '@/utils/time.ts';
import { configUploader as uploader } from '@/uploaders/config.ts';
import { cidFromUrl } from '@/utils/ipfs.ts';
/** Delete files that aren't attached to any events. */
async function cleanupMedia() {
2023-12-28 02:19:59 +00:00
console.info('Deleting orphaned media files...');
const until = new Date(Date.now() - Time.minutes(15));
const media = await getUnattachedMedia(until);
for (const { url } of media) {
const cid = cidFromUrl(new URL(url))!;
try {
await uploader.delete(cid);
await deleteUnattachedMediaByUrl(url);
} catch (e) {
console.error(`Failed to delete file ${url}`);
console.error(e);
}
}
2023-12-28 02:19:59 +00:00
console.info(`Removed ${media?.length ?? 0} orphaned media files.`);
}
await cleanupMedia();
cron.every15Minute(cleanupMedia);