From 5d4b0b1f1d28ca9f96f07e551f9259db4f2cafa9 Mon Sep 17 00:00:00 2001 From: Sylvain Marroufin Date: Wed, 31 Jan 2024 17:44:29 +0100 Subject: [PATCH] chore(storage): up --- .../api/_hub/blob/[...pathname].delete.ts | 10 ++++++ .../server/api/_hub/blob/[...pathname].get.ts | 8 +++++ .../api/_hub/blob/[...pathname].head.ts | 13 +++++++ _nuxthub/server/api/_hub/blob/[key].delete.ts | 11 ------ _nuxthub/server/api/_hub/blob/[key].get.ts | 11 ------ _nuxthub/server/utils/blob.ts | 35 +++++++++++++++---- _nuxthub/server/utils/zod.ts | 1 + 7 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 _nuxthub/server/api/_hub/blob/[...pathname].delete.ts create mode 100644 _nuxthub/server/api/_hub/blob/[...pathname].get.ts create mode 100644 _nuxthub/server/api/_hub/blob/[...pathname].head.ts delete mode 100644 _nuxthub/server/api/_hub/blob/[key].delete.ts delete mode 100644 _nuxthub/server/api/_hub/blob/[key].get.ts create mode 100644 _nuxthub/server/utils/zod.ts diff --git a/_nuxthub/server/api/_hub/blob/[...pathname].delete.ts b/_nuxthub/server/api/_hub/blob/[...pathname].delete.ts new file mode 100644 index 00000000..a507a7d9 --- /dev/null +++ b/_nuxthub/server/api/_hub/blob/[...pathname].delete.ts @@ -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) +}) diff --git a/_nuxthub/server/api/_hub/blob/[...pathname].get.ts b/_nuxthub/server/api/_hub/blob/[...pathname].get.ts new file mode 100644 index 00000000..0b7632d4 --- /dev/null +++ b/_nuxthub/server/api/_hub/blob/[...pathname].get.ts @@ -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) +}) diff --git a/_nuxthub/server/api/_hub/blob/[...pathname].head.ts b/_nuxthub/server/api/_hub/blob/[...pathname].head.ts new file mode 100644 index 00000000..afa8489f --- /dev/null +++ b/_nuxthub/server/api/_hub/blob/[...pathname].head.ts @@ -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) +}) diff --git a/_nuxthub/server/api/_hub/blob/[key].delete.ts b/_nuxthub/server/api/_hub/blob/[key].delete.ts deleted file mode 100644 index b69c562f..00000000 --- a/_nuxthub/server/api/_hub/blob/[key].delete.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { useValidatedParams, z } from 'h3-zod' - -export default eventHandler(async (event) => { - // TODO: handle authorization - - const { key } = await useValidatedParams(event, { - key: z.string().min(1) - }) - - return useBlob().delete(key) -}) diff --git a/_nuxthub/server/api/_hub/blob/[key].get.ts b/_nuxthub/server/api/_hub/blob/[key].get.ts deleted file mode 100644 index c9847076..00000000 --- a/_nuxthub/server/api/_hub/blob/[key].get.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { useValidatedParams, z } from 'h3-zod' - -export default eventHandler(async (event) => { - // TODO: handle authorization - - const { key } = await useValidatedParams(event, { - key: z.string().min(1) - }) - - return useBlob().get(key) -}) diff --git a/_nuxthub/server/utils/blob.ts b/_nuxthub/server/utils/blob.ts index dc03ab24..934e3e6c 100644 --- a/_nuxthub/server/utils/blob.ts +++ b/_nuxthub/server/utils/blob.ts @@ -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 = {} + const body = await $fetch(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(`/api/_hub/blob/${pathname}`, { + baseURL: proxyURL, + method: 'DELETE', + }) - return $fetch(`/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) } } } diff --git a/_nuxthub/server/utils/zod.ts b/_nuxthub/server/utils/zod.ts new file mode 100644 index 00000000..ca0746b5 --- /dev/null +++ b/_nuxthub/server/utils/zod.ts @@ -0,0 +1 @@ +export { z } from 'zod' \ No newline at end of file