summary refs log tree commit diff
path: root/cdn/src
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-10 14:09:18 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-10 14:09:18 +0200
commit59859177150c058d789cb04eee6c953f6818ac6d (patch)
treeb07b61e6f4d86b7082d71b572bec2b0b17c9fd57 /cdn/src
parent:bug: fix cdn (diff)
downloadserver-59859177150c058d789cb04eee6c953f6818ac6d.tar.xz
:bug: fix cdn route not working without hash
Diffstat (limited to 'cdn/src')
-rw-r--r--cdn/src/routes/avatars.ts15
-rw-r--r--cdn/src/util/FileStorage.ts14
2 files changed, 26 insertions, 3 deletions
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;
+			}
 		}
 	}