summary refs log tree commit diff
path: root/src/util/FileStorage.ts
blob: 119e990f00df5e299e28e090ee5abffd97443974 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { Storage } from "./Storage";
import fs from "fs";
import { join, relative } from "path";
import "missing-native-js-functions";

function getPath(path: string) {
	if (path.indexOf("\0") !== -1 || !/^[a-z0-9]+$/.test(path)) throw new Error("invalid path");
	// STORAGE_LOCATION has a default value in start.ts
	return join(process.env.STORAGE_LOCATION || "../", 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) {
		fs.unlinkSync(getPath(path));
	}
}