Skip to content

Commit

Permalink
chore(storage): up
Browse files Browse the repository at this point in the history
  • Loading branch information
smarroufin committed Jan 31, 2024
1 parent 7ac3252 commit 5d4b0b1
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 29 deletions.
10 changes: 10 additions & 0 deletions _nuxthub/server/api/_hub/blob/[...pathname].delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default eventHandler(async (event) => {
// TODO: handle authorization
const { pathname } = await getValidatedRouterParams(event, z.object({
pathname: z.string().min(1)
}).parse)

await useBlob().delete(pathname)

return setResponseStatus(event, 204)
})
8 changes: 8 additions & 0 deletions _nuxthub/server/api/_hub/blob/[...pathname].get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default eventHandler(async (event) => {
// TODO: handle authorization
const { pathname } = await getValidatedRouterParams(event, z.object({
pathname: z.string().min(1)
}).parse)

return useBlob().serve(event, pathname)
})
13 changes: 13 additions & 0 deletions _nuxthub/server/api/_hub/blob/[...pathname].head.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default eventHandler(async (event) => {
// TODO: handle authorization

const { pathname } = await getValidatedRouterParams(event, z.object({
pathname: z.string().min(1)
}).parse)

const blob = useBlob().head(pathname)

setHeader(event, 'x-blob', JSON.stringify(blob))

return setResponseStatus(event, 204)
})
11 changes: 0 additions & 11 deletions _nuxthub/server/api/_hub/blob/[key].delete.ts

This file was deleted.

11 changes: 0 additions & 11 deletions _nuxthub/server/api/_hub/blob/[key].get.ts

This file was deleted.

35 changes: 28 additions & 7 deletions _nuxthub/server/utils/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,44 @@ export function useBlob () {
const contentType = optionsContentType || (body as Blob).type || getContentType(pathname)

const { dir, ext, name: filename } = parse(pathname)
let key = pathname
if (addRandomSuffix) {
key = joinURL(dir === '.' ? '' : dir, `${filename}-${randomUUID().split('-')[0]}${ext}`)
pathname = joinURL(dir === '.' ? '' : dir, `${filename}-${randomUUID().split('-')[0]}${ext}`)
}

const object = await bucket.put(key, body as any, { httpMetadata: { contentType }, customMetadata })
const object = await bucket.put(pathname, body as any, { httpMetadata: { contentType }, customMetadata })

return mapR2ObjectToBlob(object)
},
async delete (key: string) {
async head (pathname: string) {
if (proxyURL) {
const query: Record<string, any> = {}
const body = await $fetch<void>(joinURL('/api/_hub/blob', pathname), {
baseURL: proxyURL,
method: 'HEAD'
})
console.log('head body', body)
return
}
// Use R2 binding
const object = await bucket.head(pathname)

if (!object) {
throw createError({ message: 'Blob not found', statusCode: 404 })
}

return mapR2ObjectToBlob(object)
},
async delete (pathname: string) {
if (proxyURL) {
const body = await $fetch<void>(`/api/_hub/blob/${pathname}`, {
baseURL: proxyURL,
method: 'DELETE',
})

return $fetch<void>(`/api/_hub/blob/${key}`, { baseURL: proxyURL, method: 'DELETE', query })
console.log('delete body', body)
return
}
// Use R2 binding
return await bucket.delete(key)
return await bucket.delete(pathname)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions _nuxthub/server/utils/zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { z } from 'zod'

0 comments on commit 5d4b0b1

Please sign in to comment.