Skip to content

Commit ba260a7

Browse files
committed
♻️ refactor[posts]: refactor Cloudflare R2 integration to use environment configuration for credentials and bucket settings
1 parent 0890e9f commit ba260a7

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

app/api/posts/create/route.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@ import prisma from "@/prisma/client";
44
import { verifyToken } from "@/app/utils/auth";
55
import { v4 as uuidv4 } from "uuid";
66
import { 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

1511
const 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

Comments
 (0)