2023-09-10 00:27:39 +00:00
|
|
|
import { deleteUnattachedMediaByUrl, getUnattachedMedia } from '@/db/unattached-media.ts';
|
2023-09-05 00:45:33 +00:00
|
|
|
import { cron } from '@/deps.ts';
|
|
|
|
import { Time } from '@/utils/time.ts';
|
2023-09-10 00:27:39 +00:00
|
|
|
import { configUploader as uploader } from '@/uploaders/config.ts';
|
|
|
|
import { cidFromUrl } from '@/utils/ipfs.ts';
|
2023-09-05 00:45:33 +00:00
|
|
|
|
2023-09-10 00:27:39 +00:00
|
|
|
/** 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...');
|
2023-09-10 00:27:39 +00:00
|
|
|
|
|
|
|
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.`);
|
2023-09-10 00:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await cleanupMedia();
|
|
|
|
cron.every15Minute(cleanupMedia);
|