Skip to content

Commit

Permalink
Refactored env for CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
SujithThirumalaisamy committed Sep 14, 2024
1 parent f077a59 commit edfee30
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ NEXTAUTH_URL="http://localhost:3000"
#
# Bunny CDN
#
CDN_SZ_NAME=
CDN_BASE_PATH=
CDN_API_KEY=
CDN_API_KEY=api-key
CDN_BASE_UPLOAD_URL=https://sg.storage.bunnycdn.com/job-board/assets
CDN_BASE_ACCESS_URL=https://job-board.b-cdn.net/assets
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ Follow these steps to set up the repository locally and run it.
#
# Bunny CDN
#
CDN_SZ_NAME=
CDN_BASE_PATH=
CDN_API_KEY=
CDN_API_KEY=api-key
CDN_BASE_UPLOAD_URL=https://sg.storage.bunnycdn.com/job-board/assets
CDN_BASE_ACCESS_URL=https://job-board.b-cdn.net/assets
```

2. To generate AUTH_SECRET,
Expand Down
7 changes: 4 additions & 3 deletions src/app/api/upload-to-cdn/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextResponse } from 'next/server';
import { v4 as uuidv4 } from 'uuid';

const CDN_BASE_URL = `https://sg.storage.bunnycdn.com/${process.env.CDN_SZ_NAME}${process.env.CDN_BASE_PATH}`;
const CDN_BASE_UPLOAD_URL = process.env.CDN_BASE_UPLOAD_URL;
const CDN_BASE_ACCESS_URL = process.env.CDN_BASE_ACCESS_URL;
const CDN_API_KEY = process.env.CDN_API_KEY!;

export async function POST(req: Request): Promise<NextResponse> {
Expand All @@ -14,7 +15,7 @@ export async function POST(req: Request): Promise<NextResponse> {
return NextResponse.json({ error: 'File is required' }, { status: 400 });
}

const uploadUrl = `${CDN_BASE_URL}/${uniqueFileName}`;
const uploadUrl = `${CDN_BASE_UPLOAD_URL}/${uniqueFileName}`;

const fileBuffer = Buffer.from(await file.arrayBuffer());

Expand All @@ -30,7 +31,7 @@ export async function POST(req: Request): Promise<NextResponse> {
if (response.ok) {
return NextResponse.json({
message: 'File uploaded successfully',
url: uploadUrl,
url: `${CDN_BASE_ACCESS_URL}/${uniqueFileName}`,
});
} else {
return NextResponse.json(
Expand Down

0 comments on commit edfee30

Please sign in to comment.