Skip to content

Commit 3eef519

Browse files
fix(blob): set correct customMetadata (nuxt-hub#146)
Co-authored-by: Sébastien Chopin <seb@nuxt.com>
1 parent cc35d14 commit 3eef519

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

docs/content/1.docs/2.storage/3.blob.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,8 @@ interface BlobObject {
648648
pathname: string
649649
contentType: string | undefined
650650
size: number
651-
uploadedAt: Date
651+
uploadedAt: Date,
652+
customMetadata?: Record<string, string> | undefined
652653
}
653654
```
654655

src/runtime/server/utils/blob.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export interface BlobObject {
3030
* The date the blob was uploaded at.
3131
*/
3232
uploadedAt: Date
33+
/**
34+
* The custom metadata of the blob.
35+
*/
36+
customMetadata?: Record<string, string>
3337
}
3438

3539
export interface BlobUploadedPart {
@@ -146,7 +150,7 @@ export type HandleMPUResponse =
146150
action: 'abort'
147151
}
148152

149-
export interface BlobUploadOptions extends BlobPutOptions, BlobValidateOptions {
153+
export interface BlobUploadOptions extends BlobPutOptions, BlobEnsureOptions {
150154
/**
151155
* The key to get the file/files from the request form.
152156
* @default 'files'
@@ -159,7 +163,7 @@ export interface BlobUploadOptions extends BlobPutOptions, BlobValidateOptions {
159163
multiple?: boolean
160164
}
161165

162-
export interface BlobValidateOptions {
166+
export interface BlobEnsureOptions {
163167
/**
164168
* The maximum size of the blob (e.g. '1MB')
165169
*/
@@ -394,14 +398,19 @@ export function hubBlob(): HubBlob {
394398
return mapR2MpuToBlobMpu(mpu)
395399
},
396400
async handleUpload(event: H3Event, options: BlobUploadOptions = {}) {
397-
const opts = { formKey: 'files', multiple: true, ...options } as BlobUploadOptions
401+
options = defu(options, {
402+
formKey: 'files',
403+
multiple: true
404+
})
405+
const { formKey, multiple, ...opts } = options
406+
const { maxSize, types, ...putOptions } = opts
398407

399408
const form = await readFormData(event)
400-
const files = form.getAll(opts.formKey || 'files') as File[]
409+
const files = form.getAll(formKey || 'files') as File[]
401410
if (!files) {
402411
throw createError({ statusCode: 400, message: 'Missing files' })
403412
}
404-
if (!opts.multiple && files.length > 1) {
413+
if (!multiple && files.length > 1) {
405414
throw createError({ statusCode: 400, message: 'Multiple files are not allowed' })
406415
}
407416

@@ -410,11 +419,11 @@ export function hubBlob(): HubBlob {
410419
// Ensure the files meet the requirements
411420
if (options.maxSize || options.types?.length) {
412421
for (const file of files) {
413-
ensureBlob(file, opts)
422+
ensureBlob(file, { maxSize, types })
414423
}
415424
}
416425
for (const file of files) {
417-
const object = await blob.put(file.name!, file, opts)
426+
const object = await blob.put(file.name!, file, putOptions)
418427
objects.push(object)
419428
}
420429
} catch (e: any) {
@@ -712,7 +721,8 @@ function mapR2ObjectToBlob(object: R2Object): BlobObject {
712721
pathname: object.key,
713722
contentType: object.httpMetadata?.contentType,
714723
size: object.size,
715-
uploadedAt: object.uploaded
724+
uploadedAt: object.uploaded,
725+
customMetadata: object.customMetadata || {}
716726
}
717727
}
718728

@@ -770,7 +780,7 @@ function fileSizeToBytes(input: string) {
770780
*
771781
* @throws If the blob does not meet the requirements
772782
*/
773-
export function ensureBlob(blob: Blob, options: BlobValidateOptions) {
783+
export function ensureBlob(blob: Blob, options: BlobEnsureOptions) {
774784
requireNuxtHubFeature('blob')
775785

776786
if (!options.maxSize && !options.types?.length) {

0 commit comments

Comments
 (0)