Feat: 同步代码

This commit is contained in:
Suwings 2022-07-16 15:08:32 +08:00
parent bb28450bc2
commit b6954dda8b

View File

@ -30,12 +30,21 @@ class StorageSubsystem {
public static readonly STIRAGE_DATA_PATH = path.normalize(path.join(process.cwd(), "data"));
public static readonly STIRAGE_INDEX_PATH = path.normalize(path.join(process.cwd(), "data", "index"));
private checkFileName(name: string) {
const blackList = ["\\", "/", ".."];
for (const ch of blackList) {
if (name.includes(ch)) return false;
}
return true;
}
/**
*
*/
public store(category: string, uuid: string, object: any) {
const dirPath = path.join(StorageSubsystem.STIRAGE_DATA_PATH, category);
if (!fs.existsSync(dirPath)) fs.mkdirsSync(dirPath);
if (!this.checkFileName(uuid)) throw new Error(`UUID ${uuid} 不符合规范`);
const filePath = path.join(dirPath, `${uuid}.json`);
const data = JSON.stringify(object, null, 4);
fs.writeFileSync(filePath, data, { encoding: "utf-8" });
@ -66,6 +75,7 @@ class StorageSubsystem {
public load(category: string, classz: any, uuid: string) {
const dirPath = path.join(StorageSubsystem.STIRAGE_DATA_PATH, category);
if (!fs.existsSync(dirPath)) fs.mkdirsSync(dirPath);
if (!this.checkFileName(uuid)) throw new Error(`UUID ${uuid} 不符合规范`);
const filePath = path.join(dirPath, `${uuid}.json`);
if (!fs.existsSync(filePath)) return null;
const data = fs.readFileSync(filePath, { encoding: "utf-8" });