Skip to content

Commit f8f83b8

Browse files
committed
fix(uploads): share MAX_MULTIPART_OVERHEAD_BYTES constant across upload routes
1 parent e45acfe commit f8f83b8

5 files changed

Lines changed: 19 additions & 6 deletions

File tree

apps/sim/app/api/files/upload/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getSession } from '@/lib/auth'
1313
import {
1414
assertKnownSizeWithinLimit,
1515
isPayloadSizeLimitError,
16+
MAX_MULTIPART_OVERHEAD_BYTES,
1617
readFileToBufferWithLimit,
1718
readFormDataWithLimit,
1819
} from '@/lib/core/utils/stream-limits'
@@ -32,7 +33,6 @@ import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
3233
import { createErrorResponse, InvalidRequestError } from '@/app/api/files/utils'
3334

3435
const ALLOWED_EXTENSIONS = new Set<string>(SUPPORTED_ATTACHMENT_EXTENSIONS)
35-
const MAX_MULTIPART_OVERHEAD_BYTES = 1024 * 1024
3636

3737
function validateFileExtension(filename: string): boolean {
3838
const extension = filename.split('.').pop()?.toLowerCase()

apps/sim/app/api/v1/files/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getValidationErrorMessage, parseRequest } from '@/lib/api/server'
77
import { generateRequestId } from '@/lib/core/utils/request'
88
import {
99
isPayloadSizeLimitError,
10+
MAX_MULTIPART_OVERHEAD_BYTES,
1011
readFileToBufferWithLimit,
1112
readFormDataWithLimit,
1213
} from '@/lib/core/utils/stream-limits'
@@ -30,7 +31,6 @@ export const dynamic = 'force-dynamic'
3031
export const revalidate = 0
3132

3233
const MAX_FILE_SIZE = 100 * 1024 * 1024
33-
const MAX_MULTIPART_OVERHEAD_BYTES = 1024 * 1024
3434

3535
/** GET /api/v1/files — List all files in a workspace. */
3636
export const GET = withRouteHandler(async (request: NextRequest) => {

apps/sim/app/api/v1/knowledge/[id]/documents/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
} from '@/lib/api/contracts/v1/knowledge'
77
import { parseRequest } from '@/lib/api/server'
88
import { checkActorUsageLimits } from '@/lib/billing/calculations/usage-monitor'
9-
import { isPayloadSizeLimitError, readFormDataWithLimit } from '@/lib/core/utils/stream-limits'
9+
import {
10+
isPayloadSizeLimitError,
11+
MAX_MULTIPART_OVERHEAD_BYTES,
12+
readFormDataWithLimit,
13+
} from '@/lib/core/utils/stream-limits'
1014
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
1115
import {
1216
createSingleDocument,
@@ -24,7 +28,6 @@ export const dynamic = 'force-dynamic'
2428
export const revalidate = 0
2529

2630
const MAX_FILE_SIZE = 100 * 1024 * 1024 // 100MB
27-
const MAX_MULTIPART_OVERHEAD_BYTES = 1024 * 1024
2831

2932
interface DocumentsRouteParams {
3033
params: Promise<{ id: string }>

apps/sim/app/api/workspaces/[id]/files/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
import { getValidationErrorMessage } from '@/lib/api/server'
1010
import { getSession } from '@/lib/auth'
1111
import { generateRequestId } from '@/lib/core/utils/request'
12-
import { isPayloadSizeLimitError, readFormDataWithLimit } from '@/lib/core/utils/stream-limits'
12+
import {
13+
isPayloadSizeLimitError,
14+
MAX_MULTIPART_OVERHEAD_BYTES,
15+
readFormDataWithLimit,
16+
} from '@/lib/core/utils/stream-limits'
1317
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
1418
import { captureServerEvent } from '@/lib/posthog/server'
1519
import { getSharesForResources } from '@/lib/public-shares/share-manager'
@@ -25,7 +29,6 @@ import { verifyWorkspaceMembership } from '@/app/api/workflows/utils'
2529
export const dynamic = 'force-dynamic'
2630

2731
const logger = createLogger('WorkspaceFilesAPI')
28-
const MAX_MULTIPART_OVERHEAD_BYTES = 1024 * 1024
2932

3033
/**
3134
* GET /api/workspaces/[id]/files

apps/sim/lib/core/utils/stream-limits.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { toError } from '@sim/utils/errors'
22

33
export const DEFAULT_MAX_ERROR_BODY_BYTES = 64 * 1024
44

5+
/**
6+
* Slack for multipart boundary/header overhead on top of the intended file
7+
* size limit, so legitimate uploads near the limit aren't rejected purely
8+
* for encoding overhead.
9+
*/
10+
export const MAX_MULTIPART_OVERHEAD_BYTES = 1024 * 1024
11+
512
export interface PayloadSizeLimitContext {
613
label: string
714
maxBytes: number

0 commit comments

Comments
 (0)