From f1b5eb2b8d4be733a37751cf7cbe43d910288cb1 Mon Sep 17 00:00:00 2001 From: Alexander Alemayhu Date: Sun, 7 May 2023 20:08:38 +0200 Subject: [PATCH] fix: remove dangling uploads This handles uploads that have no owners. --- .../storage/jobs/helpers/deleteOldUploads.ts | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/lib/storage/jobs/helpers/deleteOldUploads.ts b/src/lib/storage/jobs/helpers/deleteOldUploads.ts index ef76c9541..a18d29376 100644 --- a/src/lib/storage/jobs/helpers/deleteOldUploads.ts +++ b/src/lib/storage/jobs/helpers/deleteOldUploads.ts @@ -7,24 +7,33 @@ import StorageHandler from '../../StorageHandler'; export const MS_21 = TIME_21_MINUTES_AS_SECONDS * 1000; const MAX_KEYS = 100_000; -export default async function deleteOldUploads(db: Knex) { - const storage = new StorageHandler(); +const deleteNonSubScriberUploads = async ( + db: Knex, + storage: StorageHandler +) => { const query = await db.raw(` SELECT up.key FROM users u JOIN uploads up ON u.id = up.owner WHERE u.patreon = false; `); const nonSubScriberUploads: Uploads[] | undefined = query.rows; + if (!nonSubScriberUploads) { + return; + } - if (nonSubScriberUploads) { - for (const upload of nonSubScriberUploads.flat()) { - await storage.deleteWith(upload.key); - await db('uploads').delete().where('key', upload.key); - } + for (const upload of nonSubScriberUploads.flat()) { + await storage.deleteWith(upload.key); + await db('uploads').delete().where('key', upload.key); } +}; +const deleteDanglingUploads = async (db: Knex, storage: StorageHandler) => { + const query = await db.raw(` + SELECT up.key FROM users u JOIN uploads up ON u.id = up.owner WHERE u.patreon = true; + `); + const subScriberUploads: Uploads[] | [] = query.rows || []; const storedFiles = await storage.getContents(MAX_KEYS); const nonPatreonFiles = storedFiles?.filter( - (f) => f.Key && nonSubScriberUploads?.find((up) => up.key === f.Key) + (f) => f.Key && !subScriberUploads.find((up) => up.key === f.Key) ) || []; for (const file of nonPatreonFiles) { @@ -32,4 +41,10 @@ export default async function deleteOldUploads(db: Knex) { storage.deleteWith(file.Key); } } +}; + +export default async function deleteOldUploads(db: Knex) { + const storage = new StorageHandler(); + await deleteNonSubScriberUploads(db, storage); + await deleteDanglingUploads(db, storage); }