Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(files): serve rendered documents instead of source code#6139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
ee582e0c2214f06a72bbaabe134a78c16ec5d3107f1270bcba98f272b9060e1e3d3ba5acf9397f5ef0ebcb21c93File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| const { mockDownloadServableFileFromStorage, mockVerifyFileAccess } = vi.hoisted(() => ({ | ||
| mockDownloadServableFileFromStorage: vi.fn(), | ||
| mockVerifyFileAccess: vi.fn(), | ||
| })) | ||
| vi.mock('@/lib/uploads/utils/file-utils.server', () => ({ | ||
| downloadServableFileFromStorage: mockDownloadServableFileFromStorage, | ||
| })) | ||
| vi.mock('@/app/api/files/authorization', () => ({ | ||
| verifyFileAccess: mockVerifyFileAccess, | ||
| })) | ||
| import { readUserFileContent } from '@/lib/execution/payloads/materialization.server' | ||
| import type { UserFile } from '@/executor/types' | ||
| const PDF_SOURCE = Buffer.from('from reportlab.pdfgen import canvas') | ||
| const PDF_BYTES = Buffer.from('%PDF-1.4 rendered bytes') | ||
| const generatedPdf: UserFile = { | ||
| id: 'file-1', | ||
| name: 'report.pdf', | ||
| url: '', | ||
| size: PDF_SOURCE.length, | ||
| type: 'text/x-python-pdf', | ||
| key: 'workspace/2f1d8c3e-5b6a-4c7d-8e9f-0a1b2c3d4e5f/1700000000000-abc1234-report.pdf', | ||
| } | ||
| describe('readUserFileContent', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| generatedPdf.size = PDF_SOURCE.length | ||
| mockVerifyFileAccess.mockResolvedValue(true) | ||
| mockDownloadServableFileFromStorage.mockResolvedValue({ | ||
| buffer: PDF_BYTES, | ||
| contentType: 'application/pdf', | ||
| }) | ||
| }) | ||
| it('returns the compiled artifact instead of the stored generation source', async () => { | ||
| const content = await readUserFileContent(generatedPdf, { | ||
| userId: 'user-1', | ||
| encoding: 'base64', | ||
| }) | ||
| expect(mockDownloadServableFileFromStorage).toHaveBeenCalledOnce() | ||
| expect(content).toBe(PDF_BYTES.toString('base64')) | ||
| expect(content).not.toBe(PDF_SOURCE.toString('base64')) | ||
| expect(generatedPdf.size).toBe(PDF_BYTES.length) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -10,8 +10,12 @@ import { | ||
| } from '@/lib/execution/payloads/large-value-ref' | ||
| import { ExecutionResourceLimitError } from '@/lib/execution/resource-errors' | ||
| import type { StorageContext } from '@/lib/uploads' | ||
| import { bufferToBase64, inferContextFromKey } from '@/lib/uploads/utils/file-utils' | ||
| import { downloadFileFromStorage } from '@/lib/uploads/utils/file-utils.server' | ||
| import { | ||
| bufferToBase64, | ||
| inferContextFromKey, | ||
| isGeneratedDocumentSourceType, | ||
| } from '@/lib/uploads/utils/file-utils' | ||
| import { downloadServableFileFromStorage } from '@/lib/uploads/utils/file-utils.server' | ||
| import type { UserFile } from '@/executor/types' | ||
| const logger = createLogger('ExecutionPayloadMaterialization') | ||
| @@ -267,6 +271,11 @@ export async function assertUserFileContentAccess( | ||
| } | ||
| } | ||
| /** | ||
| * Reads the bytes a consumer should receive. For generated documents, updates the | ||
| * file's size to the rendered artifact size so downstream attachment routing does | ||
| * not make decisions from the smaller generation-source size. | ||
| */ | ||
| export async function readUserFileContent( | ||
| file: unknown, | ||
| options: ReadUserFileContentOptions | ||
| @@ -291,9 +300,14 @@ export async function readUserFileContent( | ||
| const requestId = options.requestId ?? 'unknown' | ||
| try { | ||
| buffer = await downloadFileFromStorage(file, requestId, log, { maxBytes: maxSourceBytes }) | ||
| buffer = ( | ||
| await downloadServableFileFromStorage(file, requestId, log, { maxBytes: maxSourceBytes }) | ||
| ).buffer | ||
j15z marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. j15z marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } catch (error) { | ||
| if (isPayloadSizeLimitError(error)) { | ||
| if (isGeneratedDocumentSourceType(file.type) && error.observedBytes !== undefined) { | ||
| file.size = error.observedBytes | ||
| } | ||
| throw new ExecutionResourceLimitError({ | ||
| resource: 'execution_payload_bytes', | ||
| attemptedBytes: error.observedBytes ?? maxSourceBytes + 1, | ||
| @@ -306,6 +320,9 @@ export async function readUserFileContent( | ||
| if (!buffer) { | ||
| throw new Error(`File content for ${file.name} is unavailable.`) | ||
| } | ||
| if (isGeneratedDocumentSourceType(file.type)) { | ||
| file.size = buffer.length | ||
| } | ||
| if (buffer.length > maxSourceBytes) { | ||
| throw new ExecutionResourceLimitError({ | ||
| resource: 'execution_payload_bytes', | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.