Skip to content

Commit

Permalink
fix: #434 文章没有图片时候图床错误文章发布失败
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 25, 2023
1 parent e673f58 commit 53b5016
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 76 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- 修复普通挂件版使用方式 WordPress 和博客园发布文章报错问题
- 修复图片有备注时无法上传问题,现在支持显示备注为图片的 alt
- 修复 PicGo 设置中的时间戳重命名关闭后会自动打开的问题
- #434 文章没有图片时候图床错误文章发布失败

### 新特性

Expand Down
49 changes: 40 additions & 9 deletions components/publish/tab/main/CommonBlogMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ import { PageType } from "~/utils/platform/metaweblog/IMetaweblogCfg"
import { Post } from "~/utils/models/post"
import { LogFactory } from "~/utils/logUtil"
import { SiYuanApi } from "~/utils/platform/siyuan/siYuanApi"
import { getPageId } from "~/utils/platform/siyuan/siyuanUtil"
import { getPageId, isInSiyuanWidget } from "~/utils/platform/siyuan/siyuanUtil"
import { getConf, getJSONConf, setConf } from "~/utils/configUtil"
import { cutWords, isEmptyObject, isEmptyString, jiebaToHotWords, pinyinSlugify, zhSlugify } from "~/utils/util"
import { calcLastSeconds, formatNumToZhDate } from "~/utils/dateUtil"
Expand Down Expand Up @@ -719,6 +719,11 @@ const oneclickAttr = async (hideTip) => {
}
const checkLimit = (lastmodKey) => {
// 限制开关
if (!props.limitRate) {
return false
}
const flag = false
const lastmodDate = getConf(lastmodKey)
Expand All @@ -734,7 +739,9 @@ const checkLimit = (lastmodKey) => {
ElMessage.error(
"由于【" +
props.apiType +
"】平台的限制,每6分钟之内只能发布或者更新一次文章,距离上一次发布时间为:" +
"】平台的限制,每" +
props.limitSeconds +
"秒之内只能发布或者更新一次文章,距离上一次发布时间为:" +
s +
"秒,仍需等待:" +
(props.limitSeconds - s) +
Expand Down Expand Up @@ -822,14 +829,38 @@ const doPublish = async () => {
md = imageParser.removeImages(md)
} else {
// 处理图床
if (picgoPostMethods.getPicgoPostData().picgoEnabled) {
if (picgoPostData.picgoEnabled) {
ElMessage.info(t("github.post.picgo.start.upload"))
const picgoPostResult = await picgoPostApi.uploadPostImagesToBed(siyuanData.pageId, siyuanData.meta, md)
if (picgoPostResult.flag) {
md = picgoPostResult.mdContent
} else {
ElMessage.warning(t("github.post.picgo.picbed.error"))
const picgoPostResult = await picgoPostApi.checkPostImages(siyuanData.meta, md)
// 没有图片直接跳过
if (picgoPostResult.localImages.length > 0) {
if (picgoPostResult.unuploadImages.length > 0) {
// 思源主窗口,但是有图片没上传完毕
if (isInSiyuanWidget()) {
throw new Error(
"检测到有未上传的图片,由于Electron技术限制,挂件通用版不支持上传,仅提供链接替换,请先上传完毕再使用发布。您也可以取消使用图床,或者使用新窗口方式发布。"
)
}
md = await picgoPostApi.uploadPostImagesToBed(
siyuanData.pageId,
siyuanData.meta,
md,
picgoPostResult.localImages,
picgoPostResult.unuploadImages,
false
)
} else {
// 图片都上传过了,直接替换
md = await picgoPostApi.uploadPostImagesToBed(
siyuanData.pageId,
siyuanData.meta,
md,
picgoPostResult.localImages,
picgoPostResult.unuploadImages,
true
)
}
}
}
}
Expand Down
48 changes: 38 additions & 10 deletions components/publish/tab/main/MetaweblogMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ import { API } from "~/utils/api"
import { Post } from "~/utils/models/post"
import { API_TYPE_CONSTANTS } from "~/utils/constants/apiTypeConstants"
import { LogFactory } from "~/utils/logUtil"
import { getPageId } from "~/utils/platform/siyuan/siyuanUtil"
import { getPageId, isInSiyuanWidget } from "~/utils/platform/siyuan/siyuanUtil";
import { getJSONConf } from "~/utils/configUtil"
import { cutWords, isEmptyObject, isEmptyString, jiebaToHotWords, pinyinSlugify, zhSlugify } from "~/utils/util"
import { SiYuanApi } from "~/utils/platform/siyuan/siYuanApi"
Expand Down Expand Up @@ -680,21 +680,45 @@ const doPublish = async () => {
const data = await siyuanApi.exportMdContent(siyuanData.pageId)
let md = data.content
// 引用链接替换
md = await linkParser.convertSiyuanLinkToPlatformLink(md, api)
// 处理图床
if (picgoPostMethods.getPicgoPostData().picgoEnabled) {
ElMessage.info(t("github.post.picgo.start.upload"))
const picgoPostResult = await picgoPostApi.uploadPostImagesToBed(siyuanData.pageId, siyuanData.meta, md)
if (picgoPostResult.flag) {
md = picgoPostResult.mdContent
} else {
ElMessage.warning(t("github.post.picgo.picbed.error"))
const picgoPostResult = await picgoPostApi.checkPostImages(siyuanData.meta, md)
// 没有图片直接跳过
if (picgoPostResult.localImages.length > 0) {
if (picgoPostResult.unuploadImages.length > 0) {
// 思源主窗口,但是有图片没上传完毕
if (isInSiyuanWidget()) {
throw new Error(
"检测到有未上传的图片,由于Electron技术限制,挂件通用版不支持上传,仅提供链接替换,请先上传完毕再使用发布。您也可以取消使用图床,或者使用新窗口方式发布。"
)
}
md = await picgoPostApi.uploadPostImagesToBed(
siyuanData.pageId,
siyuanData.meta,
md,
picgoPostResult.localImages,
picgoPostResult.unuploadImages,
false
)
} else {
// 图片都上传过了,直接替换
md = await picgoPostApi.uploadPostImagesToBed(
siyuanData.pageId,
siyuanData.meta,
md,
picgoPostResult.localImages,
picgoPostResult.unuploadImages,
true
)
}
}
}
// 引用链接替换
md = await linkParser.convertSiyuanLinkToPlatformLink(md, api)
if (PageType.Html === metaweblogCfg.pageType) {
const html = mdToHtml(md)
content = removeWidgetTag(html)
Expand Down Expand Up @@ -756,6 +780,10 @@ const doPublish = async () => {
} catch (e) {
isPublishLoading.value = false
logger.error("发布异常", e)
ElMessage.error({
dangerouslyUseHTMLString: true,
message: t("main.opt.failure") + "=>" + e,
})
}
isPublishLoading.value = false
Expand Down
39 changes: 30 additions & 9 deletions composables/publish/publishActionCom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { PicgoPostApi } from "~/utils/platform/picgo/picgoPostApi"
import { API } from "~/utils/api"
import { Post } from "~/utils/models/post"
import { LinkParser } from "~/utils/parser/LinkParser"
import { isInSiyuanWidget } from "~/utils/platform/siyuan/siyuanUtil";

/**
* 通用的发布操作组件
Expand Down Expand Up @@ -139,16 +140,36 @@ export const usePublish = (props, deps?: any) => {
if (picgoPostMethods.getPicgoPostData().picgoEnabled) {
ElMessage.info(t("github.post.picgo.start.upload"))
const siyuanPage = siyuanPageMethods.getSiyuanPageData().dataObj
const picgoPostResult = await picgoPostApi.uploadPostImagesToBed(
siyuanPage.pageId,
siyuanPage.meta,
mdFullContent
)
const picgoPostResult = await picgoPostApi.checkPostImages(siyuanPage.meta, md)
// 没有图片直接跳过
if (picgoPostResult.localImages.length > 0) {
if (picgoPostResult.unuploadImages.length > 0) {
// 思源主窗口,但是有图片没上传完毕
if (isInSiyuanWidget()) {
throw new Error(
"检测到有未上传的图片,由于Electron技术限制,挂件通用版不支持上传,仅提供链接替换,请先上传完毕再使用发布。您也可以取消使用图床,或者使用新窗口方式发布。"
)
}

if (picgoPostResult.flag) {
md = picgoPostResult.mdContent
} else {
ElMessage.warning(t("github.post.picgo.picbed.error"))
md = await picgoPostApi.uploadPostImagesToBed(
siyuanPage.pageId,
siyuanPage.meta,
md,
picgoPostResult.localImages,
picgoPostResult.unuploadImages,
false
)
} else {
// 图片都上传过了,直接替换
md = await picgoPostApi.uploadPostImagesToBed(
siyuanPage.pageId,
siyuanPage.meta,
md,
picgoPostResult.localImages,
picgoPostResult.unuploadImages,
true
)
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions utils/models/imageItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ export class ImageItem {
this.hash = getFileHash(this.name)

const urlAttrs = url.split(" ")
console.error(url)
console.error(urlAttrs)
if (urlAttrs.length > 1) {
this.url = urlAttrs[0]
this.alt = urlAttrs[1].replace(/"/g, "")
Expand Down
11 changes: 7 additions & 4 deletions utils/models/picgoPostResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@
* questions.
*/

import { ImageItem } from "~/utils/models/imageItem"

/**
* Picgo处理文章统一返回结果
*/
export class PicgoPostResult {
/**
* 是否成功
* 全部本地图片
*/
flag: boolean
localImages: ImageItem[]

/**
* 处理后的文章链接
* 没上传的本地图片
*/
mdContent: string
unuploadImages: ImageItem[]
}
Loading

0 comments on commit 53b5016

Please sign in to comment.