From 8752cc045f9fe9c1ec520e0b03c1a7cd2fb03173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Fri, 16 Aug 2024 18:21:58 +0200 Subject: [PATCH] fix(blob): support customMetadata in proxy on `put()` (#231) --- .../server/api/_hub/blob/[...pathname].put.ts | 11 ++++++-- test/blob.test.ts | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/runtime/blob/server/api/_hub/blob/[...pathname].put.ts b/src/runtime/blob/server/api/_hub/blob/[...pathname].put.ts index 5781885e..3aeef042 100644 --- a/src/runtime/blob/server/api/_hub/blob/[...pathname].put.ts +++ b/src/runtime/blob/server/api/_hub/blob/[...pathname].put.ts @@ -15,7 +15,7 @@ export default eventHandler(async (event) => { const query = getQuery(event) const contentType = getHeader(event, 'content-type') - const contentLength = Number(getHeader(event, 'content-length') || '0') + const contentLength = getHeader(event, 'content-length') || '0' const options = { ...query } if (!options.contentType) { @@ -24,9 +24,16 @@ export default eventHandler(async (event) => { if (!options.contentLength) { options.contentLength = contentLength } + if (typeof options.customMetadata === 'string') { + try { + options.customMetadata = JSON.parse(options.customMetadata) + } catch (e) { + options.customMetadata = {} + } + } const stream = getRequestWebStream(event)! - const body = await streamToArrayBuffer(stream, contentLength) + const body = await streamToArrayBuffer(stream, Number(contentLength)) return hubBlob().put(pathname, body, options) }) diff --git a/test/blob.test.ts b/test/blob.test.ts index 57215441..c0548714 100644 --- a/test/blob.test.ts +++ b/test/blob.test.ts @@ -75,6 +75,32 @@ describe('Blob', async () => { }) }) + describe('Put', () => { + it('single file with custom metadata', async () => { + const image = images[0] + const file = await fs.readFile(fileURLToPath(new URL('./fixtures/blob/public/' + image.pathname, import.meta.url))) + const result = await $fetch(`/api/_hub/blob/${image.pathname}`, { + method: 'PUT', + body: file, + headers: { + 'content-type': image.contentType, + 'content-length': image.size + }, + query: { + customMetadata: { + hello: 'world' + } + } + }) + expect(result).toMatchObject({ + ...image, + customMetadata: { + hello: 'world' + } + }) + }) + }) + describe('Upload', () => { it('Upload single file', async () => { const image = images[0]