This repository has been archived on 2026-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
ServerSpacebarOld/src/util/FileStorage.ts
2021-08-07 13:15:26 +02:00

29 lines
721 B
TypeScript

import { Storage } from "./Storage";
import fs from "fs";
import { join, relative } from "path";
import "missing-native-js-functions";
function getPath(path: string) {
// STORAGE_LOCATION has a default value in start.ts
return join(process.env.STORAGE_LOCATION || "../", relative("/", path));
}
export class FileStorage implements Storage {
async get(path: string): Promise<Buffer | null> {
try {
return fs.readFileSync(getPath(path));
} catch (error) {
return null;
}
}
async set(path: string, value: any) {
return fs.writeFileSync(getPath(path), value, { encoding: "binary" });
}
async delete(path: string) {
path = join(process.env.STORAGE_LOCATION || "", path);
fs.unlinkSync(path);
}
}