Skip to content

Commit

Permalink
chore(storage): clean and type useBlob
Browse files Browse the repository at this point in the history
  • Loading branch information
smarroufin committed Jan 31, 2024
1 parent a041a94 commit 20d6ea0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
42 changes: 13 additions & 29 deletions _nuxthub/server/utils/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { R2Bucket, R2ListOptions } from '@cloudflare/workers-types/experimental'
import type { EventHandlerRequest, H3Event } from 'h3'
import type { BlobObject } from '~/_nuxthub/types'
import mime from 'mime'
import { imageMeta } from 'image-meta'
// import { imageMeta } from 'image-meta'
import { defu } from 'defu'
import { randomUUID } from 'uncrypto'
import { parse } from 'pathe'
Expand Down Expand Up @@ -36,7 +36,7 @@ export function useBlob () {
if (proxy) {
const query: Record<string, any> = {}

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

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

export function getMetadata (filename: string, buffer: Buffer) {
const metadata: Record<string, any> = {
contentType: getContentType(filename)
}
// function getMetadata (filename: string, buffer: Buffer) {
// const metadata: Record<string, any> = {
// contentType: getContentType(filename)
// }

if (metadata.contentType.startsWith('image/')) {
Object.assign(metadata, imageMeta(buffer))
}
// if (metadata.contentType.startsWith('image/')) {
// Object.assign(metadata, imageMeta(buffer))
// }

return metadata
}

export function toArrayBuffer (buffer: Buffer) {
const arrayBuffer = new ArrayBuffer(buffer.length)
const view = new Uint8Array(arrayBuffer)
for (let i = 0; i < buffer.length; ++i) {
view[i] = buffer[i]
}
return arrayBuffer
}

export async function readFiles (event: H3Event<EventHandlerRequest>) {
const files = (await readMultipartFormData(event) || [])

// Filter only files
return files.filter((file) => Boolean(file.filename))
}
// return metadata
// }

function mapR2ObjectToBlob (object: R2Object) {
function mapR2ObjectToBlob (object: R2Object): BlobObject {
return {
pathname: object.key,
contentType: object.httpMetadata?.contentType,
Expand Down
6 changes: 6 additions & 0 deletions _nuxthub/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface BlobObject {
pathname: string
contentType: string | undefined
size: number
uploadedAt: Date
}
9 changes: 6 additions & 3 deletions server/api/storage/index.put.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import type { BlobObject } from '~/_nuxthub/types'

export default eventHandler(async (event) => {
await requireUserSession(event)

const files = await readFiles(event)
const form = await readFormData(event)
const files = form.getAll('files') as File[]
if (!files) {
throw createError({ statusCode: 400, message: 'Missing files' })
}

const { put } = useBlob()
const objects = []
const objects: BlobObject[] = []
try {
for (const file of files) {
const object = await put(file.filename!, toArrayBuffer(file.data), { addRandomSuffix: true, ...getMetadata(file.filename!, file.data) })
const object = await put(file.name, file, { addRandomSuffix: true })
objects.push(object)
}
} catch (e: any) {
Expand Down

0 comments on commit 20d6ea0

Please sign in to comment.