-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: purge non subscriber uploads (properly this time)
- Loading branch information
Showing
1 changed file
with
12 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,33 @@ | ||
import { Knex } from 'knex'; | ||
import Uploads from '../../../../schemas/public/Uploads'; | ||
import Users from '../../../../schemas/public/Users'; | ||
|
||
import { TIME_21_MINUTES_AS_SECONDS } from '../../../constants'; | ||
import StorageHandler from '../../StorageHandler'; | ||
import { Upload } from '../../types'; | ||
|
||
export const MS_21 = TIME_21_MINUTES_AS_SECONDS * 1000; | ||
const MAX_KEYS = 100_000; | ||
|
||
const getFreeUsers = (db: Knex): Promise<Users[]> => | ||
db('users').where('patreon', 'false').returning(['owner']); | ||
|
||
const getUploadsForUser = (user: Users, db: Knex): Promise<Upload> => | ||
db('uploads').where('owner', user.id).returning(['key']); | ||
|
||
export default async function deleteOldUploads(db: Knex) { | ||
const storage = new StorageHandler(); | ||
const users = await getFreeUsers(db); | ||
const uploads = await Promise.all( | ||
users.map((user) => getUploadsForUser(user, db)) | ||
); | ||
const nonSubScriberUploads: Uploads[] = await db.raw(` | ||
SELECT up.key FROM users u JOIN uploads up ON u.id = up.owner WHERE u.patreon = false; | ||
`); | ||
|
||
for (const upload of uploads.flat()) { | ||
for (const upload of nonSubScriberUploads.flat()) { | ||
console.debug('delete', upload.key); | ||
await storage.deleteWith(upload.key); | ||
await db('uploads').delete().where('key', upload.key); | ||
} | ||
|
||
const files = await storage.getContents(MAX_KEYS); | ||
const query = await db.raw(` | ||
SELECT up.key FROM users u JOIN uploads up ON u.id = up.owner WHERE u.patreon = true; | ||
`); | ||
|
||
const anonUploads = query.rows as Uploads[]; | ||
const storedFiles = await storage.getContents(MAX_KEYS); | ||
const nonPatreonFiles = | ||
files?.filter((f) => f.Key && anonUploads.find((up) => up.key !== f.Key)) || | ||
[]; | ||
for (const no of nonPatreonFiles) { | ||
if (no.Key) { | ||
storage.deleteWith(no.Key); | ||
storedFiles?.filter( | ||
(f) => f.Key && nonSubScriberUploads.find((up) => up.key === f.Key) | ||
) || []; | ||
|
||
for (const file of nonPatreonFiles) { | ||
if (file.Key) { | ||
storage.deleteWith(file.Key); | ||
} | ||
} | ||
} |