-
Notifications
You must be signed in to change notification settings - Fork 41
feat: added vercel blob as intermediate layer #657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sachigoyal
wants to merge
4
commits into
Merit-Systems:master
Choose a base branch
from
sachigoyal:sachi/next-image
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| NEXT_PUBLIC_ECHO_APP_ID="74d9c979-e036-4e43-904f-32d214b361fc" | ||
| ECHO_APP_ID="74d9c979-e036-4e43-904f-32d214b361fc" | ||
| BLOB_READ_WRITE_TOKEN= | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| NEXT_PUBLIC_ECHO_APP_ID="74d9c979-e036-4e43-904f-32d214b361fc" | ||
| ECHO_APP_ID="74d9c979-e036-4e43-904f-32d214b361fc" | ||
| ECHO_APP_ID="74d9c979-e036-4e43-904f-32d214b361fc" | ||
|
|
||
| # get your blob read write token from your vercel storage dashboard | ||
| BLOB_READ_WRITE_TOKEN="your-blob-read-write-token-here" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,9 @@ yarn-error.log* | |
| # vercel | ||
| .vercel | ||
|
|
||
| # env | ||
| .env | ||
|
|
||
| # typescript | ||
| *.tsbuildinfo | ||
| next-env.d.ts | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
templates/next-image/src/app/api/blob/upload-handler/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /** | ||
| * API Route: Blob Upload Handler | ||
| * | ||
| * This route handles client-side blob upload token generation. | ||
| * It's called by @vercel/blob/client's upload() function to generate | ||
| * secure tokens that allow direct client-to-blob uploads. | ||
| * | ||
| * SECURITY: | ||
| * - Validates file types (images only) | ||
| * - Sets maximum file size (50MB) | ||
| * - Generates temporary tokens | ||
| */ | ||
|
|
||
| import { handleUpload, type HandleUploadBody } from '@vercel/blob/client'; | ||
| import { NextResponse } from 'next/server'; | ||
|
|
||
| export async function POST(request: Request): Promise<NextResponse> { | ||
| const body = (await request.json()) as HandleUploadBody; | ||
|
|
||
| try { | ||
| const jsonResponse = await handleUpload({ | ||
| body, | ||
| request, | ||
| onBeforeGenerateToken: async (pathname, clientPayload, multipart) => { | ||
| // Here you can add authentication/authorization logic | ||
| // For example: verify user session, check quotas, etc. | ||
|
|
||
| return { | ||
| allowedContentTypes: ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/heic', 'image/heif'], | ||
| maximumSizeInBytes: 50 * 1024 * 1024, // 50MB | ||
| addRandomSuffix: true, // Prevent filename collisions and add obscurity | ||
| }; | ||
| }, | ||
| onUploadCompleted: async ({ blob, tokenPayload }) => { | ||
| // Called after successful upload | ||
| // You can add logging, analytics, etc. here | ||
| console.log('Blob upload completed:', blob.pathname); | ||
| }, | ||
| }); | ||
|
|
||
| return NextResponse.json(jsonResponse); | ||
| } catch (error) { | ||
| console.error('Upload handler error:', error); | ||
| return NextResponse.json( | ||
| { error: error instanceof Error ? error.message : 'Upload failed' }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /** | ||
| * Client-side Vercel Blob utilities | ||
| * | ||
| * This module handles uploading images DIRECTLY to Vercel Blob storage from the client. | ||
| * This bypasses the 4.5MB server request limit entirely! | ||
| */ | ||
|
|
||
| import { upload } from '@vercel/blob/client'; | ||
|
|
||
| export interface BlobUploadResult { | ||
| url: string; | ||
| pathname: string; | ||
| contentType?: string; | ||
| contentDisposition: string; | ||
| downloadUrl: string; | ||
| } | ||
|
|
||
| export async function uploadFileToBlob(file: File): Promise<BlobUploadResult> { | ||
| try { | ||
| const blob = await upload(file.name, file, { | ||
| access: 'public', | ||
| handleUploadUrl: '/api/blob/upload-handler', | ||
| }); | ||
|
|
||
| return blob; | ||
| } catch (error) { | ||
| console.error('Blob upload error:', error); | ||
| throw new Error( | ||
| `Failed to upload file to blob storage: ${error instanceof Error ? error.message : 'Unknown error'}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export async function uploadFilesToBlob( | ||
| files: File[] | ||
| ): Promise<BlobUploadResult[]> { | ||
| if (files.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| const results = await Promise.allSettled( | ||
| files.map(file => uploadFileToBlob(file)) | ||
| ); | ||
|
|
||
| const successful: BlobUploadResult[] = []; | ||
| const failed: { file: File; error: Error }[] = []; | ||
|
|
||
| results.forEach((result, index) => { | ||
| if (result.status === 'fulfilled') { | ||
| successful.push(result.value); | ||
| } else { | ||
| failed.push({ | ||
| file: files[index], | ||
| error: result.reason instanceof Error | ||
| ? result.reason | ||
| : new Error('Unknown upload error'), | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| if (failed.length > 0) { | ||
| console.warn(`${failed.length} of ${files.length} uploads failed:`, failed); | ||
|
|
||
| if (successful.length === 0) { | ||
| throw new Error( | ||
| `All ${files.length} file uploads failed. First error: ${failed[0].error.message}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return successful; | ||
| } | ||
|
|
||
| export async function blobUrlToFile( | ||
| blobUrl: string, | ||
| filename: string | ||
| ): Promise<File> { | ||
| const response = await fetch(blobUrl); | ||
| const blob = await response.blob(); | ||
| return new File([blob], filename, { type: blob.type }); | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.