Skip to content

Commit 0fdd01b

Browse files
committed
chore(storage): improve file customMetadata
1 parent 3974989 commit 0fdd01b

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

_nuxthub/server/utils/bucket.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ export function useBlob (name: string = '') {
8484
const bucket = useBucket(name)
8585

8686
const type = file.type || getContentType(file.filename)
87-
const extension = getExtension(type)
88-
// TODO: ensure filename unicity
89-
const filename = [randomUUID(), extension].filter(Boolean).join('.')
87+
// TODO: ensure key unicity
88+
const key = randomUUID()
9089
const httpMetadata = { contentType: type }
91-
const customMetadata = getMetadata(type, file.data)
90+
const customMetadata: Record<string, any> = {
91+
...getMetadata(type, file.data),
92+
filename: file.filename
93+
}
9294

93-
return await bucket.put(filename, toArrayBuffer(file.data), { httpMetadata, customMetadata })
95+
return await bucket.put(key, toArrayBuffer(file.data), { httpMetadata, customMetadata })
9496
}
9597
},
9698
async delete (key: string) {
@@ -109,10 +111,6 @@ function getContentType (pathOrExtension?: string) {
109111
return (pathOrExtension && mime.getType(pathOrExtension)) || 'application/octet-stream'
110112
}
111113

112-
function getExtension (type?: string) {
113-
return (type && mime.getExtension(type)) || ''
114-
}
115-
116114
function getMetadata (type: string, buffer: Buffer) {
117115
if (type.startsWith('image/')) {
118116
return imageMeta(buffer) as Record<string, any>

pages/storage.vue

+3-2
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ const items = [[{
122122
>
123123
<!-- <img v-if="file.httpMetadata?.contentType?.startsWith('image/')" :src="`/api/storage/${file.key}`" class="h-36 w-full object-cover"> -->
124124
<div class="h-36 w-full flex items-center justify-center p-2 text-center">
125-
{{ file.key }}
125+
{{ file.customMetadata?.filename }}
126126
</div>
127127
<div class="flex flex-col gap-1 p-2 border-t border-gray-200 dark:border-gray-800">
128-
<span class="text-sm font-medium">{{ file.key }}</span>
128+
<span class="text-sm font-medium">{{ file.customMetadata?.filename }}</span>
129+
<span class="text-sm line-clamp-2">{{ file.key }}</span>
129130
<div class="flex items-center justify-between gap-1">
130131
<span class="text-xs truncate">{{ file.httpMetadata?.contentType || '-' }}</span>
131132
<span class="text-xs">{{ file.size ? `${Math.round(file.size / Math.pow(1024, 2) * 100) / 100}MB` : '-' }}</span>

0 commit comments

Comments
 (0)