Skip to content

Commit 5d4b0b1

Browse files
committed
chore(storage): up
1 parent 7ac3252 commit 5d4b0b1

File tree

7 files changed

+60
-29
lines changed

7 files changed

+60
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default eventHandler(async (event) => {
2+
// TODO: handle authorization
3+
const { pathname } = await getValidatedRouterParams(event, z.object({
4+
pathname: z.string().min(1)
5+
}).parse)
6+
7+
await useBlob().delete(pathname)
8+
9+
return setResponseStatus(event, 204)
10+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default eventHandler(async (event) => {
2+
// TODO: handle authorization
3+
const { pathname } = await getValidatedRouterParams(event, z.object({
4+
pathname: z.string().min(1)
5+
}).parse)
6+
7+
return useBlob().serve(event, pathname)
8+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default eventHandler(async (event) => {
2+
// TODO: handle authorization
3+
4+
const { pathname } = await getValidatedRouterParams(event, z.object({
5+
pathname: z.string().min(1)
6+
}).parse)
7+
8+
const blob = useBlob().head(pathname)
9+
10+
setHeader(event, 'x-blob', JSON.stringify(blob))
11+
12+
return setResponseStatus(event, 204)
13+
})

_nuxthub/server/api/_hub/blob/[key].delete.ts

-11
This file was deleted.

_nuxthub/server/api/_hub/blob/[key].get.ts

-11
This file was deleted.

_nuxthub/server/utils/blob.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,44 @@ export function useBlob () {
9494
const contentType = optionsContentType || (body as Blob).type || getContentType(pathname)
9595

9696
const { dir, ext, name: filename } = parse(pathname)
97-
let key = pathname
9897
if (addRandomSuffix) {
99-
key = joinURL(dir === '.' ? '' : dir, `${filename}-${randomUUID().split('-')[0]}${ext}`)
98+
pathname = joinURL(dir === '.' ? '' : dir, `${filename}-${randomUUID().split('-')[0]}${ext}`)
10099
}
101100

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

104103
return mapR2ObjectToBlob(object)
105104
},
106-
async delete (key: string) {
105+
async head (pathname: string) {
107106
if (proxyURL) {
108-
const query: Record<string, any> = {}
107+
const body = await $fetch<void>(joinURL('/api/_hub/blob', pathname), {
108+
baseURL: proxyURL,
109+
method: 'HEAD'
110+
})
111+
console.log('head body', body)
112+
return
113+
}
114+
// Use R2 binding
115+
const object = await bucket.head(pathname)
116+
117+
if (!object) {
118+
throw createError({ message: 'Blob not found', statusCode: 404 })
119+
}
120+
121+
return mapR2ObjectToBlob(object)
122+
},
123+
async delete (pathname: string) {
124+
if (proxyURL) {
125+
const body = await $fetch<void>(`/api/_hub/blob/${pathname}`, {
126+
baseURL: proxyURL,
127+
method: 'DELETE',
128+
})
109129

110-
return $fetch<void>(`/api/_hub/blob/${key}`, { baseURL: proxyURL, method: 'DELETE', query })
130+
console.log('delete body', body)
131+
return
111132
}
112133
// Use R2 binding
113-
return await bucket.delete(key)
134+
return await bucket.delete(pathname)
114135
}
115136
}
116137
}

_nuxthub/server/utils/zod.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { z } from 'zod'

0 commit comments

Comments
 (0)