Skip to content

Commit 24f0157

Browse files
enhancement: build w/o env
1 parent a77301f commit 24f0157

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/lib/storage/gcp.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Storage, StorageOptions } from "@google-cloud/storage";
1+
import { Storage, type StorageOptions } from "@google-cloud/storage";
22

33
interface GCPCredentials {
44
type: string;
@@ -13,25 +13,43 @@ interface GCPCredentials {
1313
universe_domain?: string;
1414
}
1515

16-
const credentials: GCPCredentials = JSON.parse(
17-
process.env.GOOGLE_APPLICATION_CREDENTIALS_JSON ?? "{}"
18-
) as GCPCredentials;
16+
let storage: Storage | null = null;
17+
let bucket: ReturnType<Storage["bucket"]> | null = null;
1918

20-
const storage = new Storage({
21-
projectId: process.env.GOOGLE_CLOUD_PROJECT,
22-
credentials,
23-
} as StorageOptions);
19+
// Only initialize if environment variables are present
20+
if (
21+
process.env.GOOGLE_APPLICATION_CREDENTIALS_JSON &&
22+
process.env.GOOGLE_CLOUD_PROJECT &&
23+
process.env.GOOGLE_CLOUD_BUCKET
24+
) {
25+
const credentials: GCPCredentials = JSON.parse(
26+
process.env.GOOGLE_APPLICATION_CREDENTIALS_JSON
27+
) as GCPCredentials;
2428

25-
const bucketName = process.env.GOOGLE_CLOUD_BUCKET ?? "";
26-
const bucket = storage.bucket(bucketName);
29+
storage = new Storage({
30+
projectId: process.env.GOOGLE_CLOUD_PROJECT,
31+
credentials,
32+
} as StorageOptions);
33+
34+
const bucketName = process.env.GOOGLE_CLOUD_BUCKET;
35+
bucket = storage.bucket(bucketName);
36+
}
2737

2838
export async function uploadPDF(folder: string, buffer: Buffer) {
39+
if (!bucket) {
40+
throw new Error("Google Cloud Storage is not configured");
41+
}
42+
const bucketName = process.env.GOOGLE_CLOUD_BUCKET ?? "";
2943
const pdfFilename = `${folder}/${Date.now()}-${Math.random().toString(36).substring(2)}.pdf`;
3044
await bucket.file(pdfFilename).save(buffer, { resumable: false, contentType: "application/pdf" });
3145
return `https://storage.googleapis.com/${bucketName}/${pdfFilename}`;
3246
}
3347

3448
export async function uploadThumbnail(buffer: Buffer, pdfFilename: string) {
49+
if (!bucket) {
50+
throw new Error("Google Cloud Storage is not configured");
51+
}
52+
const bucketName = process.env.GOOGLE_CLOUD_BUCKET ?? "";
3553
const thumbFilename = pdfFilename.replace(".pdf", ".png");
3654
await bucket.file(thumbFilename).save(buffer, { resumable: false, contentType: "image/png" });
3755
return `https://storage.googleapis.com/${bucketName}/${thumbFilename}`;

src/lib/utils/redis.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Redis } from "@upstash/redis";
22

3-
export const redis = new Redis({
4-
url: process.env.UPSTASH_REDIS_REST_URL!,
5-
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
6-
});
3+
export const redis =
4+
process.env.UPSTASH_REDIS_REST_URL && process.env.UPSTASH_REDIS_REST_TOKEN
5+
? new Redis({
6+
url: process.env.UPSTASH_REDIS_REST_URL,
7+
token: process.env.UPSTASH_REDIS_REST_TOKEN,
8+
})
9+
: ({} as Redis);

0 commit comments

Comments
 (0)