@@ -3,20 +3,17 @@ import prisma from "@/prisma/client";
33import { slugify } from "@/app/utils/slugify" ;
44import { v4 as uuidv4 } from "uuid" ;
55import { S3Client , PutObjectCommand } from "@aws-sdk/client-s3" ;
6+ import envConfig from "@/app/configs/envConfig" ;
67
7- // Cloudflare R2 settings
8- const R2_BUCKET_NAME = process . env . R2_BUCKET_NAME ;
9- const R2_ACCESS_KEY = process . env . R2_ACCESS_KEY ;
10- const R2_SECRET_KEY = process . env . R2_SECRET_KEY ;
11- const R2_ACCOUNT_ID = process . env . R2_ACCOUNT_ID ;
12- const R2_ENDPOINT = `https://${ R2_ACCOUNT_ID } .r2.cloudflarestorage.com` ;
8+
9+ const R2_ENDPOINT = `https://${ envConfig . cloudflareR2AccessId } .r2.cloudflarestorage.com` ;
1310
1411const s3Client = new S3Client ( {
1512 region : "auto" ,
1613 endpoint : R2_ENDPOINT ,
1714 credentials : {
18- accessKeyId : R2_ACCESS_KEY ! ,
19- secretAccessKey : R2_SECRET_KEY ! ,
15+ accessKeyId : envConfig . cloudflareR2AccessKey ,
16+ secretAccessKey : envConfig . cloudflareR2SecretKey ,
2017 } ,
2118} ) ;
2219
@@ -32,16 +29,6 @@ async function uploadImagesFromContent(content: string): Promise<string> {
3229
3330 // If it's not an external URL (e.g., https://), consider it a blob
3431 const isExternalUrl = imgSrc . startsWith ( "https://" ) ;
35- const oldBucketUrl = imgSrc . startsWith ( "https://bso-image.posyayee.shop/" ) ;
36-
37- if ( oldBucketUrl ) {
38- const newImageUrl = imgSrc . replace (
39- "https://bso-image.posyayee.shop/" ,
40- `https://assets.bsospace.com/`
41- ) ;
42-
43- content = content . replace ( imgSrc , newImageUrl ) ;
44- }
4532
4633 if ( ! isExternalUrl ) {
4734 // Separate the image type and base64 data
@@ -59,15 +46,15 @@ async function uploadImagesFromContent(content: string): Promise<string> {
5946 // Upload image to Cloudflare R2
6047 const uploadPromise = s3Client . send (
6148 new PutObjectCommand ( {
62- Bucket : R2_BUCKET_NAME ,
49+ Bucket : envConfig . cloudflareR2BucketName ,
6350 Key : newFileName ,
6451 Body : imageBuffer ,
6552 ContentType : contentType ,
6653 } )
6754 ) ;
6855
6956 // Create new URL from Cloudflare R2
70- const newImageUrl = `https://bso-image.posyayee.shop /${ newFileName } ` ;
57+ const newImageUrl = `${ envConfig . cloudflareR2Domain } /${ newFileName } ` ;
7158 content = content . replace ( imgSrc , newImageUrl ) ;
7259
7360 uploads . push ( uploadPromise ) ;
0 commit comments