diff --git a/libs/Universal-PicGo-Core/src/api/SiyuanPicGoUploadApi.ts b/libs/Universal-PicGo-Core/src/api/SiyuanPicGoUploadApi.ts index 231879d..f897b13 100644 --- a/libs/Universal-PicGo-Core/src/api/SiyuanPicGoUploadApi.ts +++ b/libs/Universal-PicGo-Core/src/api/SiyuanPicGoUploadApi.ts @@ -45,35 +45,26 @@ export const SIYUAN_PICGO_FILE_MAP_KEY = "custom-picgo-file-map-key" class SiyuanPicGoUploadApi { private readonly picgo: IPicGo private readonly logger: ILogger + public cfgUpdating: boolean constructor(isDev?: boolean) { - let cfgfolder = "" - let picgo_cfg_160 = "" + // 初始化 PicGO + this.picgo = new UniversalPicGo("", "", isDev) + this.logger = this.picgo.getLogger("siyuan-picgo-upload-api") + this.cfgUpdating = false + + // ================================================================================================================= + + // 迁移旧插件配置 + let legacyCfgfolder = "" // 初始化思源 PicGO 配置 const workspaceDir = win?.siyuan?.config?.system?.workspaceDir ?? "" if (hasNodeEnv && workspaceDir !== "") { const path = win.require("path") - cfgfolder = path.join(workspaceDir, "data", "storage", "syp", "picgo") - picgo_cfg_160 = path.join(cfgfolder, "picgo.cfg.json") - } - - // 初始化 PicGO - this.picgo = new UniversalPicGo(picgo_cfg_160, "", isDev) - this.logger = this.picgo.getLogger("siyuan-picgo-upload-api") - - // 迁移旧插件 - if (hasNodeEnv && cfgfolder !== "") { + legacyCfgfolder = path.join(workspaceDir, "data", "storage", "syp", "picgo") // 如果新插件采用了不同的目录,需要迁移旧插件 node_modules 文件夹 - if (cfgfolder !== this.picgo.pluginBaseDir) { - const path = win.require("path") - - const plugin_dir_070 = path.join(cfgfolder, "node_modules") - const pkg_070 = path.join(cfgfolder, "package.json") - const plugin_dir_160 = path.join(this.picgo.pluginBaseDir, "node_modules") - const pkg_160 = path.join(this.picgo.pluginBaseDir, "package.json") - - this.moveFile(plugin_dir_070, plugin_dir_160) - this.moveFile(pkg_070, pkg_160) + if (legacyCfgfolder !== this.picgo.baseDir) { + void this.moveFile(legacyCfgfolder, this.picgo.baseDir) } } @@ -101,27 +92,67 @@ class SiyuanPicGoUploadApi { // =================================================================================================================== - private moveFile(from: string, to: string) { + private async moveFile(from: string, to: string) { const fs = win.fs const path = win.require("path") - const exist = pathExistsSync(fs, path, from) + const existFrom = pathExistsSync(fs, path, from) const existTo = pathExistsSync(fs, path, to) + if (!existFrom) { + return + } + // 存在旧文件采取迁移 - if (exist) { - this.logger.info(`will move ${from} to ${to}`) - // 目的地存在复制 - if (existTo) { - fs.promises.cp(from, to).catch((e: any) => { + this.cfgUpdating = true + this.logger.info(`will move ${from} to ${to}`) + // 目的地存在复制 + if (existTo) { + this.copyFolder(from, to) + .then(() => { + this.cfgUpdating = false + }) + .catch((e: any) => { + this.cfgUpdating = false this.logger.error(`copy ${from} to ${to} failed: ${e}`) }) - } else { - // 不存在移动过去 - fs.promises.rename(from, to).catch((e: any) => { + } else { + // 不存在移动过去 + fs.promises + .rename(from, to) + .then(() => { + this.cfgUpdating = false + }) + .catch((e: any) => { + this.cfgUpdating = false this.logger.error(`move ${from} to ${to} failed: ${e}`) }) + } + } + + private async copyFolder(from: string, to: string) { + const fs = win.fs + const path = win.require("path") + + const files = await fs.promises.readdir(from) + for (const file of files) { + if (file.startsWith(".")) { + continue + } + const sourcePath = path.join(from, file) + const destPath = path.join(to, file) + + const stats = await fs.promises.lstat(sourcePath) + if (stats.isDirectory()) { + await fs.promises.mkdir(destPath, { recursive: true }) + // 递归复制子文件夹 + await this.copyFolder(sourcePath, destPath) + } else { + await fs.promises.copyFile(sourcePath, destPath) } } + + // 删除源文件夹 + await fs.promises.rmdir(from, { recursive: true }) } } diff --git a/libs/Universal-PicGo-Core/src/core/UniversalPicGo.ts b/libs/Universal-PicGo-Core/src/core/UniversalPicGo.ts index cc715bc..9475538 100644 --- a/libs/Universal-PicGo-Core/src/core/UniversalPicGo.ts +++ b/libs/Universal-PicGo-Core/src/core/UniversalPicGo.ts @@ -17,9 +17,8 @@ import { IPicGo, IPicGoPlugin, IPicGoPluginInterface, - IPicGoRequest, IPluginLoader, - IStringKeyMap, + IStringKeyMap } from "../types" import { Lifecycle } from "./Lifecycle" import uploaders from "../plugins/uploader" @@ -91,7 +90,7 @@ class UniversalPicGo extends EventEmitter implements IPicGo { uploader: new LifecyclePlugins("uploader"), beforeTransformPlugins: new LifecyclePlugins("beforeTransformPlugins"), beforeUploadPlugins: new LifecyclePlugins("beforeUploadPlugins"), - afterUploadPlugins: new LifecyclePlugins("afterUploadPlugins"), + afterUploadPlugins: new LifecyclePlugins("afterUploadPlugins") } this.initConfigPath() // this.cmd = new Commander(this) @@ -166,7 +165,7 @@ class UniversalPicGo extends EventEmitter implements IPicGo { _.set(this._config, name, config[name]) eventBus.emit(IBusEvent.CONFIG_CHANGE, { configName: name, - value: config[name], + value: config[name] }) }) } @@ -233,8 +232,9 @@ class UniversalPicGo extends EventEmitter implements IPicGo { if (hasNodeEnv) { const os = win.require("os") const fs = win.fs + const path = win.require("path") const { homedir } = os - const dir = homedir() + "/.universal-picgo" + const dir = path.join(homedir(), ".universal-picgo") ensureFolderSync(fs, dir) return dir } else { @@ -245,6 +245,12 @@ class UniversalPicGo extends EventEmitter implements IPicGo { private initConfigPath(): void { if (this.configPath === "") { this.baseDir = this.getDefautBaseDir() + if (hasNodeEnv) { + const path = win.require("path") + this.configPath = path.join(this.baseDir, "picgo.cfg.json") + } else { + this.configPath = browserPathJoin(this.baseDir, "picgo.cfg.json") + } } else { if (hasNodeEnv) { const fs = win.fs diff --git a/packages/picgo-plugin-app/README.md b/packages/picgo-plugin-app/README.md index 655e3d7..839d095 100644 --- a/packages/picgo-plugin-app/README.md +++ b/packages/picgo-plugin-app/README.md @@ -6,4 +6,38 @@ picgo plugin app for siyuan-note ``` ├── universal-picgo +``` + +## Docs + +New store path from 1.6.0 + +``` +1.5.6 之前的配置位置 + +[工作空间]/data/storage/syp/picgo/picgo.cfg.json + [工作空间]/data/storage/syp/picgo/mac.applescript + [工作空间]/data/storage/syp/picgo/i18n-cli + [工作空间]/data/storage/syp/picgo/picgo-clipboard-images + [工作空间]/data/storage/syp/picgo/external-picgo-cfg.json + [工作空间]/data/storage/syp/picgo/picgo.log + [工作空间]/data/storage/syp/picgo/picgo.log + [工作空间]/data/storage/syp/picgo/package.json + [工作空间]/data/storage/syp/picgo/package-lock.json + [工作空间]/data/storage/syp/picgo/node_modules + + +1.6.0+ 默认插件位置 + +~/.universal-picgo/picgo.cfg.json +~/.universal-picgo/mac.applescript +~/.universal-picgo/i18n-cli +~/.universal-picgo/picgo-clipboard-images +~/.universal-picgo/external-picgo-cfg.json +~/.universal-picgo/picgo.log +~/.universal-picgo/package.json +~/.universal-picgo/package-lock.json +~/.universal-picgo/node_modules + ~/.universal-picgo/node_modules/plugin-1 + ~/.universal-picgo/node_modules/plugin-2 ``` \ No newline at end of file diff --git a/packages/picgo-plugin-app/src/components/home/BrowserIndex.vue b/packages/picgo-plugin-app/src/components/home/BrowserIndex.vue index 7632688..2a4d672 100644 --- a/packages/picgo-plugin-app/src/components/home/BrowserIndex.vue +++ b/packages/picgo-plugin-app/src/components/home/BrowserIndex.vue @@ -9,9 +9,7 @@