Skip to content

Commit

Permalink
feat: prevent overload from anon users
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Feb 18, 2023
1 parent 56ed9e4 commit 9a1eff2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/lib/misc/getLimitMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const getLimitMessage =
() => `Your request has hit the limit, there is a max to prevent abuse.
<a href="https://alemayhu.com/patreon">Become a patron</a> to remove
the limit.`;
4 changes: 4 additions & 0 deletions src/lib/storage/jobs/ConversionJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export default class ConversionJob {
return this.setStatus('failed');
}

cancelled() {
return this.setStatus('cancelled');
}

async createWorkSpace(api: NotionAPIWrapper) {
await this.setStatus('step1_create_workspace');
if (!this.raw) {
Expand Down
11 changes: 8 additions & 3 deletions src/lib/storage/jobs/helpers/performConversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import NotionAPIWrapper from '../../../notion/NotionAPIWrapper';
import DB from '../../db';
import StorageHandler from '../../StorageHandler';
import { notifyUserIfNecessary } from './notifyUserIfNecessary';
import { getLimitMessage } from '../../../misc/getLimitMessage';

interface ConversionRequest {
title: string | null;
Expand All @@ -25,14 +26,18 @@ export default async function performConversion({
}: ConversionRequest) {
try {
console.log(`Performing conversion for ${id}`);

const storage = new StorageHandler();
const job = new ConversionJob(DB);
await job.load(id, owner, title);
if (!job.canStart()) {
console.log(`job ${id} was not started`);
return res
? res.status(405).send({ message: 'Job is already active' })
: null;
return res ? res.status(500).send('Job is already active') : null;
}
const jobs = await DB('jobs').where({ owner }).returning(['*']);
if (!res?.locals.patreon && jobs.length > 1) {
await job.cancelled();
return res ? res.status(500).send(getLimitMessage()) : null;
}

console.log(`job ${id} is not active, starting`);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export type JobStatus =
| 'step2_creating_flashcards'
| 'step3_building_deck'
| 'stale'
| 'failed';
| 'failed'
| 'cancelled';

export interface Job {
id: string;
Expand Down
5 changes: 2 additions & 3 deletions src/routes/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import deleteJob from './deleteJob';
import upload from './upload';
import { sendError } from '../../lib/error/sendError';
import TokenHandler from '../../lib/misc/TokenHandler';
import { getLimitMessage } from '../../lib/misc/getLimitMessage';

const router = express.Router();

Expand All @@ -34,9 +35,7 @@ router.post('/file', RequireAllowedOrigin, async (req, res) => {
if (error) {
let msg = error.message;
if (msg === 'File too large') {
msg = `Your upload is too big, there is a max to prevent abuse.
<a href="https://alemayhu.com/patreon">Become a patron</a> to remove
the limit.`;
msg = getLimitMessage();
} else {
sendError(error);
}
Expand Down

0 comments on commit 9a1eff2

Please sign in to comment.