Skip to content

Commit

Permalink
docs: improve blob
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Feb 26, 2024
1 parent 998decf commit 66d8c9e
Showing 1 changed file with 8 additions and 31 deletions.
39 changes: 8 additions & 31 deletions docs/content/docs/2.storage/3.blob.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Returns an array of [`BlobObject`](#blobobject).

Returns a blob's data.

```ts[/api/blob/[...pathname\\].get.ts]
```ts [server/api/files/[...pathname\\].get.ts]
export default eventHandler(async (event) => {
const { pathname } = getRouterParams(event)

Expand Down Expand Up @@ -62,16 +62,8 @@ If you are fetching an image with a server route similar to the one above, you m

Returns a blob's metadata.

```ts[/api/blob/[...pathname\\].head.ts]
export default eventHandler(async (event) => {
const { pathname } = getRouterParams(event)
const blob = await useBlob().head(pathname)
setHeader(event, 'x-blob', JSON.stringify(blob))
return sendNoContent(event)
})
```ts
const blob = await useBlob().head(pathname)
```

#### Params
Expand All @@ -86,7 +78,7 @@ Returns a [`BlobObject`](#blobobject).

Uploads a blob to the storage.

```ts [server/api/upload.post.ts]
```ts [server/api/files.post.ts]
export default eventHandler(async (event) => {
const { pathname } = getRouterParams(event)
const form = await readFormData(event)
Expand Down Expand Up @@ -116,7 +108,7 @@ Returns a [`BlobObject`](#blobobject).

Deletes a blob.

```ts[/api/blob/[...pathname\\].delete.ts]
```ts [server/api/files/[...pathname\\].delete.ts]
export default eventHandler(async (event) => {
const { pathname } = getRouterParams(event)

Expand All @@ -139,24 +131,9 @@ Returns nothing.

`ensureBlob()` is a handy util to validate a `Blob` by checking its size and type:

```ts [server/api/upload.post.ts]
export default eventHandler(async (event) => {
const form = await readFormData(event)
const file = form.get('file') as Blob

if (!file || !file.size) {
throw createError({
statusCode: 400,
message: 'No file provided'
})
}

// Will throw an error if the file is not an image or is larger than 1MB
ensureBlob(file, { maxSize: '1MB', types: ['image' ]})

// Save the image
return useBlob().put(`images/${file.name}`, file)
})
```ts
// Will throw an error if the file is not an image or is larger than 1MB
ensureBlob(file, { maxSize: '1MB', types: ['image' ]})
```

### Params
Expand Down

0 comments on commit 66d8c9e

Please sign in to comment.