forked from mirror/MCSM-Daemon
Feat: 同步代码
This commit is contained in:
parent
bb28450bc2
commit
b6954dda8b
@ -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" });
|
||||
|
Loading…
Reference in New Issue
Block a user