From 42197373002fe3b2415f50d495634130ce1f6137 Mon Sep 17 00:00:00 2001 From: terwer Date: Sun, 17 Nov 2024 01:24:16 +0800 Subject: [PATCH] fix: pic upload error in some cases --- package.json | 6 ++-- pnpm-lock.yaml | 51 ++++++++++++++++--------------- src/composables/usePicgoBridge.ts | 39 ++++------------------- 3 files changed, 35 insertions(+), 61 deletions(-) diff --git a/package.json b/package.json index 2de4a005..cf49061a 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "vue-i18n": "^10.0.4", "vue-router": "^4.4.5", "xmlbuilder2": "^3.1.1", - "zhi-blog-api": "^1.66.0", + "zhi-blog-api": "^1.67.0", "zhi-common": "^1.33.0", "zhi-device": "^2.11.0", "zhi-fetch-middleware": "^0.12.0", @@ -91,8 +91,8 @@ "zhi-gitlab-middleware": "^0.10.0", "zhi-lib-base": "^0.8.0", "zhi-notion-markdown": "^0.1.4", - "zhi-siyuan-api": "^2.22.0", - "zhi-siyuan-picgo": "1.11.0", + "zhi-siyuan-api": "^2.23.0", + "zhi-siyuan-picgo": "1.11.2", "zhi-xmlrpc-middleware": "^0.6.21" }, "packageManager": "pnpm@9.6.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2456dbb6..7355287f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -90,8 +90,8 @@ importers: specifier: ^3.1.1 version: 3.1.1 zhi-blog-api: - specifier: ^1.66.0 - version: 1.66.0(typescript@5.6.3) + specifier: ^1.67.0 + version: 1.67.0(typescript@5.6.3) zhi-common: specifier: ^1.33.0 version: 1.33.0(typescript@5.6.3) @@ -114,11 +114,11 @@ importers: specifier: ^0.1.4 version: 0.1.4 zhi-siyuan-api: - specifier: ^2.22.0 - version: 2.22.0(typescript@5.6.3) + specifier: ^2.23.0 + version: 2.23.0(typescript@5.6.3) zhi-siyuan-picgo: - specifier: 1.11.0 - version: 1.11.0(typescript@5.6.3) + specifier: 1.11.2 + version: 1.11.2(typescript@5.6.3) zhi-xmlrpc-middleware: specifier: ^0.6.21 version: 0.6.21(typescript@5.6.3) @@ -2660,6 +2660,7 @@ packages: eslint@8.57.0: resolution: {integrity: sha512-dZ6+mexnaTIbSBZWgou51U6OmzIhYM2VcNdtiTtI7qPNZm35Akpr0f6vtw3w1Kmn5PYo+tZVfh13WrhpS6oLqQ==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} + deprecated: This version is no longer supported. Please see https://eslint.org/version-support for other options. hasBin: true espree@9.6.1: @@ -4642,11 +4643,11 @@ packages: unist-util-visit-parents@3.1.1: resolution: {integrity: sha512-1KROIZWo6bcMrZEwiH2UrXDyalAa0uqzWCxCJj6lPOvTve2WkfgCytoDTPaMnodXh1WrXOq0haVYHj99ynJlsg==} - universal-picgo-store@1.11.0: - resolution: {integrity: sha512-qDrtPAQBjh31rVW4srS52wm9SfrhMs6bpSfVWmYAAh9TSZQ2dfXAurMkEcRBwbpKKFDgdoigGcBZxWKZynUBcQ==} + universal-picgo-store@1.11.2: + resolution: {integrity: sha512-qxs1LzeUM6O1SYbcGIAOZhTAK2/2yM1fRlPmu6yUhWHb1vRlB3Z5+wEwYAzJFVV4bV+CiS/IM6PqGCRyT2gylg==} - universal-picgo@1.11.0: - resolution: {integrity: sha512-JfdM2cmN82D9xsG0477tteRnMhbfID9Gqoz+WoW3VA9OML5d3/J4yG6XqSBUiNlIrytTU0w432s3+5DSQ8JVJQ==} + universal-picgo@1.11.2: + resolution: {integrity: sha512-BmWPzk02oyuJpwHM+pfYuCUCrffXTMZvkHjJ0IDIo+Ctf8ld3n9U5OL0z5az/CvCyOugYm7BSr8WmecjmrM3LQ==} universal-user-agent@6.0.1: resolution: {integrity: sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ==} @@ -5080,8 +5081,8 @@ packages: resolution: {integrity: sha512-b4JR1PFR10y1mKjhHY9LaGo6tmrgjit7hxVIeAmyMw3jegXR4dhYqLaQF5zMXZxY7tLpMyJeLjr1C4rLmkVe8g==} engines: {node: '>=12.20'} - zhi-blog-api@1.66.0: - resolution: {integrity: sha512-0FQmYhJUaMHlnAHXkS0r2OEy26EX4IEPxw5AKDLmI8yoafniZtJ1knpmyx+Ds9dGrvTZj1iELSRYOTkdhIe5YA==} + zhi-blog-api@1.67.0: + resolution: {integrity: sha512-AVytAFXe4bs/12Xnt0dbKvYMUMudt4JMYjaeI0RUtkKEEZRevO6BedrU9G5py2R0M9cRxBhO2TadlqIAV5qiUA==} zhi-common@1.33.0: resolution: {integrity: sha512-ggHVmZ/zkVIOLfAWv4vqJBZY9BZnlnBUhlOmtYoVHV35UdxI5zcvvakBRgDZxjyknikHOLot7MtYfccCCOdyKw==} @@ -5104,11 +5105,11 @@ packages: zhi-notion-markdown@0.1.4: resolution: {integrity: sha512-Ltoeb+NmQg/bi/d5OBI1PzlnX3hCE3x6nN94t50gegdhI53h3ZctY6dutd7GnDF7SpMNn++4m8kH625udYLrog==} - zhi-siyuan-api@2.22.0: - resolution: {integrity: sha512-a4z3+aGDfWSkiTRQW2Rvjw8q174ueE1aEWrdBZlaTiRbaKs8/jU8kdITJM1IozEcTt3Db7o+ibC7mkgARFQ5wA==} + zhi-siyuan-api@2.23.0: + resolution: {integrity: sha512-T4DSd5wR3lsBCdCrKefuoleMfeOjF5yOJOXMCeXlw7EQduz4n4IkP8dHixP/GkSSQdEJHc1Ia0jbUlu9nQka1w==} - zhi-siyuan-picgo@1.11.0: - resolution: {integrity: sha512-h9+nkhI66h0SmqMMGlyzMjXuJp+MTgA3t8JKCmfGcgk4r1mkM6HgU4sDpeRZ7N2GwhVZOSVAl0d50UL0eidA9w==} + zhi-siyuan-picgo@1.11.2: + resolution: {integrity: sha512-pTLJWXz+V2e7HTZuXnJyrD1YTH/e+EXzC0A/sCDaIWOuQ2kVlROYyTaC8U1V33ySX1HCOYwyweoLyI4I/OXeZw==} zhi-xmlrpc-middleware@0.6.21: resolution: {integrity: sha512-PIAYQUaRmu+qUK4ZnqefyhPz3CkEkELmCWoyHIorfLrcZ6nyRIFFvyraCslQQtaMlk9asKr9QDg9WaNicU20Bg==} @@ -10439,7 +10440,7 @@ snapshots: '@types/unist': 2.0.11 unist-util-is: 4.1.0 - universal-picgo-store@1.11.0: + universal-picgo-store@1.11.2: dependencies: '@commonify/lowdb': 3.0.0 comment-json: 4.2.5 @@ -10447,7 +10448,7 @@ snapshots: ts-localstorage: 3.1.0 zhi-lib-base: 0.8.0 - universal-picgo@1.11.0: + universal-picgo@1.11.2: dependencies: '@aws-sdk/client-s3': 3.687.0 '@aws-sdk/s3-request-presigner': 3.687.0 @@ -10464,7 +10465,7 @@ snapshots: mime: 4.0.4 mime-types: 2.1.35 queue: 7.0.0 - universal-picgo-store: 1.11.0 + universal-picgo-store: 1.11.2 zhi-lib-base: 0.8.0 transitivePeerDependencies: - aws-crt @@ -10923,7 +10924,7 @@ snapshots: yocto-queue@1.1.1: {} - zhi-blog-api@1.66.0(typescript@5.6.3): + zhi-blog-api@1.67.0(typescript@5.6.3): dependencies: zhi-common: 1.33.0(typescript@5.6.3) zhi-lib-base: 0.8.0 @@ -10976,23 +10977,23 @@ snapshots: - encoding - supports-color - zhi-siyuan-api@2.22.0(typescript@5.6.3): + zhi-siyuan-api@2.23.0(typescript@5.6.3): dependencies: - zhi-blog-api: 1.66.0(typescript@5.6.3) + zhi-blog-api: 1.67.0(typescript@5.6.3) zhi-common: 1.33.0(typescript@5.6.3) zhi-lib-base: 0.8.0 transitivePeerDependencies: - typescript - zhi-siyuan-picgo@1.11.0(typescript@5.6.3): + zhi-siyuan-picgo@1.11.2(typescript@5.6.3): dependencies: js-md5: 0.8.3 - universal-picgo: 1.11.0 + universal-picgo: 1.11.2 uuid: 9.0.1 zhi-common: 1.33.0(typescript@5.6.3) zhi-device: 2.11.0 zhi-lib-base: 0.8.0 - zhi-siyuan-api: 2.22.0(typescript@5.6.3) + zhi-siyuan-api: 2.23.0(typescript@5.6.3) transitivePeerDependencies: - aws-crt - debug diff --git a/src/composables/usePicgoBridge.ts b/src/composables/usePicgoBridge.ts index df9a1a24..c05e8445 100644 --- a/src/composables/usePicgoBridge.ts +++ b/src/composables/usePicgoBridge.ts @@ -23,42 +23,15 @@ * questions. */ -import { ImageItem, ImageParser, ParsedImage, SiyuanPicgoPostApi } from "zhi-siyuan-picgo" +import { ImageItem, ImageParser, ParsedImage, SiyuanPicGo } from "zhi-siyuan-picgo" import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts" import { ElMessage } from "element-plus" import { createAppLogger } from "~/src/utils/appLogger.ts" import { StrUtil } from "zhi-common" import { isDev } from "~/src/utils/constants.ts" -import { SiyuanKernelApi } from "zhi-siyuan-api" import { BlogConfig, PicbedServiceTypeEnum } from "zhi-blog-api" import { isFileExists } from "~/src/utils/siyuanUtils.ts" -let needUpdate = false -const checkConfig = async (siyuanApi: SiyuanKernelApi, picgo: SiyuanPicgoPostApi) => { - return new Promise((resolve, _reject) => { - if (picgo.cfgUpdating) { - needUpdate = true - siyuanApi.pushMsg({ - msg: "检测到旧配置,正在迁移配置,请勿进行任何操作...", - timeout: 1000, - }) - console.warn("检测到旧配置,正在迁移配置,请勿进行任何操作...") - setTimeout(checkConfig, 1000) - } else { - if (needUpdate) { - siyuanApi.pushMsg({ - msg: "PicGo 图床历史配置迁移完成", - timeout: 7000, - }) - console.log("PicGo 图床历史配置迁移完成") - needUpdate = false - } - console.log("picgo instance is ready") - resolve(picgo) - } - }) -} - /** * Picgo 桥接 API,用于上传并替换图片链接 * @@ -68,7 +41,6 @@ const checkConfig = async (siyuanApi: SiyuanKernelApi, picgo: SiyuanPicgoPostApi const usePicgoBridge = () => { const logger = createAppLogger("use-picgo-bridge") const { siyuanConfig, kernelApi, blogApi } = useSiyuanApi() - const picgoPostApi = new SiyuanPicgoPostApi(siyuanConfig as any, isDev) /** * 处理图片上传与替换 @@ -77,9 +49,6 @@ const usePicgoBridge = () => { * @param mdContent - 正文,如果为空,会用 pageId 去获取最新 */ const handlePicgo = async (pageId: string, mdContent?: string) => { - // 检测配置迁移 - await checkConfig(kernelApi, picgoPostApi) - let md: string = mdContent const picgoErrMsg = "文档可能已经成功发布,但是图片上传失败或者当前场景不支持图片上传,详细信息=>" @@ -96,6 +65,8 @@ const usePicgoBridge = () => { md = siyuanPost.markdown } + // 通用方法获取,保证单例 + const picgoPostApi = await SiyuanPicGo.getInstance(siyuanConfig, isDev) const picgoPostResult = await picgoPostApi.uploadPostImagesToBed(siyuanData.pageId, siyuanData.meta, md) // 有图片才上传 if (picgoPostResult.hasImages) { @@ -138,6 +109,8 @@ const usePicgoBridge = () => { const attrs = await kernelApi.getBlockAttrs(pageId) const baseUrl = siyuanConfig.apiUrl ?? "" + // 通用方法获取,保证单例 + const picgoPostApi = await SiyuanPicGo.getInstance(siyuanConfig, isDev) const imageItemArray = await picgoPostApi.doConvertImagesToImagesItemArray(attrs, retImgs, baseUrl) logger.debug("imageItemArray=>", imageItemArray) return imageItemArray @@ -187,7 +160,7 @@ const usePicgoBridge = () => { handlePicgo, getImageItemsFromMd, getPicbedServiceType, - checkPicgoInstalled + checkPicgoInstalled, } }