From a1abf12a091d35fffa1df165cafde0147985da2c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 18:34:51 +0000 Subject: [PATCH] fix(files): send file content, not a temporary blob: URL, on save FileInput keeps a blob: src until the async base64 read finishes, so a fast save could store the blob: URL and lose the file. The data provider now resolves any pending file to its data URL in create/update before sending. Fixes buzz-code/ops#72 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012GTicqUJeC3ZKmNLvMo6hy --- providers/__tests__/baseDataProvider.test.ts | 23 ++++++++ providers/baseDataProvider.ts | 17 +++--- utils/__tests__/pendingFiles.util.test.ts | 58 ++++++++++++++++++++ utils/pendingFiles.util.ts | 35 ++++++++++++ 4 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 utils/__tests__/pendingFiles.util.test.ts create mode 100644 utils/pendingFiles.util.ts diff --git a/providers/__tests__/baseDataProvider.test.ts b/providers/__tests__/baseDataProvider.test.ts index f889829..c1b9403 100644 --- a/providers/__tests__/baseDataProvider.test.ts +++ b/providers/__tests__/baseDataProvider.test.ts @@ -106,6 +106,18 @@ describe('baseDataProvider', () => { }) ); }); + + it('sends file content instead of a temporary blob: URL', async () => { + const rawFile = new File(['hello'], 'a.txt', { type: 'text/plain' }); + const previousData = { id: 1, fileData: null }; + const data = { id: 1, fileData: { src: 'blob:http://app/123', title: 'a.txt', rawFile } }; + httpClient.mockResolvedValueOnce({ json: data }); + + await provider.update('uploaded_file', { id: 1, previousData, data }); + + const body = JSON.parse(httpClient.mock.calls[0][1].body); + expect(body.fileData.src).toBe('data:text/plain;base64,aGVsbG8='); + }); }); describe('create', () => { @@ -124,6 +136,17 @@ describe('baseDataProvider', () => { ); expect(result).toEqual({ data: { id: 1, title: 'new post' } }); }); + + it('sends file content instead of a temporary blob: URL', async () => { + const rawFile = new File(['hello'], 'a.txt', { type: 'text/plain' }); + const data = { fileData: { src: 'blob:http://app/123', title: 'a.txt', rawFile } }; + httpClient.mockResolvedValueOnce({ json: { id: 1 } }); + + await provider.create('uploaded_file', { data }); + + const body = JSON.parse(httpClient.mock.calls[0][1].body); + expect(body.fileData.src).toBe('data:text/plain;base64,aGVsbG8='); + }); }); describe('delete', () => { diff --git a/providers/baseDataProvider.ts b/providers/baseDataProvider.ts index e41a3ad..8728953 100644 --- a/providers/baseDataProvider.ts +++ b/providers/baseDataProvider.ts @@ -5,6 +5,7 @@ import { DataProvider, fetchUtils, GetListParams, RaRecord } from 'react-admin'; import saveAs from 'file-saver'; import { MAX_PAGE_SIZE } from '@shared/config/settings'; import { parseSort } from '@shared/utils/sortUtil'; +import { resolvePendingFiles } from '@shared/utils/pendingFiles.util'; /** * Maps react-admin queries to a nestjsx/crud powered REST API. @@ -283,9 +284,9 @@ export default (apiUrl: string, httpClient = fetchUtils.fetchJson): ExtendedData })); }, - update: (resource, params) => { + update: async (resource, params) => { // no need to send all fields, only updated fields are enough - const data = countDiff(params.data, params.previousData); + const data = await resolvePendingFiles(countDiff(params.data, params.previousData)); return httpClient(`${apiUrl}/${resource}/${params.id}`, { method: 'PATCH', body: JSON.stringify(data), @@ -304,13 +305,15 @@ export default (apiUrl: string, httpClient = fetchUtils.fetchJson): ExtendedData data: responses.map(({ json }) => json), })), - create: (resource, params) => - httpClient(`${apiUrl}/${resource}`, { + create: async (resource, params) => { + const data = await resolvePendingFiles(params.data); + return httpClient(`${apiUrl}/${resource}`, { method: 'POST', - body: JSON.stringify(params.data), + body: JSON.stringify(data), }).then(({ json }) => ({ - data: { ...params.data, id: json.id } as any, - })), + data: { ...data, id: json.id } as any, + })); + }, createMany: (resource, bulk) => httpClient(`${apiUrl}/${resource}/bulk`, { diff --git a/utils/__tests__/pendingFiles.util.test.ts b/utils/__tests__/pendingFiles.util.test.ts new file mode 100644 index 0000000..308394b --- /dev/null +++ b/utils/__tests__/pendingFiles.util.test.ts @@ -0,0 +1,58 @@ +import { resolvePendingFiles } from '../pendingFiles.util'; +import { readAsDataURL } from '@shared/utils/fileUtil'; + +jest.mock('@shared/utils/fileUtil', () => ({ + readAsDataURL: jest.fn(), +})); + +const mockReadAsDataURL = readAsDataURL as jest.Mock; + +describe('resolvePendingFiles', () => { + beforeEach(() => { + mockReadAsDataURL.mockReset(); + mockReadAsDataURL.mockResolvedValue('data:application/pdf;base64,JVBERi0='); + }); + + it('replaces a blob: src with the data URL of rawFile', async () => { + const rawFile = new File(['%PDF-'], 'a.pdf', { type: 'application/pdf' }); + const result = await resolvePendingFiles({ + title: 'x', + fileData: { src: 'blob:https://app/123', title: 'a.pdf', rawFile }, + }); + + expect(mockReadAsDataURL).toHaveBeenCalledWith(rawFile); + expect(result.fileData.src).toBe('data:application/pdf;base64,JVBERi0='); + expect(result.fileData.title).toBe('a.pdf'); + expect(result.title).toBe('x'); + }); + + it('handles files inside arrays', async () => { + const rawFile = new File(['x'], 'a.png', { type: 'image/png' }); + const result = await resolvePendingFiles({ files: [{ src: 'blob:https://app/1', rawFile }] }); + + expect(result.files[0].src).toBe('data:application/pdf;base64,JVBERi0='); + }); + + it('leaves already converted files untouched', async () => { + const value = { fileData: { src: 'data:application/pdf;base64,AAA', title: 'a.pdf' } }; + const result = await resolvePendingFiles(value); + + expect(mockReadAsDataURL).not.toHaveBeenCalled(); + expect(result).toEqual(value); + }); + + it('ignores a blob: src without rawFile', async () => { + const value = { fileData: { src: 'blob:https://app/1' } }; + const result = await resolvePendingFiles(value); + + expect(mockReadAsDataURL).not.toHaveBeenCalled(); + expect(result).toEqual(value); + }); + + it('passes primitives, null, and dates through', async () => { + const date = new Date('2026-01-01'); + expect(await resolvePendingFiles(null)).toBeNull(); + expect(await resolvePendingFiles(5)).toBe(5); + expect(await resolvePendingFiles({ d: date })).toEqual({ d: date }); + }); +}); diff --git a/utils/pendingFiles.util.ts b/utils/pendingFiles.util.ts new file mode 100644 index 0000000..e07860a --- /dev/null +++ b/utils/pendingFiles.util.ts @@ -0,0 +1,35 @@ +import { readAsDataURL } from '@shared/utils/fileUtil'; + +const isPlainObject = (value: any): value is Record => + value !== null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype; + +const isPendingFile = (value: any): boolean => + isPlainObject(value) && + typeof value.src === 'string' && + value.src.startsWith('blob:') && + typeof Blob !== 'undefined' && + value.rawFile instanceof Blob; + +/** + * Replaces temporary `blob:` URLs of newly picked files with their base64 data URL. + * + * FileInput values hold a `blob:` src (valid only in the current tab) until the file is read. + * Reading happens asynchronously, so a fast save could send the `blob:` URL to the server. + * Resolving here, right before the request, guarantees the server always gets the file content. + */ +export const resolvePendingFiles = async (data: T): Promise => { + if (Array.isArray(data)) { + return Promise.all(data.map(item => resolvePendingFiles(item))) as any; + } + if (!isPlainObject(data)) { + return data; + } + if (isPendingFile(data)) { + const src = await readAsDataURL(data.rawFile); + return { ...data, src }; + } + const entries = await Promise.all( + Object.entries(data).map(async ([key, value]) => [key, await resolvePendingFiles(value)] as const) + ); + return Object.fromEntries(entries) as T; +};