Skip to content

Commit

Permalink
fix: ensure cleanup of stored files
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Mar 31, 2023
1 parent d696041 commit 9a17b31
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
File renamed without changes.
16 changes: 16 additions & 0 deletions src/lib/storage/jobs/helpers/deleteOldUploads.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Knex } from 'knex';
import Uploads from '../../../../schemas/public/Uploads';
import Users from '../../../../schemas/public/Users';

import { TIME_21_MINUTES_AS_SECONDS } from '../../../constants';
Expand All @@ -25,4 +26,19 @@ export default async function deleteOldUploads(db: Knex) {
await storage.deleteWith(upload.key);
await db('uploads').delete().where('key', upload.key);
}

const files = await storage.getContents();
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 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);
}
}
}
4 changes: 4 additions & 0 deletions src/middleware/RequireAllowedOrigin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextFunction, Request, Response } from 'express';

import { ALLOWED_ORIGINS } from '../lib/constants';
import { addUserFieldsToResponse } from './addUserFieldsToRespone';

const RequireAllowedOrigin = async (
req: Request,
Expand All @@ -18,6 +19,9 @@ const RequireAllowedOrigin = async (
}
console.info(`permitted access to ${origin}`);
res.set('Access-Control-Allow-Origin', origin);

await addUserFieldsToResponse(req, res);

return next();
};

Expand Down
16 changes: 16 additions & 0 deletions src/middleware/addUserFieldsToRespone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Request, Response } from 'express';
import TokenHandler from '../lib/misc/TokenHandler';

export const addUserFieldsToResponse = async (req: Request, res: Response) => {
if (!req.cookies.token) {
return;
}

const user = await TokenHandler.GetUserFrom(req.cookies.token);
if (!user) {
return res.redirect('/login#login');
}

res.locals.owner = user.owner;
res.locals.patreon = user.patreon;
};
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import DB from './lib/storage/db';
import KnexConfig from './KnexConfig';
import TokenHandler from './lib/misc/TokenHandler';
import CrashReporter from './lib/CrashReporter';
import { ScheduleCleanup } from './lib/storage/jobs/JobHandler';
import { ScheduleCleanup } from './lib/storage/jobs/ScheduleCleanup';
import RequireAuthentication from './middleware/RequireAuthentication';
import { Knex } from 'knex';
import { sendError } from './lib/error/sendError';
Expand Down

0 comments on commit 9a17b31

Please sign in to comment.