Skip to content

Commit 20d6ea0

Browse files
committed
chore(storage): clean and type useBlob
1 parent a041a94 commit 20d6ea0

File tree

3 files changed

+25
-32
lines changed

3 files changed

+25
-32
lines changed

_nuxthub/server/utils/bucket.ts

+13-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { R2Bucket, R2ListOptions } from '@cloudflare/workers-types/experimental'
2-
import type { EventHandlerRequest, H3Event } from 'h3'
2+
import type { BlobObject } from '~/_nuxthub/types'
33
import mime from 'mime'
4-
import { imageMeta } from 'image-meta'
4+
// import { imageMeta } from 'image-meta'
55
import { defu } from 'defu'
66
import { randomUUID } from 'uncrypto'
77
import { parse } from 'pathe'
@@ -36,7 +36,7 @@ export function useBlob () {
3636
if (proxy) {
3737
const query: Record<string, any> = {}
3838

39-
return $fetch<R2Object[]>('/api/_hub/bucket', { baseURL: proxy, method: 'GET', query })
39+
return $fetch<BlobObject[]>('/api/_hub/bucket', { baseURL: proxy, method: 'GET', query })
4040
} else {
4141
const bucket = useBucket()
4242

@@ -132,35 +132,19 @@ function getContentType (pathOrExtension?: string) {
132132
return (pathOrExtension && mime.getType(pathOrExtension)) || 'application/octet-stream'
133133
}
134134

135-
export function getMetadata (filename: string, buffer: Buffer) {
136-
const metadata: Record<string, any> = {
137-
contentType: getContentType(filename)
138-
}
135+
// function getMetadata (filename: string, buffer: Buffer) {
136+
// const metadata: Record<string, any> = {
137+
// contentType: getContentType(filename)
138+
// }
139139

140-
if (metadata.contentType.startsWith('image/')) {
141-
Object.assign(metadata, imageMeta(buffer))
142-
}
140+
// if (metadata.contentType.startsWith('image/')) {
141+
// Object.assign(metadata, imageMeta(buffer))
142+
// }
143143

144-
return metadata
145-
}
146-
147-
export function toArrayBuffer (buffer: Buffer) {
148-
const arrayBuffer = new ArrayBuffer(buffer.length)
149-
const view = new Uint8Array(arrayBuffer)
150-
for (let i = 0; i < buffer.length; ++i) {
151-
view[i] = buffer[i]
152-
}
153-
return arrayBuffer
154-
}
155-
156-
export async function readFiles (event: H3Event<EventHandlerRequest>) {
157-
const files = (await readMultipartFormData(event) || [])
158-
159-
// Filter only files
160-
return files.filter((file) => Boolean(file.filename))
161-
}
144+
// return metadata
145+
// }
162146

163-
function mapR2ObjectToBlob (object: R2Object) {
147+
function mapR2ObjectToBlob (object: R2Object): BlobObject {
164148
return {
165149
pathname: object.key,
166150
contentType: object.httpMetadata?.contentType,

_nuxthub/types/index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface BlobObject {
2+
pathname: string
3+
contentType: string | undefined
4+
size: number
5+
uploadedAt: Date
6+
}

server/api/storage/index.put.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
import type { BlobObject } from '~/_nuxthub/types'
2+
13
export default eventHandler(async (event) => {
24
await requireUserSession(event)
35

4-
const files = await readFiles(event)
6+
const form = await readFormData(event)
7+
const files = form.getAll('files') as File[]
58
if (!files) {
69
throw createError({ statusCode: 400, message: 'Missing files' })
710
}
811

912
const { put } = useBlob()
10-
const objects = []
13+
const objects: BlobObject[] = []
1114
try {
1215
for (const file of files) {
13-
const object = await put(file.filename!, toArrayBuffer(file.data), { addRandomSuffix: true, ...getMetadata(file.filename!, file.data) })
16+
const object = await put(file.name, file, { addRandomSuffix: true })
1417
objects.push(object)
1518
}
1619
} catch (e: any) {

0 commit comments

Comments
 (0)