diff --git a/src/config/digital-ocean.config.ts b/src/config/digital-ocean.config.ts index bd90b79..64ac827 100644 --- a/src/config/digital-ocean.config.ts +++ b/src/config/digital-ocean.config.ts @@ -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; diff --git a/src/s3-file-storage/s3-file-storage.service.ts b/src/s3-file-storage/s3-file-storage.service.ts index 458f6c4..6eb5203 100644 --- a/src/s3-file-storage/s3-file-storage.service.ts +++ b/src/s3-file-storage/s3-file-storage.service.ts @@ -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"; @@ -10,14 +9,20 @@ 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, ) { 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, @@ -25,10 +30,6 @@ export class S3FileStorageService { }); } - generateFileUrl (hash: string): string { - return `https://${this.config.bucketName}.${this.config.endpoint.replace(/https?:\/\//, "")}/${hash}`; - } - async fileExists (hash: string): Promise { try { await this.s3Client.send( @@ -87,9 +88,4 @@ export class S3FileStorageService { }), ); } - - generateHash (content: Buffer): string { - return createHash("sha256").update(content) - .digest("hex"); - } } diff --git a/src/social-card/social-card.service.ts b/src/social-card/social-card.service.ts index 52ad531..793c9f4 100644 --- a/src/social-card/social-card.service.ts +++ b/src/social-card/social-card.service.ts @@ -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));