summary refs log tree commit diff
path: root/src/cdn
diff options
context:
space:
mode:
Diffstat (limited to 'src/cdn')
-rw-r--r--src/cdn/Server.ts3
-rw-r--r--src/cdn/util/FileStorage.ts35
2 files changed, 20 insertions, 18 deletions
diff --git a/src/cdn/Server.ts b/src/cdn/Server.ts
index 8b684b53..3729daf9 100644
--- a/src/cdn/Server.ts
+++ b/src/cdn/Server.ts
@@ -2,11 +2,10 @@ import { Config, getOrInitialiseDatabase, registerRoutes } from "@fosscord/util"
 import bodyParser from "body-parser";
 import { Server, ServerOptions } from "lambert-server";
 import path from "path";
+import { PluginConfig } from "../util/plugin/PluginConfig";
 import avatarsRoute from "./routes/avatars";
 import guildProfilesRoute from "./routes/guild-profiles";
 import iconsRoute from "./routes/role-icons";
-import bodyParser from "body-parser";
-import { PluginConfig } from "util/plugin/PluginConfig";
 
 export interface CDNServerOptions extends ServerOptions {}
 
diff --git a/src/cdn/util/FileStorage.ts b/src/cdn/util/FileStorage.ts
index fea013a6..375d23e7 100644
--- a/src/cdn/util/FileStorage.ts
+++ b/src/cdn/util/FileStorage.ts
@@ -1,5 +1,5 @@
 import fs from "fs";
-import { dirname, join } from "path";
+import path, { dirname, join } from "path";
 import { Readable } from "stream";
 import { Storage } from "./Storage";
 //import ExifTransformer = require("exif-be-gone");
@@ -7,44 +7,47 @@ import ExifTransformer from "exif-be-gone";
 
 // TODO: split stored files into separate folders named after cloned route
 
-function getPath(path: string) {
+function getPath(filePath: string) {
 	// STORAGE_LOCATION has a default value in start.ts
 	const root = process.env.STORAGE_LOCATION || "../";
-	let filename = join(root, path);
+	let filename = join(root, filePath);
 
-	if (path.indexOf("\0") !== -1 || !filename.startsWith(root)) throw new Error("invalid path");
+	if (filePath.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);
+	async get(filePath: string): Promise<Buffer | null> {
+		filePath = getPath(filePath);
 		try {
-			return fs.readFileSync(path);
+			return fs.readFileSync(filePath);
 		} catch (error) {
 			try {
-				const files = fs.readdirSync(path);
+				const files = fs.readdirSync(filePath);
 				if (!files.length) return null;
-				return fs.readFileSync(join(path, files[0]));
+				return fs.readFileSync(join(filePath, files[0]));
 			} catch (error) {
 				return null;
 			}
 		}
 	}
 
-	async set(path: string, value: any) {
-		path = getPath(path);
+	async set(filePath: string, value: any) {
+		filePath = getPath(filePath);
 		//fse.ensureDirSync(dirname(path));
-		fs.mkdirSync(dirname(path), { recursive: true });
+		fs.mkdirSync(dirname(filePath), { recursive: true });
 
 		value = Readable.from(value);
-		const cleaned_file = fs.createWriteStream(path);
+		const cleaned_file = fs.createWriteStream(filePath);
 
 		return value.pipe(new ExifTransformer()).pipe(cleaned_file);
 	}
 
-	async delete(path: string) {
-		//TODO we should delete the parent directory if empty
-		fs.unlinkSync(getPath(path));
+	async delete(filePath: string) {
+		//TODO: (done?) we should delete the parent directory if empty
+		fs.unlinkSync(getPath(filePath));
+		if (fs.readdirSync(path.dirname(filePath)).length == 0) {
+			fs.rmSync(path.dirname(filePath));
+		}
 	}
 }