Skip to content

Commit

Permalink
fix: remove dangling uploads
Browse files Browse the repository at this point in the history
This handles uploads that have no owners.
  • Loading branch information
aalemayhu committed May 7, 2023
1 parent 3f08e79 commit f1b5eb2
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/lib/storage/jobs/helpers/deleteOldUploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,44 @@ 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) {
if (file.Key) {
storage.deleteWith(file.Key);
}
}
};

export default async function deleteOldUploads(db: Knex) {
const storage = new StorageHandler();
await deleteNonSubScriberUploads(db, storage);
await deleteDanglingUploads(db, storage);
}

0 comments on commit f1b5eb2

Please sign in to comment.