Skip to content

Commit 0f0cbba

Browse files
authored
Merge pull request #723 from 2anki/fix/bump-server-limit
fix: ensure patrons can at least upload 1GB files
2 parents 5fe6d98 + f58a6e3 commit 0f0cbba

File tree

7 files changed

+54
-13
lines changed

7 files changed

+54
-13
lines changed

src/lib/anki/zip.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import JSZip from 'jszip';
2-
import { MAX_UPLOAD_SIZE } from '../misc/file';
2+
3+
import { getUploadLimits } from '../misc/getUploadLimits';
34
import Package from '../parser/Package';
45

56
interface File {
@@ -17,11 +18,13 @@ class ZipHandler {
1718
this.files = [];
1819
}
1920

20-
async build(zipData: Buffer) {
21+
async build(zipData: Buffer, isPatron: boolean) {
2122
const size = Buffer.byteLength(zipData);
22-
if (size > MAX_UPLOAD_SIZE) {
23+
const limits = getUploadLimits(isPatron);
24+
25+
if (size > limits.fileSize) {
2326
throw new Error(
24-
`Zip data is too big max is ${MAX_UPLOAD_SIZE} but got ${size}`
27+
`Your upload is too big, there is a max of ${size} / ${limits.fileSize} currently. Become a patron to request unlimited access.`
2528
);
2629
}
2730

src/lib/misc/file.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,3 @@ export function FileSizeInMegaBytes(filePath: string): number {
2424
const stats = fs.statSync(filePath);
2525
return BytesToMegaBytes(stats.size);
2626
}
27-
28-
export const MAX_FIELD_SIZE = 2 * 1024 * 1024;
29-
export const MAX_UPLOAD_SIZE = 100 * 1024 * 1024;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getUploadLimits } from './getUploadLimits';
2+
3+
describe('getUploadLimits', () => {
4+
test('not patron', () => {
5+
const limits = getUploadLimits(false);
6+
const about100MB = 104857600;
7+
expect(limits.fileSize).toBe(about100MB);
8+
});
9+
10+
test('patron', () => {
11+
const limits = getUploadLimits(true);
12+
const about1GB = 10485760000;
13+
expect(limits.fileSize).toBe(about1GB);
14+
});
15+
});

src/lib/misc/getUploadLimits.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const MAX_FIELD_SIZE = 2 * 1024 * 1024;
2+
export const MAX_UPLOAD_SIZE = 100 * 1024 * 1024;
3+
4+
export const MAX_FIELD_SIZE_PATRON = MAX_FIELD_SIZE * 10;
5+
export const MAX_UPLOAD_SIZE_PATRON = MAX_UPLOAD_SIZE * 100;
6+
7+
export const getUploadLimits = (isPatron: boolean) => {
8+
return isPatron
9+
? {
10+
fileSize: MAX_UPLOAD_SIZE_PATRON,
11+
fieldSize: MAX_FIELD_SIZE_PATRON,
12+
}
13+
: { fileSize: MAX_UPLOAD_SIZE, fieldSize: MAX_FIELD_SIZE };
14+
};

src/routes/upload/helpers/handleUpload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default async function handleUpload(
5555
} else {
5656
const zipHandler = new ZipHandler();
5757
/* @ts-ignore */
58-
await zipHandler.build(fileContents);
58+
await zipHandler.build(fileContents, res.locals.patreon);
5959
for (const fileName of zipHandler.getFileNames()) {
6060
if (fileName.match(/.html$/) && !fileName.includes('/')) {
6161
const d = await PrepareDeck(fileName, zipHandler.files, settings);

src/routes/upload/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,26 @@ import deleteUpload from './deleteUpload';
99
import getUploads from './getUploads';
1010
import deleteJob from './deleteJob';
1111
import upload from './upload';
12+
import TokenHandler from '../../lib/misc/TokenHandler';
13+
import { captureException } from '@sentry/node';
1214

1315
const router = express.Router();
1416

1517
const storage = new StorageHandler();
1618

17-
router.post('/file', RequireAllowedOrigin, (req, res) => {
19+
router.post('/file', RequireAllowedOrigin, async (req, res) => {
20+
/**
21+
* This endpoint is open for everyone by default so we can't assume the user is a patron.
22+
*/
23+
try {
24+
const user = await TokenHandler.GetUserFrom(req.cookies.token);
25+
if (user) {
26+
res.locals.patreon = user.patreon;
27+
}
28+
} catch (error) {
29+
captureException(error);
30+
}
31+
1832
const u = upload(storage, res.locals.patreon);
1933

2034
u(req, res, (error) => {

src/routes/upload/upload.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import multer from 'multer';
22
import multerS3 from 'multer-s3';
33

4-
import { MAX_FIELD_SIZE, MAX_UPLOAD_SIZE } from '../../lib/misc/file';
4+
import { getUploadLimits } from '../../lib/misc/getUploadLimits';
55
import StorageHandler from '../../lib/storage/StorageHandler';
66

77
export default function upload(storage: StorageHandler, isPatron: boolean) {
8-
const limits = isPatron
9-
? {}
10-
: { fileSize: MAX_UPLOAD_SIZE, fieldSize: MAX_FIELD_SIZE };
8+
const limits = getUploadLimits(isPatron);
119

1210
return multer({
1311
limits,

0 commit comments

Comments
 (0)