Skip to content

Commit

Permalink
fix(blob): support customMetadata in proxy on put() (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux authored Aug 16, 2024
1 parent 5b3b2a8 commit 8752cc0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/runtime/blob/server/api/_hub/blob/[...pathname].put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
})
26 changes: 26 additions & 0 deletions test/blob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 8752cc0

Please sign in to comment.