Skip to content

Commit c5fb391

Browse files
authored
Merge pull request #106 from bsospace/develop
Develop
2 parents d56b859 + ba260a7 commit c5fb391

2 files changed

Lines changed: 97 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
}

app/configs/envConfig.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
interface IEvnConfig {
3+
[key: string]: string | number | undefined;
4+
nodeEnv: string;
5+
port: number;
6+
developmentUrl?: string;
7+
jwtSecret: string;
8+
databaseUrl: string;
9+
databasePort: number;
10+
databaseName: string;
11+
databaseUser: string;
12+
databasePassword: string;
13+
googleClientId?: string;
14+
googleClientSecret?: string;
15+
gitHubClientId?: string;
16+
gitHubClientSecret?: string;
17+
discordClientId?: string;
18+
discordClientSecret?: string;
19+
cloudflareR2BucketName: string;
20+
cloudflareR2AccessKey: string;
21+
cloudflareR2SecretKey: string;
22+
cloudflareR2AccessId: string;
23+
cloudflareR2Domain: string;
24+
destinationBucketName?: string;
25+
destinationAccountId?: string;
26+
destinationAccessKey?: string;
27+
destinationSecretKey?: string;
28+
}
29+
30+
const envConfig: IEvnConfig = {
31+
nodeEnv: process.env.NODE_ENV || 'development',
32+
port: parseInt(process.env.APP_PORT || '3000', 10),
33+
developmentUrl: process.env.DEVELOPMENT_URL || '',
34+
jwtSecret: process.env.JWT_SECRET || '',
35+
databaseUrl: process.env.DATABASE_URL || '',
36+
databasePort: parseInt(process.env.DATABASE_PORT || '5432', 10),
37+
databaseName: process.env.DATABASE_NAME || '',
38+
databaseUser: process.env.PG_USER || '',
39+
databasePassword: process.env.PG_PASSWORD || '',
40+
googleClientId: process.env.GOOGLE_CLIENT_ID || '',
41+
googleClientSecret: process.env.GOOGLE_CLIENT_SECRET || '',
42+
gitHubClientId: process.env.GITHUB_CLIENT_ID || '',
43+
gitHubClientSecret: process.env.GITHUB_CLIENT_SECRET || '',
44+
discordClientId: process.env.DISCORD_CLIENT_ID || '',
45+
discordClientSecret: process.env.DISCORD_CLIENT_SECRET || '',
46+
cloudflareR2BucketName: process.env.R2_BUCKET_NAME || '',
47+
cloudflareR2AccessKey: process.env.R2_ACCESS_KEY || '',
48+
cloudflareR2SecretKey: process.env.R2_SECRET_KEY || '',
49+
cloudflareR2AccessId: process.env.R2_ACCOUNT_ID || '',
50+
cloudflareR2Domain: process.env.R2_DOMAIN || '',
51+
destinationBucketName: process.env.DESTINATION_BUCKET_NAME || '',
52+
destinationAccountId: process.env.DESTINATION_ACCOUNT_ID || '',
53+
destinationAccessKey: process.env.DESTINATION_ACCESS_KEY || '',
54+
destinationSecretKey: process.env.DESTINATION_SECRET_KEY || ''
55+
};
56+
57+
console.table(envConfig);
58+
59+
export const checkEnvConfig = () => {
60+
// Define only the required fields
61+
const requiredFields: string[] = [
62+
'nodeEnv',
63+
'port',
64+
'jwtSecret',
65+
'databaseUrl',
66+
'databaseName',
67+
'databaseUser',
68+
'databasePassword'
69+
];
70+
71+
for (const field of requiredFields) {
72+
if (!envConfig[field] || envConfig[field] === '') {
73+
throw new Error(`Missing required environment variable: ${field.toUpperCase()}`);
74+
}
75+
}
76+
};
77+
78+
try {
79+
checkEnvConfig();
80+
console.log('All required environment variables are correctly set.');
81+
} catch (error) {
82+
if (error instanceof Error) {
83+
console.error(error.message);
84+
} else {
85+
console.error('An unknown error occurred.');
86+
}
87+
process.exit(1); // Exit if missing required environment variable
88+
}
89+
90+
export default envConfig;

0 commit comments

Comments
 (0)