-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimgur_upload.js
More file actions
38 lines (29 loc) · 869 Bytes
/
imgur_upload.js
File metadata and controls
38 lines (29 loc) · 869 Bytes
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
// @ts-check
const { URLSearchParams } = require("url")
const { fetch, getProxyAgent } = require("./util")
const agent = getProxyAgent()
/**
* 上传到 imgur.com
* @param {string} image 图片地址或图片的base64数据
* @return {Promise<string>} 图片在 imgur.com 上的地址
*/
const upload = async (image) => {
const params = new URLSearchParams()
params.append("image", image)
const r = await fetch("https://api.imgur.com/3/image", {
method: "POST",
body: params,
headers: {
"Authorization": "Client-ID f0ea04148a54268",
},
timeout: 10000,
agent,
})
const json = await r.json()
if (!json.success) {
console.error("status:", json.status)
throw new Error(JSON.stringify(json.data.error))
}
return json.data.link
}
module.exports = upload