Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 1 addition & 119 deletions src/runtime/blob/server/utils/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { joinURL } from 'ufo'
import { streamToArrayBuffer } from '../../../utils/stream'
import { requireNuxtHubFeature } from '../../../utils/features'
import { getCloudflareAccessHeaders } from '../../../utils/cloudflareAccess'
import type { BlobType, FileSizeUnit, BlobUploadedPart, BlobListResult, BlobMultipartUpload, HandleMPUResponse, BlobMultipartOptions, BlobUploadOptions, BlobPutOptions, BlobEnsureOptions, BlobObject, BlobListOptions, BlobCredentialsOptions, BlobCredentials } from '@nuxthub/core'
import type { BlobType, FileSizeUnit, BlobUploadedPart, BlobListResult, BlobMultipartUpload, BlobMultipartOptions, BlobUploadOptions, BlobPutOptions, BlobEnsureOptions, BlobObject, BlobListOptions, BlobCredentialsOptions, BlobCredentials, HubBlob } from '@nuxthub/core'
import { useRuntimeConfig } from '#imports'

const _r2_buckets: Record<string, R2Bucket> = {}
Expand All @@ -33,124 +33,6 @@ function _useBucket(name: string = 'BLOB') {
throw createError(`Missing Cloudflare ${name} binding (R2)`)
}

interface HubBlob {
/**
* List all the blobs in the bucket (metadata only).
*
* @param options The list options
*
* @example ```ts
* const { blobs } = await hubBlob().list({ limit: 10 })
* ```
*/
list(options?: BlobListOptions): Promise<BlobListResult>
/**
* Serve the blob from the bucket.
*
* @param event The H3 event (needed to set headers for the response)
* @param pathname The pathname of the blob
*
* @example ```ts
* export default eventHandler(async (event) => {
* return hubBlob().serve(event, '/my-image.jpg')
* })
* ```
*/
serve(event: H3Event, pathname: string): Promise<ReadableStream<any>>
/**
* Put a new blob into the bucket.
*
* @param pathname The pathname of the blob
* @param body The blob content
* @param options The put options
*
* @example ```ts
* const blob = await hubBlob().put('/my-image.jpg', file)
* ```
*/
put(pathname: string, body: string | ReadableStream<any> | ArrayBuffer | ArrayBufferView | Blob, options?: BlobPutOptions): Promise<BlobObject>
/**
* Get the blob metadata from the bucket.
*
* @param pathname The pathname of the blob
*
* @example ```ts
* const blobMetadata = await hubBlob().head('/my-image.jpg')
* ```
*/
head(pathname: string): Promise<BlobObject>
/**
* Get the blob body from the bucket.
*
* @param pathname The pathname of the blob
*
* @example ```ts
* const blob = await hubBlob().get('/my-image.jpg')
* ```
*/
get(pathname: string): Promise<Blob | null>
/**
* Delete the blob from the bucket.
*
* @param pathnames The pathname of the blob
*
* @example ```ts
* await hubBlob().del('/my-image.jpg')
* ```
*/
del(pathnames: string | string[]): Promise<void>
/**
* Delete the blob from the bucket.
*
* @param pathnames The pathname of the blob
*
* @example ```ts
* await hubBlob().delete('/my-image.jpg')
* ```
*/
delete(pathnames: string | string[]): Promise<void>
/**
* Create a multipart upload.
*
* @see https://hub.nuxt.com/docs/features/blob#createmultipartupload
*/
createMultipartUpload(pathname: string, options?: BlobMultipartOptions): Promise<BlobMultipartUpload>
/**
* Get the specified multipart upload.
*
* @see https://hub.nuxt.com/docs/features/blob#resumemultipartupload
*/
resumeMultipartUpload(pathname: string, uploadId: string): BlobMultipartUpload
/**
* Handle the multipart upload request.
* Make sure your route includes `[action]` and `[...pathname]` params.
*
* @see https://hub.nuxt.com/docs/features/blob#handlemultipartupload
*/
handleMultipartUpload(event: H3Event, options?: BlobMultipartOptions): Promise<HandleMPUResponse>
/**
* Handle a file upload.
*
* @param event The H3 event (needed to set headers for the response)
* @param options The upload options
*
* @see https://hub.nuxt.com/docs/features/blob#handleupload
*/
handleUpload(event: H3Event, options?: BlobUploadOptions): Promise<BlobObject[]>
/**
* Creates temporary access credentials that can be optionally scoped to prefixes or objects.
*
* Useful to create a signed url to upload directory to R2 from client-side.
*
* Only available in production or in development with `--remote` flag.
*
* @example ```ts
* const { accountId, bucketName, accessKeyId, secretAccessKey, sessionToken } = await hubBlob().createCredentials()
* ```
*/
createCredentials(options?: BlobCredentialsOptions): Promise<BlobCredentials>
}

