Skip to content

Commit

Permalink
fix: ensure patrons can at least upload 1GB files
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Jun 25, 2022
1 parent 5fe6d98 commit f58a6e3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/lib/anki/zip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import JSZip from 'jszip';
import { MAX_UPLOAD_SIZE } from '../misc/file';

import { getUploadLimits } from '../misc/getUploadLimits';
import Package from '../parser/Package';

interface File {
Expand All @@ -17,11 +18,13 @@ class ZipHandler {
this.files = [];
}

async build(zipData: Buffer) {
async build(zipData: Buffer, isPatron: boolean) {
const size = Buffer.byteLength(zipData);
if (size > MAX_UPLOAD_SIZE) {
const limits = getUploadLimits(isPatron);

if (size > limits.fileSize) {
throw new Error(
`Zip data is too big max is ${MAX_UPLOAD_SIZE} but got ${size}`
`Your upload is too big, there is a max of ${size} / ${limits.fileSize} currently. Become a patron to request unlimited access.`
);
}

Expand Down
3 changes: 0 additions & 3 deletions src/lib/misc/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ export function FileSizeInMegaBytes(filePath: string): number {
const stats = fs.statSync(filePath);
return BytesToMegaBytes(stats.size);
}

export const MAX_FIELD_SIZE = 2 * 1024 * 1024;
export const MAX_UPLOAD_SIZE = 100 * 1024 * 1024;
15 changes: 15 additions & 0 deletions src/lib/misc/getUploadLimits.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getUploadLimits } from './getUploadLimits';

describe('getUploadLimits', () => {
test('not patron', () => {
const limits = getUploadLimits(false);
const about100MB = 104857600;
expect(limits.fileSize).toBe(about100MB);
});

test('patron', () => {
const limits = getUploadLimits(true);
const about1GB = 10485760000;
expect(limits.fileSize).toBe(about1GB);
});
});
14 changes: 14 additions & 0 deletions src/lib/misc/getUploadLimits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const MAX_FIELD_SIZE = 2 * 1024 * 1024;
export const MAX_UPLOAD_SIZE = 100 * 1024 * 1024;

export const MAX_FIELD_SIZE_PATRON = MAX_FIELD_SIZE * 10;
export const MAX_UPLOAD_SIZE_PATRON = MAX_UPLOAD_SIZE * 100;

export const getUploadLimits = (isPatron: boolean) => {
return isPatron
? {
fileSize: MAX_UPLOAD_SIZE_PATRON,
fieldSize: MAX_FIELD_SIZE_PATRON,
}
: { fileSize: MAX_UPLOAD_SIZE, fieldSize: MAX_FIELD_SIZE };
};
2 changes: 1 addition & 1 deletion src/routes/upload/helpers/handleUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default async function handleUpload(
} else {
const zipHandler = new ZipHandler();
/* @ts-ignore */
await zipHandler.build(fileContents);
await zipHandler.build(fileContents, res.locals.patreon);
for (const fileName of zipHandler.getFileNames()) {
if (fileName.match(/.html$/) && !fileName.includes('/')) {
const d = await PrepareDeck(fileName, zipHandler.files, settings);
Expand Down
16 changes: 15 additions & 1 deletion src/routes/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@ import deleteUpload from './deleteUpload';
import getUploads from './getUploads';
import deleteJob from './deleteJob';
import upload from './upload';
import TokenHandler from '../../lib/misc/TokenHandler';
import { captureException } from '@sentry/node';

const router = express.Router();

const storage = new StorageHandler();

router.post('/file', RequireAllowedOrigin, (req, res) => {
router.post('/file', RequireAllowedOrigin, async (req, res) => {
/**
* This endpoint is open for everyone by default so we can't assume the user is a patron.
*/
try {
const user = await TokenHandler.GetUserFrom(req.cookies.token);
if (user) {
res.locals.patreon = user.patreon;
}
} catch (error) {
captureException(error);
}

const u = upload(storage, res.locals.patreon);

u(req, res, (error) => {
Expand Down
6 changes: 2 additions & 4 deletions src/routes/upload/upload.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import multer from 'multer';
import multerS3 from 'multer-s3';

import { MAX_FIELD_SIZE, MAX_UPLOAD_SIZE } from '../../lib/misc/file';
import { getUploadLimits } from '../../lib/misc/getUploadLimits';
import StorageHandler from '../../lib/storage/StorageHandler';

export default function upload(storage: StorageHandler, isPatron: boolean) {
const limits = isPatron
? {}
: { fileSize: MAX_UPLOAD_SIZE, fieldSize: MAX_FIELD_SIZE };
const limits = getUploadLimits(isPatron);

return multer({
limits,
Expand Down

0 comments on commit f58a6e3

Please sign in to comment.