Skip to content

Commit 7e10655

Browse files
committed
✨ Feature: add smms-v2 support
1 parent aff6326 commit 7e10655

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/plugins/uploader/smms.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import PicGo from '../../core/PicGo'
2+
import { PluginConfig } from '../../utils/interfaces'
23

3-
const postOptions = (fileName: string, image: Buffer): any => {
4+
const postOptions = (fileName: string, image: Buffer, apiToken: string): any => {
45
return {
56
method: 'POST',
6-
url: `https://sm.ms/api/upload`,
7+
url: 'https://sm.ms/api/v2/upload',
78
headers: {
89
contentType: 'multipart/form-data',
9-
'User-Agent': 'PicGo'
10+
'User-Agent': 'PicGo',
11+
Authorization: apiToken
1012
},
1113
formData: {
1214
smfile: {
@@ -21,13 +23,17 @@ const postOptions = (fileName: string, image: Buffer): any => {
2123
}
2224

2325
const handle = async (ctx: PicGo): Promise<PicGo> => {
26+
const smmsConfig = ctx.getConfig('picBed.smms.token')
27+
if (!smmsConfig) {
28+
throw new Error('Can\'t find smms config, please provide api token, see https://sm.ms/home/apitoken')
29+
}
2430
const imgList = ctx.output
2531
for (let i in imgList) {
2632
let image = imgList[i].buffer
2733
if (!image && imgList[i].base64Image) {
2834
image = Buffer.from(imgList[i].base64Image, 'base64')
2935
}
30-
const postConfig = postOptions(imgList[i].fileName, image)
36+
const postConfig = postOptions(imgList[i].fileName, image, smmsConfig)
3137
let body = await ctx.Request.request(postConfig)
3238
body = JSON.parse(body)
3339
if (body.code === 'success') {
@@ -49,7 +55,25 @@ const handle = async (ctx: PicGo): Promise<PicGo> => {
4955
return ctx
5056
}
5157

58+
const config = (ctx: PicGo): PluginConfig[] => {
59+
let userConfig = ctx.getConfig('picBed.smms')
60+
if (!userConfig || typeof userConfig !== 'object') {
61+
userConfig = {}
62+
}
63+
const config = [
64+
{
65+
name: 'token',
66+
message: 'api token',
67+
type: 'input',
68+
default: userConfig.token || '',
69+
required: true
70+
}
71+
]
72+
return config
73+
}
74+
5275
export default {
5376
name: 'SM.MS图床',
54-
handle
77+
handle,
78+
config
5579
}

0 commit comments

Comments
 (0)