diff --git a/.env.example b/.env.example index 08562072..03541fec 100644 --- a/.env.example +++ b/.env.example @@ -9,6 +9,7 @@ R2_UPLOAD_IMAGE_ACCESS_KEY_ID= R2_UPLOAD_IMAGE_SECRET_ACCESS_KEY= CLOUDFLARE_ACCOUNT_ID= R2_UPLOAD_IMAGE_BUCKET_NAME=images +R2_PUBLIC_URL= # Auth BETTER_AUTH_SECRET= diff --git a/lib/upload-image.ts b/lib/upload-image.ts index d02e0f16..8c7bb48f 100644 --- a/lib/upload-image.ts +++ b/lib/upload-image.ts @@ -23,6 +23,12 @@ export const uploadImageAssets = async (buffer: Buffer, key: string) => { }) ); - const publicUrl = `https://pub-6f0cf05705c7412b93a792350f3b3aa5.r2.dev/${key}`; + const baseUrl = process.env.R2_PUBLIC_URL; + if (!baseUrl) { + throw new Error("R2_PUBLIC_URL environment variable is not configured"); + } + const normalizedBase = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl; + const publicUrl = `${normalizedBase}/${key}`; + return publicUrl; return publicUrl; }; diff --git a/next.config.ts b/next.config.ts index 35aa4664..db1ee665 100644 --- a/next.config.ts +++ b/next.config.ts @@ -10,7 +10,7 @@ const nextConfig: NextConfig = { remotePatterns: [ { protocol: "https", - hostname: "pub-6f0cf05705c7412b93a792350f3b3aa5.r2.dev", + hostname: process.env.R2_PUBLIC_URL?.replace(/^https?:\/\//, "") || "", }, { protocol: "https",