summary refs log tree commit diff
path: root/src/cdn/util/FileStorage.ts
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-27 05:18:48 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2022-09-04 10:48:54 +0200
commit7d936efb2f37db42c3e57ee9a915ed4d5ab8e25f (patch)
tree69d68d640c4ac2946b118591f02de9d3c24fb29b /src/cdn/util/FileStorage.ts
parentAdd sqlite migration for plugin settings (diff)
downloadserver-7d936efb2f37db42c3e57ee9a915ed4d5ab8e25f.tar.xz
Cleanup, reformat, fix some todos, git hook
fixup! Cleanup, reformat, fix some todos, git hook
Diffstat (limited to 'src/cdn/util/FileStorage.ts')
-rw-r--r--src/cdn/util/FileStorage.ts35
1 files changed, 19 insertions, 16 deletions
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)); + } } }