From 9a1eff263a49ef7d6e0dbae02ba483be7eba195a Mon Sep 17 00:00:00 2001 From: Alexander Alemayhu Date: Sat, 18 Feb 2023 18:58:19 +0100 Subject: [PATCH] feat: prevent overload from anon users --- src/lib/misc/getLimitMessage.ts | 4 ++++ src/lib/storage/jobs/ConversionJob.ts | 4 ++++ src/lib/storage/jobs/helpers/performConversion.ts | 11 ++++++++--- src/lib/storage/types.ts | 3 ++- src/routes/upload/index.ts | 5 ++--- 5 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 src/lib/misc/getLimitMessage.ts diff --git a/src/lib/misc/getLimitMessage.ts b/src/lib/misc/getLimitMessage.ts new file mode 100644 index 000000000..92660f076 --- /dev/null +++ b/src/lib/misc/getLimitMessage.ts @@ -0,0 +1,4 @@ +export const getLimitMessage = + () => `Your request has hit the limit, there is a max to prevent abuse. + Become a patron to remove + the limit.`; diff --git a/src/lib/storage/jobs/ConversionJob.ts b/src/lib/storage/jobs/ConversionJob.ts index e23d1abd5..51d457ce7 100644 --- a/src/lib/storage/jobs/ConversionJob.ts +++ b/src/lib/storage/jobs/ConversionJob.ts @@ -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) { diff --git a/src/lib/storage/jobs/helpers/performConversion.ts b/src/lib/storage/jobs/helpers/performConversion.ts index 49e74a0d1..c2e2947fa 100644 --- a/src/lib/storage/jobs/helpers/performConversion.ts +++ b/src/lib/storage/jobs/helpers/performConversion.ts @@ -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; @@ -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`); diff --git a/src/lib/storage/types.ts b/src/lib/storage/types.ts index 77f3ac8a4..17db94200 100644 --- a/src/lib/storage/types.ts +++ b/src/lib/storage/types.ts @@ -20,7 +20,8 @@ export type JobStatus = | 'step2_creating_flashcards' | 'step3_building_deck' | 'stale' - | 'failed'; + | 'failed' + | 'cancelled'; export interface Job { id: string; diff --git a/src/routes/upload/index.ts b/src/routes/upload/index.ts index fa02b4a3c..6ba390972 100644 --- a/src/routes/upload/index.ts +++ b/src/routes/upload/index.ts @@ -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(); @@ -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. - Become a patron to remove - the limit.`; + msg = getLimitMessage(); } else { sendError(error); }