From 14c426ba210def43e0433027c107837409c4d407 Mon Sep 17 00:00:00 2001 From: Alex Luckett Date: Wed, 1 Oct 2025 16:08:06 +0100 Subject: [PATCH 1/4] Log mimimum data for client-side to function for file upload --- src/server/plugins/engine/routes/file-upload.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/server/plugins/engine/routes/file-upload.ts b/src/server/plugins/engine/routes/file-upload.ts index 85e768eb3..edd62e24b 100644 --- a/src/server/plugins/engine/routes/file-upload.ts +++ b/src/server/plugins/engine/routes/file-upload.ts @@ -22,11 +22,14 @@ export async function getHandler( return h.response({ error: 'Status check failed' }).code(400) } - return h.response(status) - } catch (err) { + return h.response({ + uploadStatus: status.uploadStatus + }) + } catch (error) { + const errMsg = getErrorMessage(error) request.logger.error( - err, - `[uploadStatusFailed] Upload status check failed for uploadId: ${uploadId} - ${getErrorMessage(err)}` + errMsg, + `[uploadStatusFailed] Upload status check failed for uploadId: ${uploadId} - ${errMsg}` ) return h.response({ error: 'Status check error' }).code(500) } From 6cb7659f88fb71a8852c1d5214b22487bd1da6b1 Mon Sep 17 00:00:00 2001 From: Alex Luckett Date: Thu, 2 Oct 2025 15:15:01 +0100 Subject: [PATCH 2/4] Fix buildUploadStatusUrl so it works without a prefix --- src/client/javascripts/file-upload.js | 18 +++++- .../plugins/engine/routes/file-upload.ts | 7 +-- test/client/javascripts/file-upload.test.js | 56 +++++++++++++++---- 3 files changed, 64 insertions(+), 17 deletions(-) diff --git a/src/client/javascripts/file-upload.js b/src/client/javascripts/file-upload.js index 1fdccecd4..913825dc6 100644 --- a/src/client/javascripts/file-upload.js +++ b/src/client/javascripts/file-upload.js @@ -230,13 +230,27 @@ function reloadPage() { /** * Build the upload status URL given the current pathname and the upload ID. + * This only works when called on a file upload page that has a maximum depth of 1 URL segments following the slug. * @param {string} pathname – e.g. window.location.pathname * @param {string} uploadId * @returns {string} e.g. "/form/upload-status/abc123" */ export function buildUploadStatusUrl(pathname, uploadId) { - const pathSegments = pathname.split('/').filter((segment) => segment) - const prefix = pathSegments.length > 0 ? `/${pathSegments[0]}` : '' + // Remove preview markers and duplicate slashes + const normalisedPath = pathname + .replace(/\/preview\/(draft|live)/g, '') + .replace(/\/{2,}/g, '/') + .replace(/\/$/, '') + + const segments = normalisedPath.split('/').filter(Boolean) + + // The slug is always the second to last segment + // The prefix is everything before the slug + const prefix = + segments.length > 2 + ? `/${segments.slice(0, segments.length - 2).join('/')}` + : '' + return `${prefix}/upload-status/${uploadId}` } diff --git a/src/server/plugins/engine/routes/file-upload.ts b/src/server/plugins/engine/routes/file-upload.ts index edd62e24b..40bf37294 100644 --- a/src/server/plugins/engine/routes/file-upload.ts +++ b/src/server/plugins/engine/routes/file-upload.ts @@ -25,11 +25,10 @@ export async function getHandler( return h.response({ uploadStatus: status.uploadStatus }) - } catch (error) { - const errMsg = getErrorMessage(error) + } catch (err) { request.logger.error( - errMsg, - `[uploadStatusFailed] Upload status check failed for uploadId: ${uploadId} - ${errMsg}` + err, + `[uploadStatusFailed] Upload status check failed for uploadId: ${uploadId} - ${getErrorMessage(err)}` ) return h.response({ error: 'Status check error' }).code(500) } diff --git a/test/client/javascripts/file-upload.test.js b/test/client/javascripts/file-upload.test.js index fded23a26..fe30ee589 100644 --- a/test/client/javascripts/file-upload.test.js +++ b/test/client/javascripts/file-upload.test.js @@ -1345,21 +1345,55 @@ describe('File Upload Client JS', () => { }) }) -describe('buildUploadStatusUrl()', () => { - it('builds URL with no prefix for root paths', () => { - expect(buildUploadStatusUrl('/', 'abc')).toBe('/upload-status/abc') - expect(buildUploadStatusUrl('', 'xyz')).toBe('/upload-status/xyz') +describe('buildUploadStatusUrl', () => { + it('works without a prefix to the path', () => { + expect(buildUploadStatusUrl('/my-form/file-upload-page', '123')).toBe( + '/upload-status/123' + ) + + expect( + buildUploadStatusUrl('/preview/draft/my-form/file-upload-page', '123') + ).toBe('/upload-status/123') + + expect( + buildUploadStatusUrl('/preview/live/my-form/file-upload-page', '123') + ).toBe('/upload-status/123') }) - it('uses the first segment as prefix', () => { - expect(buildUploadStatusUrl('/form/mypage', 'id1')).toBe( - '/form/upload-status/id1' + it('works with a prefix to the path', () => { + expect(buildUploadStatusUrl('/form/my-form/file-upload-page', '123')).toBe( + '/form/upload-status/123' ) + + expect( + buildUploadStatusUrl( + '/form/preview/draft/my-form/file-upload-page', + '123' + ) + ).toBe('/form/upload-status/123') + + expect( + buildUploadStatusUrl('/form/preview/live/my-form/file-upload-page', '123') + ).toBe('/form/upload-status/123') }) - it('trims nested segments and trailing slashes', () => { - expect(buildUploadStatusUrl('/one/two/three/', 'id2')).toBe( - '/one/upload-status/id2' - ) + it('works nested prefixes to the path', () => { + expect( + buildUploadStatusUrl('/org/form/my-form/file-upload-page', '123') + ).toBe('/org/form/upload-status/123') + + expect( + buildUploadStatusUrl( + '/org/form/preview/draft/my-form/file-upload-page', + '123' + ) + ).toBe('/org/form/upload-status/123') + + expect( + buildUploadStatusUrl( + '/org/form/preview/live/my-form/file-upload-page', + '123' + ) + ).toBe('/org/form/upload-status/123') }) }) From a8afc620d7812909fd287c06bb60cf482654aa06 Mon Sep 17 00:00:00 2001 From: Alex Luckett Date: Thu, 2 Oct 2025 15:21:25 +0100 Subject: [PATCH 3/4] restore missing test --- test/client/javascripts/file-upload.test.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/client/javascripts/file-upload.test.js b/test/client/javascripts/file-upload.test.js index fe30ee589..edf218d74 100644 --- a/test/client/javascripts/file-upload.test.js +++ b/test/client/javascripts/file-upload.test.js @@ -1346,7 +1346,7 @@ describe('File Upload Client JS', () => { }) describe('buildUploadStatusUrl', () => { - it('works without a prefix to the path', () => { + it('works with no prefix', () => { expect(buildUploadStatusUrl('/my-form/file-upload-page', '123')).toBe( '/upload-status/123' ) @@ -1360,7 +1360,7 @@ describe('buildUploadStatusUrl', () => { ).toBe('/upload-status/123') }) - it('works with a prefix to the path', () => { + it('works with a prefix with a single level', () => { expect(buildUploadStatusUrl('/form/my-form/file-upload-page', '123')).toBe( '/form/upload-status/123' ) @@ -1377,7 +1377,7 @@ describe('buildUploadStatusUrl', () => { ).toBe('/form/upload-status/123') }) - it('works nested prefixes to the path', () => { + it('works with a prefix with multiple levels', () => { expect( buildUploadStatusUrl('/org/form/my-form/file-upload-page', '123') ).toBe('/org/form/upload-status/123') @@ -1396,4 +1396,10 @@ describe('buildUploadStatusUrl', () => { ) ).toBe('/org/form/upload-status/123') }) + + it('trims trailing slashes', () => { + expect(buildUploadStatusUrl('/my-form/file-upload-page/', '123')).toBe( + '/upload-status/123' + ) + }) }) From 4a6f20317dcf1fad4682de0faa831bc0f90fedf4 Mon Sep 17 00:00:00 2001 From: Alex Luckett Date: Fri, 3 Oct 2025 11:27:28 +0100 Subject: [PATCH 4/4] Test correct output for file upload status API --- src/server/index.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/server/index.test.ts b/src/server/index.test.ts index 79c1888c3..f3f741b52 100644 --- a/src/server/index.test.ts +++ b/src/server/index.test.ts @@ -508,7 +508,9 @@ describe('Upload status route', () => { const res = await server.inject(options) expect(res.statusCode).toBe(StatusCodes.OK) - expect(res.result).toEqual(mockStatus) + expect(res.result).toEqual({ + uploadStatus: UploadStatus.ready + }) expect(getUploadStatus).toHaveBeenCalledWith( '123e4567-e89b-12d3-a456-426614174000' )