Skip to content

Commit

Permalink
feat: #1053 新增发布平台 telegra.ph - cookie 验证
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 2, 2024
1 parent da6e5c7 commit 94cdb83
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/adaptors/api/base/baseBlogApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export class BaseBlogApi extends BlogApi {
return this.baseExtendApi.getTags()
}

// ===================================================================================================================

public async apiFormFetch(url: string, headers: any[], formData: FormData) {
const win = this.appInstance.win
const doFetch = win.require(`${this.appInstance.moduleBase}libs/zhi-formdata-fetch/index.cjs`)
Expand Down
38 changes: 31 additions & 7 deletions src/adaptors/api/telegraph/telegraphApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import CookieUtils from "~/src/utils/cookieUtils.ts"
* @see https://telegra.ph/ telegra.ph
*/
class TelegraphApiAdaptor extends BaseBlogApi {
private TPH_UUID_KEY = "tph_uuid"

public async getUsersBlogs(): Promise<UserBlog[]> {
const result: UserBlog[] = []

Expand Down Expand Up @@ -98,11 +100,9 @@ class TelegraphApiAdaptor extends BaseBlogApi {
header: Record<any, any> = {}
) {
const contentType = "text/plain"
const TPH_UUID_KEY = "tph_uuid"
const tphUuidObj = CookieUtils.getCookieObject(this.cfg.corsCookieArray, TPH_UUID_KEY)
this.logger.debug("tphUuidObj =>", tphUuidObj)
if (!StrUtil.isEmptyString(tphUuidObj[TPH_UUID_KEY])) {
header["Cookie"] = `${TPH_UUID_KEY}=${tphUuidObj[TPH_UUID_KEY]}`
const tphUuidObj = CookieUtils.getCookieObject(this.cfg.corsCookieArray, this.TPH_UUID_KEY)
if (!StrUtil.isEmptyString(tphUuidObj[this.TPH_UUID_KEY])) {
header["Cookie"] = `${this.TPH_UUID_KEY}=${tphUuidObj[this.TPH_UUID_KEY]}`
}
const headers = {
"Content-Type": contentType,
Expand Down Expand Up @@ -133,12 +133,36 @@ class TelegraphApiAdaptor extends BaseBlogApi {
*/
private async telegraphFormFetch(url: string, formData: FormData) {
const apiUrl = `${this.cfg.apiUrl}${url}`
const header = {
const tphUuidObj = CookieUtils.getCookieObject(this.cfg.corsCookieArray, this.TPH_UUID_KEY)
let header: Record<any, any> = {}
if (!StrUtil.isEmptyString(tphUuidObj[this.TPH_UUID_KEY])) {
header["Cookie"] = `${this.TPH_UUID_KEY}=${tphUuidObj[this.TPH_UUID_KEY]}`
}
const headers = {
origin: "https://telegra.ph",
referer: "https://telegra.ph/",
...header,
}
const options: RequestInit = {
method: "POST",
headers: headers,
body: formData,
}
this.logger.debug("向 Telegraph 发送表单数据,apiUrl =>", apiUrl)
this.logger.debug("向 Telegraph 发送表单数据,options =>", options)
const res = await fetch(apiUrl, options)
const resText = await res.text()
this.logger.debug("向 Telegraph 发送表单数据,resText =>", resText)
const resJson = JsonUtil.safeParse<any>(resText, {})
if (resJson.error) {
throw new Error(
"telegra.ph 发布错误,注意:切换设备(包括从PC到浏览器环境)需要重新验证,并且获取新token。详细错误 =>" +
resJson.error
)
}
this.logger.debug("向 Telegraph 发送表单数据,resJson =>", resJson)

const resJson = await this.apiFormFetch(apiUrl, [header], formData)
// const resJson = await this.apiFormFetch(apiUrl, [headers], formData)
return resJson
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ const valiConf = async () => {
}
// isAuth和apiStatus同步
formData.dynCfg.isAuth = formData.cfg.apiStatus
if (formData.dynCfg) {
formData.dynCfg.isAuth = formData.cfg.apiStatus
}
// 刷新状态
await saveConf(true)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/cookieUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CookieUtils {
cookies = cookie.parse(value)
this.logger.debug("cookieObj", cookies)
} catch (e) {
this.logger.error("Failed to parse cookie =>" + e)
this.logger.warn("Failed to parse cookie =>" + e)
}

return cookies
Expand Down
2 changes: 1 addition & 1 deletion src/utils/siyuanUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { useSiyuanDevice } from "~/src/composables/useSiyuanDevice.ts"
import { BrowserUtil } from "zhi-device"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"

const route = useRoute()
const logger = createAppLogger("siyuan-util")
const { isInSiyuanWidget, isInSiyuanBrowser } = useSiyuanDevice()

Expand Down Expand Up @@ -120,6 +119,7 @@ export const getSiyuanPageId = async (pageId?: string, force?: boolean) => {
// 3、开发模式模拟传递一个ID
if (BrowserUtil.isInBrowser) {
// 尝试从url参数解析ID
const route = useRoute()
const paramsPageId = route?.params?.id as string
if (!StrUtil.isEmptyString(paramsPageId)) {
return paramsPageId
Expand Down

0 comments on commit 94cdb83

Please sign in to comment.