summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-12-25 03:57:01 +0100
committerRory& <root@rory.gay>2025-12-25 03:57:01 +0100
commitb0105d7d807b660be7e29643304eccf00509f35d (patch)
tree6a3379105592a96f7d17dba5b393c9a72db9fc2e /src
parentDrop sqlite3 by default (diff)
downloadserver-ts-b0105d7d807b660be7e29643304eccf00509f35d.tar.xz
No s3 support by default
Diffstat (limited to 'src')
-rw-r--r--src/cdn/util/S3Storage.ts17
-rw-r--r--src/cdn/util/Storage.ts15
2 files changed, 24 insertions, 8 deletions
diff --git a/src/cdn/util/S3Storage.ts b/src/cdn/util/S3Storage.ts

index f3ac0b44..f657fd29 100644 --- a/src/cdn/util/S3Storage.ts +++ b/src/cdn/util/S3Storage.ts
@@ -16,7 +16,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { S3 } from "@aws-sdk/client-s3"; import { Readable } from "stream"; import { Storage } from "./Storage"; @@ -29,11 +28,15 @@ const readableToBuffer = (readable: Readable): Promise<Buffer> => }); export class S3Storage implements Storage { + private client: unknown; public constructor( - private client: S3, + private region: string, private bucket: string, private basePath?: string, - ) {} + ) { + const { S3 } = require("@aws-sdk/client-s3"); + this.client = new S3({ region }); + } /** * Always return a string, to ensure consistency. @@ -43,6 +46,8 @@ export class S3Storage implements Storage { } async set(path: string, data: Buffer): Promise<void> { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error await this.client.putObject({ Bucket: this.bucket, Key: `${this.bucketBasePath}${path}`, @@ -52,6 +57,8 @@ export class S3Storage implements Storage { async clone(path: string, newPath: string): Promise<void> { // TODO: does this even work? + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error await this.client.copyObject({ Bucket: this.bucket, CopySource: `/${this.bucket}/${this.bucketBasePath}${path}`, @@ -61,6 +68,8 @@ export class S3Storage implements Storage { async get(path: string): Promise<Buffer | null> { try { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error const s3Object = await this.client.getObject({ Bucket: this.bucket, Key: `${this.bucketBasePath ?? ""}${path}`, @@ -79,6 +88,8 @@ export class S3Storage implements Storage { } async delete(path: string): Promise<void> { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error await this.client.deleteObject({ Bucket: this.bucket, Key: `${this.bucketBasePath}${path}`, diff --git a/src/cdn/util/Storage.ts b/src/cdn/util/Storage.ts
index 6ec1b796..1184bde8 100644 --- a/src/cdn/util/Storage.ts +++ b/src/cdn/util/Storage.ts
@@ -19,8 +19,7 @@ import { FileStorage } from "./FileStorage"; import path from "path"; import fs from "fs"; -import { S3 } from "@aws-sdk/client-s3"; -import { S3Storage } from "./S3Storage"; +import { red } from "picocolors"; process.cwd(); export interface Storage { @@ -46,6 +45,13 @@ if (process.env.STORAGE_PROVIDER === "file" || !process.env.STORAGE_PROVIDER) { storage = new FileStorage(); } else if (process.env.STORAGE_PROVIDER === "s3") { + try { + require("@aws-sdk/client-s3"); + } catch (e) { + console.error(red(`[CDN] AWS S3 SDK not installed. Please run 'npm install --no-save @aws-sdk/client-s3' to use the S3 storage provider.`)); + process.exit(1); + } + const region = process.env.STORAGE_REGION, bucket = process.env.STORAGE_BUCKET; @@ -67,9 +73,8 @@ if (process.env.STORAGE_PROVIDER === "file" || !process.env.STORAGE_PROVIDER) { location = undefined; } - const client = new S3({ region }); - - storage = new S3Storage(client, bucket, location); + const { S3Storage } = require("S3Storage"); + storage = new S3Storage(region, bucket, location); } export { storage };