@@ -7,25 +7,22 @@ import { randomUUID } from 'uncrypto'
77import { parse } from 'pathe'
88import { joinURL } from 'ufo'
99
10- const _buckets : Record < string , R2Bucket > = { }
10+ const _blobs : Record < string , R2Bucket > = { }
1111
12- function useBucket ( ) {
13- const bucketName = 'BUCKET '
14- if ( _buckets [ bucketName ] ) {
15- return _buckets [ bucketName ]
12+ function _useBlob ( ) {
13+ const name = 'BLOB '
14+ if ( _blobs [ name ] ) {
15+ return _blobs [ name ]
1616 }
1717
18- if ( process . env . NUXT_HUB_URL ) {
19- console . log ( 'Using R2 local (proxy for useBucket() is not yet supported)' )
20- }
2118 // @ts -ignore
22- const binding = process . env [ bucketName ] || globalThis . __env__ ?. [ bucketName ] || globalThis [ bucketName ]
19+ const binding = process . env [ name ] || globalThis . __env__ ?. [ name ] || globalThis [ name ]
2320 if ( ! binding ) {
24- throw createError ( `Missing Cloudflare R2 binding ${ bucketName } ` )
21+ throw createError ( `Missing Cloudflare R2 binding ${ name } ` )
2522 }
26- _buckets [ bucketName ] = binding as R2Bucket
23+ _blobs [ name ] = binding as R2Bucket
2724
28- return _buckets [ bucketName ]
25+ return _blobs [ name ]
2926}
3027
3128export function useBlob ( ) {
@@ -36,22 +33,22 @@ export function useBlob () {
3633 if ( proxy ) {
3734 const query : Record < string , any > = { }
3835
39- return $fetch < BlobObject [ ] > ( '/api/_hub/bucket ' , { baseURL : proxy , method : 'GET' , query } )
36+ return $fetch < BlobObject [ ] > ( '/api/_hub/blob ' , { baseURL : proxy , method : 'GET' , query } )
4037 } else {
41- const bucket = useBucket ( )
38+ const blob = _useBlob ( )
4239
4340 const resolvedOptions = defu ( options , {
4441 limit : 500 ,
4542 include : [ 'httpMetadata' as const , 'customMetadata' as const ] ,
4643 } )
4744
4845 // https://developers.cloudflare.com/r2/api/workers/workers-api-reference/#r2listoptions
49- const listed = await bucket . list ( resolvedOptions )
46+ const listed = await blob . list ( resolvedOptions )
5047 let truncated = listed . truncated
5148 let cursor = listed . truncated ? listed . cursor : undefined
5249
5350 while ( truncated ) {
54- const next = await bucket . list ( {
51+ const next = await blob . list ( {
5552 ...options ,
5653 cursor : cursor ,
5754 } )
@@ -68,10 +65,10 @@ export function useBlob () {
6865 if ( proxy ) {
6966 const query : Record < string , any > = { }
7067
71- return $fetch < ReadableStreamDefaultReader < any > > ( `/api/_hub/bucket /${ key } ` , { baseURL : proxy , method : 'GET' , query } )
68+ return $fetch < ReadableStreamDefaultReader < any > > ( `/api/_hub/blob /${ key } ` , { baseURL : proxy , method : 'GET' , query } )
7269 } else {
73- const bucket = useBucket ( )
74- const object = await bucket . get ( key )
70+ const blob = _useBlob ( )
71+ const object = await blob . get ( key )
7572
7673 if ( ! object ) {
7774 throw createError ( { message : 'File not found' , statusCode : 404 } )
@@ -88,17 +85,17 @@ export function useBlob () {
8885 if ( proxy ) {
8986 // TODO
9087 } else {
91- const bucket = useBucket ( )
92- const fileContentType = ( body as Blob ) . type || getContentType ( pathname )
93- const { contentType, addRandomSuffix , ... customMetadata } = options
88+ const blob = _useBlob ( )
89+ const { contentType : optionsContentType , addRandomSuffix , ... customMetadata } = options
90+ const contentType = optionsContentType || ( body as Blob ) . type || getContentType ( pathname )
9491
9592 const { dir, ext, name : filename } = parse ( pathname )
9693 let key = pathname
9794 if ( addRandomSuffix ) {
9895 key = joinURL ( dir === '.' ? '' : dir , `${ filename } -${ randomUUID ( ) . split ( '-' ) [ 0 ] } ${ ext } ` )
9996 }
10097
101- const object = await bucket . put ( key , body as any , { httpMetadata : { contentType : contentType || fileContentType } , customMetadata } )
98+ const object = await blob . put ( key , body as any , { httpMetadata : { contentType } , customMetadata } )
10299
103100 return mapR2ObjectToBlob ( object )
104101 }
@@ -107,11 +104,11 @@ export function useBlob () {
107104 if ( proxy ) {
108105 const query : Record < string , any > = { }
109106
110- return $fetch < void > ( `/api/_hub/bucket /${ key } ` , { baseURL : proxy , method : 'DELETE' , query } )
107+ return $fetch < void > ( `/api/_hub/blob /${ key } ` , { baseURL : proxy , method : 'DELETE' , query } )
111108 } else {
112- const bucket = useBucket ( )
109+ const blob = _useBlob ( )
113110
114- return await bucket . delete ( key )
111+ return await blob . delete ( key )
115112 }
116113 }
117114 }
0 commit comments