1
1
import PicGo from '../../core/PicGo'
2
+ import { PluginConfig } from '../../utils/interfaces'
2
3
3
- const postOptions = ( fileName : string , image : Buffer ) : any => {
4
+ const postOptions = ( fileName : string , image : Buffer , apiToken : string ) : any => {
4
5
return {
5
6
method : 'POST' ,
6
- url : ` https://sm.ms/api/upload` ,
7
+ url : ' https://sm.ms/api/v2/ upload' ,
7
8
headers : {
8
9
contentType : 'multipart/form-data' ,
9
- 'User-Agent' : 'PicGo'
10
+ 'User-Agent' : 'PicGo' ,
11
+ Authorization : apiToken
10
12
} ,
11
13
formData : {
12
14
smfile : {
@@ -21,13 +23,17 @@ const postOptions = (fileName: string, image: Buffer): any => {
21
23
}
22
24
23
25
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
+ }
24
30
const imgList = ctx . output
25
31
for ( let i in imgList ) {
26
32
let image = imgList [ i ] . buffer
27
33
if ( ! image && imgList [ i ] . base64Image ) {
28
34
image = Buffer . from ( imgList [ i ] . base64Image , 'base64' )
29
35
}
30
- const postConfig = postOptions ( imgList [ i ] . fileName , image )
36
+ const postConfig = postOptions ( imgList [ i ] . fileName , image , smmsConfig )
31
37
let body = await ctx . Request . request ( postConfig )
32
38
body = JSON . parse ( body )
33
39
if ( body . code === 'success' ) {
@@ -49,7 +55,25 @@ const handle = async (ctx: PicGo): Promise<PicGo> => {
49
55
return ctx
50
56
}
51
57
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
+
52
75
export default {
53
76
name : 'SM.MS图床' ,
54
- handle
77
+ handle,
78
+ config
55
79
}
0 commit comments