Skip to content

Commit

Permalink
feat: add custom cdn endpoint configuration for s3 bucket on digital …
Browse files Browse the repository at this point in the history
…ocean

closes #18
  • Loading branch information
0-vortex committed Apr 8, 2023
1 parent 65615c3 commit 077e473
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/config/digital-ocean.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { registerAs } from "@nestjs/config";

const DigitalOceanConfig = registerAs("digitalOcean", () => ({
endpoint: String(process.env.DO_SPACES_ENDPOINT ?? "https://sfo3.digitaloceanspaces.com"),
region: String(process.env.DO_SPACES_REGION ?? "us-east-1"),
accessKeyId: String(process.env.DO_SPACES_ACCESS_KEY_ID ?? ""),
secretAccessKey: String(process.env.DO_SPACES_SECRET_ACCESS_KEY ?? ""),
protocol: String(process.env.DO_SPACES_PROTOCOL ?? "https"),
endpoint: String(process.env.DO_SPACES_ENDPOINT ?? "digitaloceanspaces.com"),
region: String(process.env.DO_SPACES_REGION ?? "sfo3"),
bucketName: String(process.env.DO_SPACES_BUCKET_NAME ?? "opengraph-dev"),
cdnCustomDomain: String(process.env.DO_SPACES_SUBDOMAIN ?? ""),
cdnDisabled: !!process.env.DO_SPACES_CDN_DISABLED,
}));

export default DigitalOceanConfig;
20 changes: 8 additions & 12 deletions src/s3-file-storage/s3-file-storage.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable, Inject } from "@nestjs/common";
import { ConfigType } from "@nestjs/config";
import { S3, HeadObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { createHash } from "node:crypto";
import { Readable } from "node:stream";

import DigitalOceanConfig from "../config/digital-ocean.config";
Expand All @@ -10,25 +9,27 @@ import DigitalOceanConfig from "../config/digital-ocean.config";
export class S3FileStorageService {
private readonly s3Client: S3Client;

public getOriginEndpoint = (): string => `${this.config.protocol}://${this.config.bucketName}.${this.config.region}.${this.config.endpoint}/`;

public getCdnEndpoint = (): string => (this.config.cdnDisabled
? this.getOriginEndpoint()
: `${this.config.protocol}://${this.config.cdnCustomDomain !== "" ? this.config.cdnCustomDomain : `${this.config.bucketName}.${this.config.region}.cdn.${this.config.endpoint}`}/`);

constructor (
@Inject(DigitalOceanConfig.KEY)
private readonly config: ConfigType<typeof DigitalOceanConfig>,
) {
this.s3Client = new S3({
forcePathStyle: false,
endpoint: config.endpoint,
region: config.region,
endpoint: `${config.protocol}://${config.region}.${config.endpoint}`,
region: `us-east-1`,
credentials: {
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
},
});
}

generateFileUrl (hash: string): string {
return `https://${this.config.bucketName}.${this.config.endpoint.replace(/https?:\/\//, "")}/${hash}`;
}

async fileExists (hash: string): Promise<boolean> {
try {
await this.s3Client.send(
Expand Down Expand Up @@ -87,9 +88,4 @@ export class S3FileStorageService {
}),
);
}

generateHash (content: Buffer): string {
return createHash("sha256").update(content)
.digest("hex");
}
}
2 changes: 1 addition & 1 deletion src/social-card/social-card.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class SocialCardService {

const { id, name, avatarUrl, repos, langs, langTotal } = await this.getUserData(username);
const hash = `users/${String(id)}.png`;
const fileUrl = this.s3FileStorageService.generateFileUrl(hash);
const fileUrl = `${this.s3FileStorageService.getCdnEndpoint()}${hash}`;
const hasFile = await this.s3FileStorageService.fileExists(hash);
const today = (new Date);
const today3daysAgo = new Date((new Date).setDate(today.getDate() - 3));
Expand Down

0 comments on commit 077e473

Please sign in to comment.