From 18f0292d1bbb777410a10ed79d1c044d9ae593e1 Mon Sep 17 00:00:00 2001 From: faper <2446866151@qq.com> Date: Sep 09 2021 03:58:51 +0000 Subject: Merge branch 'master' of ssh://git@pagure.io/Apis.git --- diff --git a/core/controller_config_storage.mjs b/core/controller_config_storage.mjs index 25cf42c..550d259 100644 --- a/core/controller_config_storage.mjs +++ b/core/controller_config_storage.mjs @@ -30,7 +30,7 @@ class ConfigFile extends Config { return this.data2obj(this.data); } get_path() { - return join("configs", `${this.name}.${this.type}cfg.json`); + return join(cwd(), "configs", `${this.name}.${this.type}cfg.json`); } init_data() { this.data = this.create_data(); diff --git a/core/controller_localfs.mjs b/core/controller_localfs.mjs new file mode 100644 index 0000000..af7c824 --- /dev/null +++ b/core/controller_localfs.mjs @@ -0,0 +1,45 @@ +/* + * controller_localfs.mjs + * + * provide some apis for frontend to read/save loacl files + */ + +import fs from "node:fs/promises"; +import {join} from "path"; +import {cwd} from "process"; + +export const get_controller = (self) => {return { + apis: { + localfs_read_file: async (_e, src) => { + try { + let raw; + if (src.startsWith("res:")) { + const path = src.slice(4); + raw = await fs.readFile(join(cwd(), path), { + encoding: "utf8", + flag: "r" + }); + } + return raw; + } catch (e) { + throw e; + } + }, + localfs_read_json: async (_e, src) => { + return JSON.parse(await self.call_api("localfs_read_file", src)); + }, + localfs_write_file: async (_e, src, raw) => { + try { + if (src.startsWith("res:")) { + const path = src.slice(4); + await fs.writeFile(join(cwd(), path), raw, { + encoding: "utf8", + flag: "r" + }); + } + } catch (e) { + throw e; + } + }, + } +}}; diff --git a/core/main.mjs b/core/main.mjs index 418be30..08ac3f7 100644 --- a/core/main.mjs +++ b/core/main.mjs @@ -8,6 +8,7 @@ const CONTROLLERS = [ "./controller_common.mjs", "./controller_win.js", "./controller_config_storage.mjs", +"./controller_localfs.mjs", ]; class Controller {