Skip to content

Commit 8752cc0

Browse files
authored
fix(blob): support customMetadata in proxy on put() (#231)
1 parent 5b3b2a8 commit 8752cc0

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/runtime/blob/server/api/_hub/blob/[...pathname].put.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default eventHandler(async (event) => {
1515
const query = getQuery(event)
1616

1717
const contentType = getHeader(event, 'content-type')
18-
const contentLength = Number(getHeader(event, 'content-length') || '0')
18+
const contentLength = getHeader(event, 'content-length') || '0'
1919

2020
const options = { ...query }
2121
if (!options.contentType) {
@@ -24,9 +24,16 @@ export default eventHandler(async (event) => {
2424
if (!options.contentLength) {
2525
options.contentLength = contentLength
2626
}
27+
if (typeof options.customMetadata === 'string') {
28+
try {
29+
options.customMetadata = JSON.parse(options.customMetadata)
30+
} catch (e) {
31+
options.customMetadata = {}
32+
}
33+
}
2734

2835
const stream = getRequestWebStream(event)!
29-
const body = await streamToArrayBuffer(stream, contentLength)
36+
const body = await streamToArrayBuffer(stream, Number(contentLength))
3037

3138
return hubBlob().put(pathname, body, options)
3239
})

test/blob.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,32 @@ describe('Blob', async () => {
7575
})
7676
})
7777

78+
describe('Put', () => {
79+
it('single file with custom metadata', async () => {
80+
const image = images[0]
81+
const file = await fs.readFile(fileURLToPath(new URL('./fixtures/blob/public/' + image.pathname, import.meta.url)))
82+
const result = await $fetch(`/api/_hub/blob/${image.pathname}`, {
83+
method: 'PUT',
84+
body: file,
85+
headers: {
86+
'content-type': image.contentType,
87+
'content-length': image.size
88+
},
89+
query: {
90+
customMetadata: {
91+
hello: 'world'
92+
}
93+
}
94+
})
95+
expect(result).toMatchObject({
96+
...image,
97+
customMetadata: {
98+
hello: 'world'
99+
}
100+
})
101+
})
102+
})
103+
78104
describe('Upload', () => {
79105
it('Upload single file', async () => {
80106
const image = images[0]

0 commit comments

Comments
 (0)