summary refs log tree commit diff
path: root/src/util/FileStorage.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-28 21:19:19 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-28 21:19:19 +0200
commit614d8f14fa2bac5c4f4357d51b4df527bea93c19 (patch)
tree31c97a8c2bc2c73a9cf35a2fe75a15535bde86e7 /src/util/FileStorage.ts
parentUpdate attachments.ts (diff)
downloadserver-614d8f14fa2bac5c4f4357d51b4df527bea93c19.tar.xz
:sparkles: attachments
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); } }