/**
* Access the Blob storage.
*
Expand Down
119 changes: 119 additions & 0 deletions src/types/blob.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ReadableStream, R2HTTPMetadata } from '@cloudflare/workers-types/experimental'
import type { MimeType } from '@uploadthing/mime-types'
import type { H3Event } from 'h3'

// Credits from shared utils of https://github.com/pingdotgg/uploadthing
export type PowOf2 = 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024
Expand Down Expand Up @@ -262,3 +263,121 @@ export interface BlobCredentials {
*/
sessionToken: string
}

export interface HubBlob {
/**
* List all the blobs in the bucket (metadata only).
*
* @param options The list options
*
* @example ```ts
* const { blobs } = await hubBlob().list({ limit: 10 })
* ```
*/
list(options?: BlobListOptions): Promise<BlobListResult>
/**
* Serve the blob from the bucket.
*
* @param event The H3 event (needed to set headers for the response)
* @param pathname The pathname of the blob
*
* @example ```ts
* export default eventHandler(async (event) => {
* return hubBlob().serve(event, '/my-image.jpg')
* })
* ```
*/
serve(event: H3Event, pathname: string): Promise<ReadableStream<any>>
/**
* Put a new blob into the bucket.
*
* @param pathname The pathname of the blob
* @param body The blob content
* @param options The put options
*
* @example ```ts
* const blob = await hubBlob().put('/my-image.jpg', file)
* ```
*/
put(pathname: string, body: string | ReadableStream<any> | ArrayBuffer | ArrayBufferView | Blob, options?: BlobPutOptions): Promise<BlobObject>
/**
* Get the blob metadata from the bucket.
*
* @param pathname The pathname of the blob
*
* @example ```ts
* const blobMetadata = await hubBlob().head('/my-image.jpg')
* ```
*/
head(pathname: string): Promise<BlobObject>
/**
* Get the blob body from the bucket.
*
* @param pathname The pathname of the blob
*
* @example ```ts
* const blob = await hubBlob().get('/my-image.jpg')
* ```
*/
get(pathname: string): Promise<Blob | null>
/**
* Delete the blob from the bucket.
*
* @param pathnames The pathname of the blob
*
* @example ```ts
* await hubBlob().del('/my-image.jpg')
* ```
*/
del(pathnames: string | string[]): Promise<void>
/**
* Delete the blob from the bucket.
*
* @param pathnames The pathname of the blob
*
* @example ```ts
* await hubBlob().delete('/my-image.jpg')
* ```
*/
delete(pathnames: string | string[]): Promise<void>
/**
* Create a multipart upload.
*
* @see https://hub.nuxt.com/docs/features/blob#createmultipartupload
*/
createMultipartUpload(pathname: string, options?: BlobMultipartOptions): Promise<BlobMultipartUpload>
/**
* Get the specified multipart upload.
*
* @see https://hub.nuxt.com/docs/features/blob#resumemultipartupload
*/
resumeMultipartUpload(pathname: string, uploadId: string): BlobMultipartUpload
/**
* Handle the multipart upload request.
* Make sure your route includes `[action]` and `[...pathname]` params.
*
* @see https://hub.nuxt.com/docs/features/blob#handlemultipartupload
*/
handleMultipartUpload(event: H3Event, options?: BlobMultipartOptions): Promise<HandleMPUResponse>
/**
* Handle a file upload.
*
* @param event The H3 event (needed to set headers for the response)
* @param options The upload options
*
* @see https://hub.nuxt.com/docs/features/blob#handleupload
*/
handleUpload(event: H3Event, options?: BlobUploadOptions): Promise<BlobObject[]>
/**
* Creates temporary access credentials that can be optionally scoped to prefixes or objects.
*
* Useful to create a signed url to upload directory to R2 from client-side.
*
* Only available in production or in development with `--remote` flag.
*
* @example ```ts
* const { accountId, bucketName, accessKeyId, secretAccessKey, sessionToken } = await hubBlob().createCredentials()
* ```
*/
createCredentials(options?: BlobCredentialsOptions): Promise<BlobCredentials>
}
Loading