From e4b49b00eccbc86f5f930df48121e05f4c27161a Mon Sep 17 00:00:00 2001 From: Alexandre Bourdin Date: Thu, 9 Apr 2026 16:21:04 +0200 Subject: [PATCH] Merge pull request #32639 from overleaf/fix/figure-modal-zero-byte-file-crash [web] Fix FileSize crash when uploading zero-byte file in figure modal GitOrigin-RevId: 6ce0dfa221c44af4df813231c97aac846214b34d --- .../file-sources/figure-modal-upload-source.tsx | 8 ++++---- .../codemirror-editor-figure-modal.spec.tsx | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/services/web/frontend/js/features/source-editor/components/figure-modal/file-sources/figure-modal-upload-source.tsx b/services/web/frontend/js/features/source-editor/components/figure-modal/file-sources/figure-modal-upload-source.tsx index e8f827676e..a489cafc1a 100644 --- a/services/web/frontend/js/features/source-editor/components/figure-modal/file-sources/figure-modal-upload-source.tsx +++ b/services/web/frontend/js/features/source-editor/components/figure-modal/file-sources/figure-modal-upload-source.tsx @@ -331,10 +331,10 @@ const FileSize: FC<{ size: number; className?: string }> = ({ ['TB', 1e12], ['PB', 1e15], ] - const labelIndex = Math.min( - Math.floor(Math.log10(size) / 3), - BYTE_UNITS.length - 1 - ) + const labelIndex = + size > 0 + ? Math.min(Math.floor(Math.log10(size) / 3), BYTE_UNITS.length - 1) + : 0 const [label, bytesPerUnit] = BYTE_UNITS[labelIndex] const sizeInUnits = Math.round(size / bytesPerUnit) diff --git a/services/web/test/frontend/features/source-editor/components/codemirror-editor-figure-modal.spec.tsx b/services/web/test/frontend/features/source-editor/components/codemirror-editor-figure-modal.spec.tsx index 309a265a10..816f7fe679 100644 --- a/services/web/test/frontend/features/source-editor/components/codemirror-editor-figure-modal.spec.tsx +++ b/services/web/test/frontend/features/source-editor/components/codemirror-editor-figure-modal.spec.tsx @@ -149,6 +149,19 @@ describe('', function () { cy.findByLabelText('File size').should('have.text', '10 B') }) + it('Shows file name and zero size when selecting a zero-byte file', function () { + cy.get('@file-input').selectFile( + { + fileName: 'empty.jpg', + contents: Cypress.Buffer.alloc(0), + mimeType: 'image/jpg', + }, + { force: true } + ) + cy.findByLabelText('File name').should('have.text', 'empty.jpg') + cy.findByLabelText('File size').should('have.text', '0 B') + }) + it('Uploads file when clicking insert', function () { chooseFileFromComputer() cy.get('@uploadRequest').should('not.have.been.called')