Skip to content

Commit 73cb680

Browse files
authored
Merge pull request #112 from bsospace/feature/version
♻️ refactor[posts]: update Cloudflare R2 integration
2 parents c4e4931 + 7a49c10 commit 73cb680

2 files changed

Lines changed: 8 additions & 24 deletions

File tree

Jenkinsfile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pipeline {
3131
default:
3232
env.ENVIRONMENT = 'other'
3333
env.DOCKER_COMPOSE_FILE = ''
34-
echo "Branch ${branchName} is not supported. Skipping deployment."
34+
env.ENV_FILE_CREDENTIAL = 'blog-dev-env-file'
3535
}
3636

3737
echo "Environment: ${env.ENVIRONMENT}"
@@ -41,9 +41,6 @@ pipeline {
4141
}
4242

4343
stage('Setup .env') {
44-
when {
45-
expression { env.ENVIRONMENT != 'other' }
46-
}
4744
steps {
4845
script {
4946
withCredentials([file(credentialsId: env.ENV_FILE_CREDENTIAL, variable: 'SECRET_ENV_FILE')]) {

app/api/posts/edit/[id]/route.ts

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

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

Comments
 (0)