summary refs log tree commit diff
path: root/src/cdn/util/FileStorage.ts
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-12-17 11:00:51 +0100
committerRory& <root@rory.gay>2025-12-17 11:01:27 +0100
commit491c0de845a886954262e3ddb24959ba37a014b6 (patch)
treec0251f3779a5cd2842320e1278232b48fbea0390 /src/cdn/util/FileStorage.ts
parentPre-commit: use spaces for formatting, regenerate schemas if changed (diff)
downloadserver-ts-491c0de845a886954262e3ddb24959ba37a014b6.tar.xz
lintstagedrc: regenerate schemas/openapi if schemas changed
Diffstat (limited to 'src/cdn/util/FileStorage.ts')
-rw-r--r--src/cdn/util/FileStorage.ts74
1 files changed, 37 insertions, 37 deletions
diff --git a/src/cdn/util/FileStorage.ts b/src/cdn/util/FileStorage.ts

index 94f7ab14..9ebe9f72 100644 --- a/src/cdn/util/FileStorage.ts +++ b/src/cdn/util/FileStorage.ts
@@ -25,52 +25,52 @@ import ExifTransformer from "exif-be-gone"; // TODO: split stored files into separate folders named after cloned route function getPath(path: string) { - // STORAGE_LOCATION has a default value in start.ts - const root = process.env.STORAGE_LOCATION || "../"; - const filename = join(root, path); + // STORAGE_LOCATION has a default value in start.ts + const root = process.env.STORAGE_LOCATION || "../"; + const filename = join(root, path); - if (path.indexOf("\0") !== -1 || !filename.startsWith(root)) throw new Error("invalid path"); - return filename; + 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(path); - } catch (error) { - try { - const files = fs.readdirSync(path); - if (!files.length) return null; - return fs.readFileSync(join(path, files[0])); - } catch (error) { - return null; - } - } - } + async get(path: string): Promise<Buffer | null> { + path = getPath(path); + try { + return fs.readFileSync(path); + } catch (error) { + try { + const files = fs.readdirSync(path); + if (!files.length) return null; + return fs.readFileSync(join(path, files[0])); + } catch (error) { + return null; + } + } + } - async clone(path: string, newPath: string) { - path = getPath(path); - newPath = getPath(newPath); + async clone(path: string, newPath: string) { + path = getPath(path); + newPath = getPath(newPath); - if (!fs.existsSync(dirname(newPath))) fs.mkdirSync(dirname(newPath), { recursive: true }); + if (!fs.existsSync(dirname(newPath))) fs.mkdirSync(dirname(newPath), { recursive: true }); - // use reflink if possible, in order to not duplicate files at the block layer... - fs.copyFileSync(path, newPath, fs.constants.COPYFILE_FICLONE); - } + // use reflink if possible, in order to not duplicate files at the block layer... + fs.copyFileSync(path, newPath, fs.constants.COPYFILE_FICLONE); + } - async set(path: string, value: Buffer) { - path = getPath(path); - if (!fs.existsSync(dirname(path))) fs.mkdirSync(dirname(path), { recursive: true }); + async set(path: string, value: Buffer) { + path = getPath(path); + if (!fs.existsSync(dirname(path))) fs.mkdirSync(dirname(path), { recursive: true }); - const ret = Readable.from(value); - const cleaned_file = fs.createWriteStream(path); + const ret = Readable.from(value); + const cleaned_file = fs.createWriteStream(path); - ret.pipe(new ExifTransformer()).pipe(cleaned_file); - } + ret.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(path: string) { + //TODO we should delete the parent directory if empty + fs.unlinkSync(getPath(path)); + } }