-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExternalPicgo.ts
122 lines (106 loc) · 3.43 KB
/
ExternalPicgo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* GNU GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* Copyright (C) 2022-2024 Terwer, Inc. <https://terwer.space/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
import { ILogger, simpleLogger } from "zhi-lib-base"
import ExternalPicgoConfigDb from "../db/externalPicGo"
import { IImgInfo, IPicGo } from "../types"
import { PicgoTypeEnum } from "../utils/enums"
import { browserPathJoin } from "../utils/browserUtils"
import { isFileOrBlob } from "../utils/common"
/**
*外部的PicGO 上传 Api
*
* @since 0.6.0
* @version 1.6.0
* @author terwer
*/
class ExternalPicgo {
private logger: ILogger
private requestUrl = "http://127.0.0.1:36677"
private readonly endpointUrl = "/upload"
public db: ExternalPicgoConfigDb
constructor(ctx: IPicGo, isDev?: boolean) {
this.logger = simpleLogger("external-picgo", "external-picgo", isDev)
this.db = new ExternalPicgoConfigDb(ctx)
}
/**
* 上传图片到PicGO
*
* @param input 路径数组,可为空,为空上传剪贴板
*/
public async upload(input?: any[]): Promise<IImgInfo[] | Error> {
const useBundledPicgo = this.db.get("useBundledPicgo")
const picgoType = this.db.get("picgoType")
if (useBundledPicgo) {
throw new Error("bundled picgo cannot use extenal picgo api")
}
if (picgoType !== PicgoTypeEnum.App) {
throw new Error(`picgoType ${picgoType} is not supported via external picgo api`)
}
// check blob
let hasBlob = false
if (input) {
for (const inputItem of input) {
if (isFileOrBlob(inputItem) || typeof inputItem !== "string") {
hasBlob = true
break
}
}
}
if (hasBlob) {
throw new Error("blob is not supported via external picgo api")
}
this.requestUrl = this.db.get("extPicgoApiUrl") ?? this.requestUrl
let ret: IImgInfo[] = []
const fetchOptions = {
method: "POST",
}
let data
// 传递了路径,上传具体图片,否则上传剪贴板
if (input) {
data = { list: input }
}
// 数据不为空才传递
if (data) {
Object.assign(fetchOptions, {
body: JSON.stringify(data),
})
}
Object.assign(fetchOptions, {
headers: {
"Content-Type": "application/json",
"User-Agent": "Terwer/0.1.0",
},
})
// 发送请求
const apiUrl = browserPathJoin(this.requestUrl, this.endpointUrl)
this.logger.debug("调用HTTP请求上传图片到PicGO,apiUrl=>", apiUrl)
this.logger.debug("调用HTTP请求上传图片到PicGO,fetchOps=>", fetchOptions)
// 使用兼容的fetch调用并返回统一的JSON数据
const response = await fetch(apiUrl, fetchOptions)
const resJson = await response.json()
this.logger.debug("调用HTTP请求上传图片到PicGO,resJson=>", resJson)
if (resJson.success) {
const rtnArray: IImgInfo[] = []
if (resJson.result && resJson.result.length > 0) {
resJson.result.forEach((img: string) => {
const rtnItem = {
fileName: img.substring(img.lastIndexOf("/") + 1),
imgUrl: img,
}
rtnArray.push(rtnItem)
})
}
ret = rtnArray
} else {
throw new Error("调用HTTP上传到PicGO失败,请检查配置=>" + resJson.message)
}
return Promise.resolve(ret)
}
}
export { ExternalPicgo }