Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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