Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

refactor(config): replace hardcoded time and size values with constants (#708)

Corentin Thomasset4878a3f8ddd8
diff --git a/apps/papra-server/src/modules/config/config.ts b/apps/papra-server/src/modules/config/config.ts
index 6424b0b3..ecba6c50 100644
--- a/apps/papra-server/src/modules/config/config.ts
+++ b/apps/papra-server/src/modules/config/config.ts
@@ -16,6 +16,7 @@ import { intakeEmailsConfig } from '../intake-emails/intake-emails.config';
import { organizationsConfig } from '../organizations/organizations.config';
import { organizationPlansConfig } from '../plans/plans.config';
import { createLogger } from '../shared/logger/logger';
+import { IN_MS } from '../shared/units';
import { isString } from '../shared/utils';
import { subscriptionsConfig } from '../subscriptions/subscriptions.config';
import { tasksConfig } from '../tasks/tasks.config';
@@ -87,7 +88,7 @@ export const configDefinition = {
defaultRouteTimeoutMs: {
doc: 'The maximum time in milliseconds for a route to complete before timing out',
schema: z.coerce.number().int().positive(),
- default: 20_000,
+ default: 20 * IN_MS.SECOND,
env: 'SERVER_API_ROUTES_TIMEOUT_MS',
},
routeTimeouts: {
@@ -103,7 +104,7 @@ export const configDefinition = {
{
method: 'POST',
route: '/api/organizations/:organizationId/documents',
- timeoutMs: 5 * 60 * 1000, // 5 minutes for file uploads
+ timeoutMs: 5 * IN_MS.MINUTE,
},
],
},
diff --git a/apps/papra-server/src/modules/ingestion-folders/ingestion-folders.config.ts b/apps/papra-server/src/modules/ingestion-folders/ingestion-folders.config.ts
index d2d876d3..b1f207eb 100644
--- a/apps/papra-server/src/modules/ingestion-folders/ingestion-folders.config.ts
+++ b/apps/papra-server/src/modules/ingestion-folders/ingestion-folders.config.ts
@@ -1,6 +1,7 @@
import type { ConfigDefinition } from 'figue';
import { z } from 'zod';
import { booleanishSchema } from '../config/config.schemas';
+import { IN_MS } from '../shared/units';
import { isString } from '../shared/utils';
import { defaultIgnoredPatterns } from './ingestion-folders.constants';
@@ -27,7 +28,7 @@ export const ingestionFolderConfig = {
pollingInterval: {
doc: 'When polling is used, this is the interval at which the watcher checks for changes in the ingestion folder (in milliseconds)',
schema: z.coerce.number().int().positive(),
- default: 2_000,
+ default: 2 * IN_MS.SECOND,
env: 'INGESTION_FOLDER_WATCHER_POLLING_INTERVAL_MS',
},
},
diff --git a/apps/papra-server/src/modules/plans/plans.repository.ts b/apps/papra-server/src/modules/plans/plans.repository.ts
index 8ec4d22c..d411428c 100644
--- a/apps/papra-server/src/modules/plans/plans.repository.ts
+++ b/apps/papra-server/src/modules/plans/plans.repository.ts
@@ -2,6 +2,7 @@ import type { Config } from '../config/config.types';
import type { OrganizationPlanRecord } from './plans.types';
import { injectArguments } from '@corentinth/chisels';
import { isDocumentSizeLimitEnabled } from '../documents/documents.models';
+import { IN_BYTES } from '../shared/units';
import { FREE_PLAN_ID, PLUS_PLAN_ID, PRO_PLAN_ID } from './plans.constants';
import { createPlanNotFoundError } from './plans.errors';
@@ -30,7 +31,7 @@ export function getOrganizationPlansRecords({ config }: { config: Config }) {
id: FREE_PLAN_ID,
name: 'Free',
limits: {
- maxDocumentStorageBytes: isFreePlanUnlimited ? Number.POSITIVE_INFINITY : 1024 * 1024 * 500, // 500 MiB
+ maxDocumentStorageBytes: isFreePlanUnlimited ? Number.POSITIVE_INFINITY : 500 * IN_BYTES.MEGABYTE,
maxIntakeEmailsCount: isFreePlanUnlimited ? Number.POSITIVE_INFINITY : 1,
maxOrganizationsMembersCount: isFreePlanUnlimited ? Number.POSITIVE_INFINITY : 3,
maxFileSize: isDocumentSizeLimitEnabled({ maxUploadSize }) ? maxUploadSize : Number.POSITIVE_INFINITY,
@@ -42,10 +43,10 @@ export function getOrganizationPlansRecords({ config }: { config: Config }) {
monthlyPriceId: config.organizationPlans.plusPlanMonthlyPriceId,
annualPriceId: config.organizationPlans.plusPlanAnnualPriceId,
limits: {
- maxDocumentStorageBytes: 1024 * 1024 * 1024 * 5, // 5 GiB
+ maxDocumentStorageBytes: 5 * IN_BYTES.GIGABYTE, // 5 GiB
maxIntakeEmailsCount: 10,
maxOrganizationsMembersCount: 10,
- maxFileSize: 1024 * 1024 * 100, // 100 MiB
+ maxFileSize: 100 * IN_BYTES.MEGABYTE, // 100 MiB
},
},
[PRO_PLAN_ID]: {
@@ -54,10 +55,10 @@ export function getOrganizationPlansRecords({ config }: { config: Config }) {
monthlyPriceId: config.organizationPlans.proPlanMonthlyPriceId,
annualPriceId: config.organizationPlans.proPlanAnnualPriceId,
limits: {
- maxDocumentStorageBytes: 1024 * 1024 * 1024 * 50, // 50 GiB
+ maxDocumentStorageBytes: 50 * IN_BYTES.GIGABYTE, // 50 GiB
maxIntakeEmailsCount: 100,
maxOrganizationsMembersCount: 50,
- maxFileSize: 1024 * 1024 * 500, // 500 MiB
+ maxFileSize: 500 * IN_BYTES.MEGABYTE, // 500 MiB
},
},
};
diff --git a/apps/papra-server/src/modules/shared/units.ts b/apps/papra-server/src/modules/shared/units.ts
new file mode 100644
index 00000000..f3a1ba4c
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/units.ts
@@ -0,0 +1,16 @@
+export const IN_MS = {
+ SECOND: 1_000,
+ MINUTE: 60_000, // 60 * 1_000
+ HOUR: 3_600_000, // 60 * 60 * 1_000
+ DAY: 86_400_000, // 24 * 60 * 60 * 1_000
+ WEEK: 604_800_000, // 7 * 24 * 60 * 60 * 1_000
+ MONTH: 2_630_016_000, // 30.44 * 24 * 60 * 60 * 1_000 -- approximation using average month length
+ YEAR: 31_556_736_000, // 365.24 * 24 * 60 * 60 * 1_000 -- approximation using average year length
+};
+
+export const IN_BYTES = {
+ KILOBYTE: 1_024,
+ MEGABYTE: 1_048_576, // 1_024 * 1_024
+ GIGABYTE: 1_073_741_824, // 1_024 * 1_024 * 1_024
+ TERABYTE: 1_099_511_627_776, // 1_024 * 1_024 * 1_024 * 1_024
+};
diff --git a/apps/papra-server/src/modules/tasks/tasks.config.ts b/apps/papra-server/src/modules/tasks/tasks.config.ts
index 54727a85..7f2a939f 100644
--- a/apps/papra-server/src/modules/tasks/tasks.config.ts
+++ b/apps/papra-server/src/modules/tasks/tasks.config.ts
@@ -2,6 +2,7 @@ import type { ConfigDefinition } from 'figue';
import type { TasksDriverName } from './drivers/tasks-driver.constants';
import { z } from 'zod';
import { booleanishSchema } from '../config/config.schemas';
+import { IN_MS } from '../shared/units';
import { tasksDriverNames } from './drivers/tasks-driver.constants';
export const tasksConfig = {
@@ -35,7 +36,7 @@ export const tasksConfig = {
pollIntervalMs: {
doc: 'The interval at which the task persistence driver polls for new tasks',
schema: z.coerce.number().int().positive(),
- default: 1_000,
+ default: 1 * IN_MS.SECOND,
env: 'TASKS_PERSISTENCE_DRIVERS_LIBSQL_POLL_INTERVAL_MS',
},
},