From 0b867afd8ed284487211383249dd198fa8882e51 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 9 Sep 2023 19:27:39 -0500 Subject: [PATCH] unattached-media: delete orphaned attachments after 15 minutes --- src/cron.ts | 26 ++++++++++++++++++++++++++ src/db/unattached-media.ts | 23 ++++++++++++++++++++++- src/utils/ipfs.ts | 2 +- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/cron.ts b/src/cron.ts index a08fdf6..b29ab19 100644 --- a/src/cron.ts +++ b/src/cron.ts @@ -1,6 +1,9 @@ import * as eventsDB from '@/db/events.ts'; +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'; /** Clean up old remote events. */ async function cleanupEvents() { @@ -14,6 +17,29 @@ async function cleanupEvents() { console.log(`Cleaned up ${result?.numDeletedRows ?? 0} old remote events.`); } +/** Delete files that aren't attached to any events. */ +async function cleanupMedia() { + console.log('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); + } + } + + console.log(`Removed ${media?.length ?? 0} orphaned media files.`); +} + await cleanupEvents(); +await cleanupMedia(); cron.every15Minute(cleanupEvents); +cron.every15Minute(cleanupMedia); diff --git a/src/db/unattached-media.ts b/src/db/unattached-media.ts index b13474c..409b365 100644 --- a/src/db/unattached-media.ts +++ b/src/db/unattached-media.ts @@ -27,4 +27,25 @@ function insertUnattachedMedia(media: Omit