Skip to content

Commit

Permalink
fix: 兼容平台图片上传-知乎平台支持图片上传
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 14, 2023
1 parent 6c01950 commit c3e1c54
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ In later releases, the release configuration will only be backward compatible to

## Recent critical updates and bug fixes

- Zhihu platform supports automatic image upload
- When the Picgo plugin is not installed, some platforms can use the built-in image upload, such as Cnblogs, Typecho, WordPress
- [General release] The Hexo platform supports custom Yaml
- Platforms that do not support image link replacement use the platform's own image upload, such as Zhihu, Notion, adapting...
- All platforms migrated to official forward proxy
- Support replacing picture bed image links with Picgo plugin
Expand Down
6 changes: 3 additions & 3 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

> **前排公告1:发布工具首个全平台使用思源笔记正向代理的版本 `1.8.0` 发布!**
>
> **前排公告2:发布工具首个完整支持图片上传[知乎、Notion适配中,语雀、Hexo需要Picgo插件支持,Metaweblog系列平台自带图片上传]的版本 `1.8.0` 发布!**
> **前排公告2:发布工具首个完整支持图片上传[知乎使用知乎平台的图片上传,Notion使用Notion的图片上传(适配中),语雀、Hexo需要Picgo插件支持,Metaweblog系列平台同时支持Picgo插件和自带图片上传(安装了Picgo插件使用Picgo插件,否则使用自带)]的版本 `1.8.0` 发布!**
我们建议您采用 `卸载->重装` 的方式进行更新。如果配置异常请先备份 `[工作空间]/data/storage/syp/sy-p-plus-cfg.json` ,然后删除他,这个配置文件会在第一次使用的时候自动初始化。

之后的版本中,发布配置将仅向后兼容到 `1.6.0+`

## 最近的关键更新与 Bug 修复

- 知乎平台支持图片自动上传
- 未安装Picgo插件时,部分平台可使用自带的图片上传,例如博客园、Typecho、WordPress
- 【常规发布】Hexo 平台支持自定义Yaml
- 不支持图片链接替换的平台使用平台自带的图片上传,例如知乎、Notion,适配中...
- 提供不支持图片链接替换的平台使用平台自带的图片上传接口
- 所有平台迁移到官方的正向代理,极大的提升性能
- 支持使用Picgo插件的情况下替换图床图片链接
- 修复授权码模式发布预览失效问题
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"devDependencies": {
"@terwer/esbuild-config-custom": "^1.2.0",
"@terwer/eslint-config-custom": "^1.3.6",
"@types/crypto-js": "^4.1.1",
"@types/node": "^18.17.5",
"@vitejs/plugin-vue": "^4.2.3",
"@vitest/coverage-v8": "^0.34.1",
Expand Down Expand Up @@ -56,6 +57,7 @@
"@vueuse/core": "^10.3.0",
"cheerio": "1.0.0-rc.12",
"cross-fetch": "^3.1.8",
"crypto-js": "^4.1.1",
"element-plus": "^2.3.9",
"js-base64": "^3.7.5",
"pinia": "^2.1.6",
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

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

20 changes: 20 additions & 0 deletions public/libs/alioss/aliyun-oss-sdk-6.16.0.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/adaptors/base/baseExtendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class BaseExtendApi extends WebApi implements IBlogApi, IWebApi {
base64Info = toBase64Info(url, base64String)
}

this.logger.info("readFileToBase64 proxyFetch base64Info =>", { base64Info })
this.logger.debug("readFileToBase64 proxyFetch base64Info =>", { base64Info })
return base64Info
}
}
Expand Down
83 changes: 81 additions & 2 deletions src/adaptors/web/zhihu/zhihuWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import { BaseWebApi } from "~/src/adaptors/web/base/baseWebApi.ts"
import { Post, UserBlog } from "zhi-blog-api"
import * as cheerio from "cheerio"
import { JsonUtil, StrUtil } from "zhi-common"
import CryptoJS from "crypto-js"
import { arrayToBuffer } from "~/src/utils/polyfillUtils.ts"
import {SiyuanDevice} from "zhi-device";

