The minimalistic document archiving platform.
feat(documents): made file upload limit disableable (#209)
Corentin Thomasset2f2ad90fd3ab
diff --git a/apps/papra-client/src/modules/documents/documents.composables.tsx b/apps/papra-client/src/modules/documents/documents.composables.tsxindex d0354609..0cbbdcce 100644--- a/apps/papra-client/src/modules/documents/documents.composables.tsx+++ b/apps/papra-client/src/modules/documents/documents.composables.tsx@@ -77,6 +77,34 @@ export function useRestoreDocument() {};}+function toastUploadError({ error, file }: { error: Error; file: File }) {+ if (isHttpErrorWithCode({ error, code: 'document.already_exists' })) {+ createToast({+ type: 'error',+ message: 'Document already exists',+ description: `The document ${file.name} already exists, it has not been uploaded.`,+ });++ return;+ }++ if (isHttpErrorWithCode({ error, code: 'document.file_too_big' })) {+ createToast({+ type: 'error',+ message: 'Document too big',+ description: `The document ${file.name} is too big, it has not been uploaded.`,+ });++ return;+ }++ createToast({+ type: 'error',+ message: 'Failed to upload document',+ description: error.message,+ });+}+export function useUploadDocuments({ organizationId }: { organizationId: string }) {const uploadDocuments = async ({ files }: { files: File[] }) => {const throttledInvalidateOrganizationDocumentsQuery = throttle(invalidateOrganizationDocumentsQuery, 500);@@ -84,12 +112,8 @@ export function useUploadDocuments({ organizationId }: { organizationId: stringawait Promise.all(files.map(async (file) => {const [, error] = await safely(uploadDocument({ file, organizationId }));- if (isHttpErrorWithCode({ error, code: 'document.already_exists' })) {- createToast({- type: 'error',- message: 'Document already exists',- description: `The document ${file.name} already exists, it has not been uploaded.`,- });+ if (error) {+ toastUploadError({ error, file });}await throttledInvalidateOrganizationDocumentsQuery({ organizationId });diff --git a/apps/papra-server/src/modules/documents/documents.models.test.ts b/apps/papra-server/src/modules/documents/documents.models.test.tsindex 10631c53..623d6021 100644--- a/apps/papra-server/src/modules/documents/documents.models.test.ts+++ b/apps/papra-server/src/modules/documents/documents.models.test.ts@@ -1,5 +1,5 @@import { describe, expect, test } from 'vitest';-import { buildOriginalDocumentKey, joinStorageKeyParts } from './documents.models';+import { buildOriginalDocumentKey, isDocumentSizeLimitEnabled, joinStorageKeyParts } from './documents.models';describe('documents models', () => {describe('joinStorageKeyParts', () => {@@ -46,4 +46,13 @@ describe('documents models', () => {});});});++ describe('isDocumentSizeLimitEnabled', () => {+ test('the user can disable the document size limit by setting the maxUploadSize to 0', () => {+ expect(isDocumentSizeLimitEnabled({ maxUploadSize: 0 })).to.eql(false);++ expect(isDocumentSizeLimitEnabled({ maxUploadSize: 100 })).to.eql(true);+ expect(isDocumentSizeLimitEnabled({ maxUploadSize: 42 })).to.eql(true);+ });+ });});diff --git a/apps/papra-server/src/modules/documents/documents.models.ts b/apps/papra-server/src/modules/documents/documents.models.tsindex aa6cf8be..e62a14ed 100644--- a/apps/papra-server/src/modules/documents/documents.models.ts+++ b/apps/papra-server/src/modules/documents/documents.models.ts@@ -19,3 +19,7 @@ export function buildOriginalDocumentKey({ documentId, organizationId, fileNameexport function generateDocumentId() {return generateId({ prefix: 'doc' });}++export function isDocumentSizeLimitEnabled({ maxUploadSize }: { maxUploadSize: number }) {+ return maxUploadSize !== 0;+}diff --git a/apps/papra-server/src/modules/documents/documents.routes.ts b/apps/papra-server/src/modules/documents/documents.routes.tsindex a782dd0f..5d390940 100644--- a/apps/papra-server/src/modules/documents/documents.routes.ts+++ b/apps/papra-server/src/modules/documents/documents.routes.ts@@ -12,6 +12,7 @@ import { createSubscriptionsRepository } from '../subscriptions/subscriptions.reimport { createTaggingRulesRepository } from '../tagging-rules/tagging-rules.repository';import { createTagsRepository } from '../tags/tags.repository';import { createDocumentIsNotDeletedError } from './documents.errors';+import { isDocumentSizeLimitEnabled } from './documents.models';import { createDocumentsRepository } from './documents.repository';import { createDocument, ensureDocumentExists, getDocumentOrThrow } from './documents.usecases';import { createDocumentStorageService } from './storage/documents.storage.services';@@ -34,6 +35,10 @@ function setupCreateDocumentRoute({ app, config, db, trackingServices }: RouteDe(context, next) => {const { maxUploadSize } = config.documentsStorage;+ if (!isDocumentSizeLimitEnabled({ maxUploadSize })) {+ return next();+ }+const middleware = bodyLimit({maxSize: maxUploadSize,onError: () => {diff --git a/apps/papra-server/src/modules/documents/storage/document-storage.config.ts b/apps/papra-server/src/modules/documents/storage/document-storage.config.tsindex ec3472a2..43fb79ad 100644--- a/apps/papra-server/src/modules/documents/storage/document-storage.config.ts+++ b/apps/papra-server/src/modules/documents/storage/document-storage.config.ts@@ -6,8 +6,8 @@ import { S3_STORAGE_DRIVER_NAME } from './drivers/s3/s3.storage-driver';export const documentStorageConfig = {maxUploadSize: {- doc: 'The maximum size in bytes for an uploaded file',- schema: z.coerce.number().int().positive(),+ doc: 'The maximum size in bytes for an uploaded file. Set to 0 to disable the limit and allow uploading documents of any size.',+ schema: z.coerce.number().int().nonnegative(),default: 10 * 1024 * 1024, // 10MBenv: 'DOCUMENT_STORAGE_MAX_UPLOAD_SIZE',},