Skip to content

Commit acf9397

Browse files
committed
fix(files): preserve cached artifact size
1 parent e3d3ba5 commit acf9397

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

apps/sim/lib/uploads/utils/user-file-base64.server.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ describe('hydrateUserFilesWithBase64', () => {
149149
expect(hydrated.file.size).toBe(11)
150150
})
151151

152+
it('records cached rendered size when a generated document must use a provider upload path', async () => {
153+
mockGetRedisClient.mockReturnValue(mockRedis)
154+
mockRedis.get.mockResolvedValueOnce(Buffer.alloc(11).toString('base64'))
155+
const file: UserFile = {
156+
id: 'file-1',
157+
name: 'report.pdf',
158+
key: 'workspace/2f1d8c3e-5b6a-4c7d-8e9f-0a1b2c3d4e5f/report.pdf',
159+
url: '',
160+
size: 1,
161+
type: 'text/x-python-pdf',
162+
}
163+
164+
const hydrated = await hydrateUserFilesWithBase64({ file }, { maxBytes: 10, userId: 'user-1' })
165+
166+
expect(hydrated.file).not.toHaveProperty('base64')
167+
expect(hydrated.file.size).toBe(11)
168+
expect(mockDownloadServableFileFromStorage).not.toHaveBeenCalled()
169+
})
170+
152171
it('propagates generated documents that are still compiling', async () => {
153172
const notReady = new Error('Document is still being generated')
154173
notReady.name = 'DocCompileUserError'

apps/sim/lib/uploads/utils/user-file-base64.server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,11 @@ async function hydrateUserFile(
463463
const cached = await state.cache.get(file)
464464
if (cached) {
465465
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BASE64_BYTES
466-
if (Buffer.byteLength(cached, 'base64') > maxBytes) {
466+
const cachedBytes = Buffer.byteLength(cached, 'base64')
467+
if (isGeneratedDocumentSourceType(file.type)) {
468+
file.size = cachedBytes
469+
}
470+
if (cachedBytes > maxBytes) {
467471
return stripBase64(file)
468472
}
469473
return { ...file, base64: cached }

0 commit comments

Comments
 (0)