/**
* 知乎网页授权适配器
Expand Down Expand Up @@ -226,12 +229,88 @@ class ZhihuWebAdaptor extends BaseWebApi {
this.logger.info("文章收录到专栏成功")
}

public async uploadFile(file: File): Promise<any> {
public async uploadFile(file: File | Blob): Promise<any> {
this.logger.debug("zhihu start uploadFile =>", file)
if (file instanceof Blob) {
// 1. 获取图片hash
const ab = await file.arrayBuffer()
const bits = arrayToBuffer(ab)
const hash = CryptoJS.MD5(bits.toString("utf8")).toString()
// const wordArray = CryptoJS.enc.Latin1.parse(bits.toString("latin1"))
// const hash = CryptoJS.MD5(wordArray).toString()
const params = JSON.stringify({
image_hash: hash,
source: "article",
})
this.logger.debug("zhihu uploadFile, params =>", params)
const fileResp = await this.webProxyFetch("https://api.zhihu.com/images", [], params, "POST")
this.logger.debug("zhihu uploadFile, fileResp =>", fileResp)

// 开始上传
const upload_file = fileResp.upload_file
if (fileResp.upload_file.state == 1) {
const imgDetail = await this.untilImageDone(upload_file.image_id)
this.logger.debug("imgDetail", imgDetail)
upload_file.object_key = imgDetail.original_hash
} else {
const token = fileResp.upload_token
try {
// https://help.aliyun.com/zh/oss/developer-reference/simple-upload-8?spm=a2c4g.11186623.0.0.7e531769TAYbAL#concept-2161572
let client = new OSS({

Check failure on line 259 in src/adaptors/web/zhihu/zhihuWebAdaptor.ts

View workflow job for this annotation

GitHub Actions / ci

Cannot find name 'OSS'.
endpoint: "https://zhihu-pics-upload.zhimg.com",
accessKeyId: token.access_id,
accessKeySecret: token.access_key,
stsToken: token.access_token,
cname: true,
bucket: "zhihu-pics",
})
const finalUrl = await client.put(upload_file.object_key, new Blob([bits]))
this.logger.debug("zhihu uploadFile finished", { client, finalUrl })
} catch (e) {
this.logger.error("知乎图片上传失败 =>", e)
throw new Error("知乎图片上传失败, 错误原因 =>" + e)
}
}

if (file.type === "image/gif") {
// add extension for gif
upload_file.object_key = upload_file.object_key + ".gif"
}
return {
id: upload_file.object_key,
object_key: upload_file.object_key,
// url: "https://pic1.zhimg.com/80/v2-af46e3b737c2d69b5f24420009f59455_1440w.jpeg",
url: "https://pic4.zhimg.com/" + upload_file.object_key,
// url: 'https://pic1.zhimg.com/80/' + upload_file.object_key + '_hd.png',
}
}

throw new Error("开发中=>zhihu uploadFile")
return {}
}

// ================
// private methods
// ================
private async untilImageDone(image_id: string): Promise<any> {
const that = this
return new Promise(function (resolve, reject) {
function waitToNext() {
that.logger.debug("untilImageDone start processing...", image_id)
;(async () => {
const imgDetail = await that.webProxyFetch(`https://api.zhihu.com/images/${image_id}`, [], {}, "GET")
that.logger.debug("imgDetail", imgDetail)
if (imgDetail.status != "processing") {
that.logger.info("image upload all done")
resolve(imgDetail)
} else {
that.logger.debug("go next", waitToNext)
setTimeout(waitToNext, 300)
}
})()
}
waitToNext()
})
}
}

export { ZhihuWebAdaptor }
3 changes: 2 additions & 1 deletion src/utils/polyfillUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function readFileToBase64(url: string): Promise<string> {
})
}

const uintArrayToBuffer = (uint8Array: Uint8Array): Buffer => {
export const arrayToBuffer = (ab: ArrayBuffer): Buffer => {
const uint8Array = new Uint8Array(ab)
return Buffer.from(uint8Array)
}
14 changes: 14 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ export default defineConfig({
},
injectTo: "head",
},
{
tag: "script",
attrs: {
src: "./libs/alioss/aliyun-oss-sdk-6.16.0.min.js",
},
injectTo: "head",
},
]
: [
{
Expand All @@ -126,6 +133,13 @@ export default defineConfig({
},
injectTo: "head",
},
{
tag: "script",
attrs: {
src: "./libs/alioss/aliyun-oss-sdk-6.16.0.min.js",
},
injectTo: "head",
},
],
data: {
title: "eruda",
Expand Down

0 comments on commit c3e1c54

Please sign in to comment.