From 614d8f14fa2bac5c4f4357d51b4df527bea93c19 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 28 May 2021 21:19:19 +0200 Subject: :sparkles: attachments --- src/util/FileStorage.ts | 23 ++++++++++++++++++++--- src/util/Storage.ts | 5 +++-- 2 files changed, 23 insertions(+), 5 deletions(-) (limited to 'src/util') 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 { + 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); } } diff --git a/src/util/Storage.ts b/src/util/Storage.ts index 391afa83..f8b09e71 100644 --- a/src/util/Storage.ts +++ b/src/util/Storage.ts @@ -1,8 +1,9 @@ import { FileStorage } from "./FileStorage"; export interface Storage { - set(path: string, data: any): Promise; - get(path: string): Promise; + set(path: string, data: Buffer): Promise; + get(path: string): Promise; + delete(path: string): Promise; } var storage: Storage; -- cgit 1.5.1