diff --git a/cdn/src/Server.ts b/cdn/src/Server.ts
index 590eda6f..cac34a80 100644
--- a/cdn/src/Server.ts
+++ b/cdn/src/Server.ts
@@ -1,5 +1,5 @@
import { Server, ServerOptions } from "lambert-server";
-import { Config, initDatabase } from "@fosscord/util";
+import { Config, initDatabase, registerRoutes } from "@fosscord/util";
import path from "path";
import avatarsRoute from "./routes/avatars";
import bodyParser from "body-parser";
@@ -23,13 +23,19 @@ export class CDNServer extends Server {
"Content-security-policy",
"default-src * data: blob: filesystem: about: ws: wss: 'unsafe-inline' 'unsafe-eval'; script-src * data: blob: 'unsafe-inline' 'unsafe-eval'; connect-src * data: blob: 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src * data: blob: ; style-src * data: blob: 'unsafe-inline'; font-src * data: blob: 'unsafe-inline';"
);
- res.set("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers") || "*");
- res.set("Access-Control-Allow-Methods", req.header("Access-Control-Request-Methods") || "*");
+ res.set(
+ "Access-Control-Allow-Headers",
+ req.header("Access-Control-Request-Headers") || "*"
+ );
+ res.set(
+ "Access-Control-Allow-Methods",
+ req.header("Access-Control-Request-Methods") || "*"
+ );
next();
});
this.app.use(bodyParser.json({ inflate: true, limit: "10mb" }));
- await this.registerRoutes(path.join(__dirname, "routes/"));
+ await registerRoutes(this, path.join(__dirname, "routes/"));
this.app.use("/icons/", avatarsRoute);
this.log("verbose", "[Server] Route /icons registered");
diff --git a/cdn/src/routes/avatars.ts b/cdn/src/routes/avatars.ts
index 3d5e7d77..2a4a0ffe 100644
--- a/cdn/src/routes/avatars.ts
+++ b/cdn/src/routes/avatars.ts
@@ -58,6 +58,21 @@ router.post(
}
);
+router.get("/:user_id", async (req: Request, res: Response) => {
+ var { user_id } = req.params;
+ user_id = user_id.split(".")[0]; // remove .file extension
+ const path = `avatars/${user_id}`;
+
+ const file = await storage.get(path);
+ if (!file) throw new HTTPError("not found", 404);
+ const type = await FileType.fromBuffer(file);
+
+ res.set("Content-Type", type?.mime);
+ res.set("Cache-Control", "public, max-age=31536000");
+
+ return res.send(file);
+});
+
router.get("/:user_id/:hash", async (req: Request, res: Response) => {
var { user_id, hash } = req.params;
hash = hash.split(".")[0]; // remove .file extension
diff --git a/cdn/src/util/FileStorage.ts b/cdn/src/util/FileStorage.ts
index e0b24a84..84ecf556 100644
--- a/cdn/src/util/FileStorage.ts
+++ b/cdn/src/util/FileStorage.ts
@@ -13,16 +13,24 @@ function getPath(path: string) {
const root = process.env.STORAGE_LOCATION || "../";
var filename = join(root, path);
- if (path.indexOf("\0") !== -1 || !filename.startsWith(root)) throw new Error("invalid path");
+ if (path.indexOf("\0") !== -1 || !filename.startsWith(root))
+ throw new Error("invalid path");
return filename;
}
export class FileStorage implements Storage {
async get(path: string): Promise<Buffer | null> {
+ path = getPath(path);
try {
- return fs.readFileSync(getPath(path));
+ return fs.readFileSync(path);
} catch (error) {
- return null;
+ try {
+ const files = fs.readdirSync(path);
+ if (!files.length) return null;
+ return fs.readFileSync(join(path, files[0]));
+ } catch (error) {
+ return null;
+ }
}
}
diff --git a/cdn/src/util/S3Storage.ts b/cdn/src/util/S3Storage.ts
new file mode 100644
index 00000000..df5bc19c
--- /dev/null
+++ b/cdn/src/util/S3Storage.ts
@@ -0,0 +1,60 @@
+import { S3 } from "@aws-sdk/client-s3";
+import { Readable } from "stream";
+import { Storage } from "./Storage";
+
+const readableToBuffer = (readable: Readable): Promise<Buffer> =>
+ new Promise((resolve, reject) => {
+ const chunks: Buffer[] = [];
+ readable.on('data', chunk => chunks.push(chunk));
+ readable.on('error', reject);
+ readable.on('end', () => resolve(Buffer.concat(chunks)));
+ });
+
+export class S3Storage implements Storage {
+ public constructor(
+ private client: S3,
+ private bucket: string,
+ private basePath?: string,
+ ) {}
+
+ /**
+ * Always return a string, to ensure consistency.
+ */
+ get bucketBasePath() {
+ return this.basePath ?? '';
+ }
+
+ async set(path: string, data: Buffer): Promise<void> {
+ await this.client.putObject({
+ Bucket: this.bucket,
+ Key: `${this.bucketBasePath}${path}`,
+ Body: data
+ });
+ }
+
+ async get(path: string): Promise<Buffer | null> {
+ try {
+ const s3Object = await this.client.getObject({
+ Bucket: this.bucket,
+ Key: `${this.bucketBasePath ?? ''}${path}`
+ });
+
+ if (!s3Object.Body) return null;
+
+ const body = s3Object.Body;
+
+ return await readableToBuffer(<Readable> body);
+ } catch(err) {
+ console.error(`[CDN] Unable to get S3 object at path ${path}.`);
+ console.error(err);
+ return null;
+ }
+ }
+
+ async delete(path: string): Promise<void> {
+ await this.client.deleteObject({
+ Bucket: this.bucket,
+ Key: `${this.bucketBasePath}${path}`
+ });
+ }
+}
diff --git a/cdn/src/util/Storage.ts b/cdn/src/util/Storage.ts
index 91f841a6..3332f21c 100644
--- a/cdn/src/util/Storage.ts
+++ b/cdn/src/util/Storage.ts
@@ -2,6 +2,8 @@ import { FileStorage } from "./FileStorage";
import path from "path";
import fse from "fs-extra";
import { bgCyan, black } from "nanocolors";
+import { S3 } from '@aws-sdk/client-s3';
+import { S3Storage } from "./S3Storage";
process.cwd();
export interface Storage {
@@ -10,10 +12,10 @@ export interface Storage {
delete(path: string): Promise<void>;
}
-var storage: Storage;
+let storage: Storage;
if (process.env.STORAGE_PROVIDER === "file" || !process.env.STORAGE_PROVIDER) {
- var location = process.env.STORAGE_LOCATION;
+ let location = process.env.STORAGE_LOCATION;
if (location) {
location = path.resolve(location);
} else {
@@ -24,6 +26,32 @@ if (process.env.STORAGE_PROVIDER === "file" || !process.env.STORAGE_PROVIDER) {
process.env.STORAGE_LOCATION = location;
storage = new FileStorage();
+} else if (process.env.STORAGE_PROVIDER === "s3") {
+ const
+ region = process.env.STORAGE_REGION,
+ bucket = process.env.STORAGE_BUCKET;
+
+ if (!region) {
+ console.error(`[CDN] You must provide a region when using the S3 storage provider.`);
+ process.exit(1);
+ }
+
+ if (!bucket) {
+ console.error(`[CDN] You must provide a bucket when using the S3 storage provider.`);
+ process.exit(1);
+ }
+
+ // in the S3 provider, this should be the root path in the bucket
+ let location = process.env.STORAGE_LOCATION;
+
+ if (!location) {
+ console.warn(`[CDN] STORAGE_LOCATION unconfigured for S3 provider, defaulting to the bucket root...`);
+ location = undefined;
+ }
+
+ const client = new S3({ region });
+
+ storage = new S3Storage(client, bucket, location);
}
export { storage };
|