@@ -4,20 +4,16 @@ import prisma from "@/prisma/client";
44import { verifyToken } from "@/app/utils/auth" ;
55import { v4 as uuidv4 } from "uuid" ;
66import { S3Client , PutObjectCommand } from "@aws-sdk/client-s3" ;
7+ import envConfig from "@/app/configs/envConfig" ;
78
8- // Cloudflare R2 settings
9- const R2_BUCKET_NAME = process . env . DESTINATION_BUCKET_NAME ;
10- const R2_ACCESS_KEY = process . env . DESTINATION_ACCESS_KEY ;
11- const R2_SECRET_KEY = process . env . DESTINATION_SECRET_KEY ;
12- const R2_ACCOUNT_ID = process . env . DESTINATION_ACCOUNT_ID ;
13- const R2_ENDPOINT = `https://${ R2_ACCOUNT_ID } .r2.cloudflarestorage.com` ;
9+ const R2_ENDPOINT = `https://${ envConfig . cloudflareR2AccessId } .r2.cloudflarestorage.com` ;
1410
1511const s3Client = new S3Client ( {
1612 region : "auto" ,
1713 endpoint : R2_ENDPOINT ,
1814 credentials : {
19- accessKeyId : R2_ACCESS_KEY ! ,
20- secretAccessKey : R2_SECRET_KEY ! ,
15+ accessKeyId : envConfig . cloudflareR2AccessKey ,
16+ secretAccessKey : envConfig . cloudflareR2SecretKey ,
2117 } ,
2218} ) ;
2319
@@ -49,16 +45,16 @@ async function uploadImagesFromContent(content: string): Promise<string> {
4945 // Upload image to Cloudflare R2
5046 const uploadPromise = s3Client . send (
5147 new PutObjectCommand ( {
52- Bucket : R2_BUCKET_NAME ,
48+ Bucket : envConfig . cloudflareR2BucketName ,
5349 Key : newFileName ,
5450 Body : imageBuffer ,
5551 ContentType : contentType , // Set file type based on base64
5652 } )
5753 ) ;
5854
5955 // Create new URL from Cloudflare R2
60- const newImageUrl = `https://assets.bsospace.com /${ newFileName } ` ;
61- content = content . replace ( imgSrc , newImageUrl ) ; // Replace original URL with new URL
56+ const newImageUrl = `${ envConfig . cloudflareR2Domain } /${ newFileName } ` ;
57+ content = content . replace ( imgSrc , newImageUrl ) ;
6258
6359 uploads . push ( uploadPromise ) ;
6460 }
0 commit comments