summary refs log tree commit diff
path: root/src/util/FileStorage.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/FileStorage.ts')
-rw-r--r--src/util/FileStorage.ts23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/util/FileStorage.ts b/src/util/FileStorage.ts

index b4d00213..9c9911f3 100644 --- a/src/util/FileStorage.ts +++ b/src/util/FileStorage.ts
@@ -1,13 +1,30 @@ import { Storage } from "./Storage"; import fs from "fs/promises"; import { join } from "path"; +import "missing-native-js-functions"; export class FileStorage implements Storage { - async get(path: string) { - return fs.readFile(join(process.env.STORAGE_LOCATION || "", path), { encoding: "binary" }); + async get(path: string): Promise<Buffer | null> { + path = join(process.env.STORAGE_LOCATION || "", path); + try { + const file = await fs.readFile(path); + // @ts-ignore + return file; + } catch (error) { + return null; + } } async set(path: string, value: any) { - return fs.writeFile(join(process.env.STORAGE_LOCATION || "", path), value, { encoding: "binary" }); + path = join(process.env.STORAGE_LOCATION || "", path); + const dir = path.split("/").slice(0, -1).join("/"); + await fs.mkdir(dir, { recursive: true }).caught(); + + return fs.writeFile(path, value, { encoding: "binary" }); + } + + async delete(path: string) { + path = join(process.env.STORAGE_LOCATION || "", path); + await fs.unlink(path); } }