@@ -7,25 +7,22 @@ import { randomUUID } from 'uncrypto'
7
7
import { parse } from 'pathe'
8
8
import { joinURL } from 'ufo'
9
9
10
- const _buckets : Record < string , R2Bucket > = { }
10
+ const _blobs : Record < string , R2Bucket > = { }
11
11
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 ]
16
16
}
17
17
18
- if ( process . env . NUXT_HUB_URL ) {
19
- console . log ( 'Using R2 local (proxy for useBucket() is not yet supported)' )
20
- }
21
18
// @ts -ignore
22
- const binding = process . env [ bucketName ] || globalThis . __env__ ?. [ bucketName ] || globalThis [ bucketName ]
19
+ const binding = process . env [ name ] || globalThis . __env__ ?. [ name ] || globalThis [ name ]
23
20
if ( ! binding ) {
24
- throw createError ( `Missing Cloudflare R2 binding ${ bucketName } ` )
21
+ throw createError ( `Missing Cloudflare R2 binding ${ name } ` )
25
22
}
26
- _buckets [ bucketName ] = binding as R2Bucket
23
+ _blobs [ name ] = binding as R2Bucket
27
24
28
- return _buckets [ bucketName ]
25
+ return _blobs [ name ]
29
26
}
30
27
31
28
export function useBlob ( ) {
@@ -36,22 +33,22 @@ export function useBlob () {
36
33
if ( proxy ) {
37
34
const query : Record < string , any > = { }
38
35
39
- return $fetch < BlobObject [ ] > ( '/api/_hub/bucket ' , { baseURL : proxy , method : 'GET' , query } )
36
+ return $fetch < BlobObject [ ] > ( '/api/_hub/blob ' , { baseURL : proxy , method : 'GET' , query } )
40
37
} else {
41
- const bucket = useBucket ( )
38
+ const blob = _useBlob ( )
42
39
43
40
const resolvedOptions = defu ( options , {
44
41
limit : 500 ,
45
42
include : [ 'httpMetadata' as const , 'customMetadata' as const ] ,
46
43
} )
47
44
48
45
// 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 )
50
47
let truncated = listed . truncated
51
48
let cursor = listed . truncated ? listed . cursor : undefined
52
49
53
50
while ( truncated ) {
54
- const next = await bucket . list ( {
51
+ const next = await blob . list ( {
55
52
...options ,
56
53
cursor : cursor ,
57
54
} )
@@ -68,10 +65,10 @@ export function useBlob () {
68
65
if ( proxy ) {
69
66
const query : Record < string , any > = { }
70
67
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 } )
72
69
} else {
73
- const bucket = useBucket ( )
74
- const object = await bucket . get ( key )
70
+ const blob = _useBlob ( )
71
+ const object = await blob . get ( key )
75
72
76
73
if ( ! object ) {
77
74
throw createError ( { message : 'File not found' , statusCode : 404 } )
@@ -88,17 +85,17 @@ export function useBlob () {
88
85
if ( proxy ) {
89
86
// TODO
90
87
} 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 )
94
91
95
92
const { dir, ext, name : filename } = parse ( pathname )
96
93
let key = pathname
97
94
if ( addRandomSuffix ) {
98
95
key = joinURL ( dir === '.' ? '' : dir , `${ filename } -${ randomUUID ( ) . split ( '-' ) [ 0 ] } ${ ext } ` )
99
96
}
100
97
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 } )
102
99
103
100
return mapR2ObjectToBlob ( object )
104
101
}
@@ -107,11 +104,11 @@ export function useBlob () {
107
104
if ( proxy ) {
108
105
const query : Record < string , any > = { }
109
106
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 } )
111
108
} else {
112
- const bucket = useBucket ( )
109
+ const blob = _useBlob ( )
113
110
114
- return await bucket . delete ( key )
111
+ return await blob . delete ( key )
115
112
}
116
113
}
117
114
}
0 commit comments