Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pic upload error in some cases #1433

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@
"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",
"zhi-github-middleware": "^0.5.0",
"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": "[email protected]"
Expand Down
51 changes: 26 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 6 additions & 33 deletions src/composables/usePicgoBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,用于上传并替换图片链接
*
Expand All @@ -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)

/**
* 处理图片上传与替换
Expand All @@ -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 = "文档可能已经成功发布,但是图片上传失败或者当前场景不支持图片上传,详细信息=>"

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -187,7 +160,7 @@ const usePicgoBridge = () => {
handlePicgo,
getImageItemsFromMd,
getPicbedServiceType,
checkPicgoInstalled
checkPicgoInstalled,
}
}

Expand Down
Loading