@@ -8,7 +8,7 @@ const postOptions = (fileName: string, options: IGithubConfig, data: any): IOldR
8
8
const { token, repo } = options
9
9
return {
10
10
method : 'PUT' ,
11
- url : `https://api.github.com/repos/${ repo } /contents/${ encodeURI ( path ) } ${ encodeURI ( fileName ) } ` ,
11
+ url : `https://api.github.com/repos/${ repo } /contents/${ encodeURI ( path ) } ${ encodeURIComponent ( fileName ) } ` ,
12
12
headers : {
13
13
Authorization : `token ${ token } ` ,
14
14
'User-Agent' : 'PicGo' ,
@@ -36,21 +36,36 @@ const handle = async (ctx: IPicGo): Promise<IPicGo> => {
36
36
path : githubOptions . path + encodeURI ( img . fileName )
37
37
}
38
38
const postConfig = postOptions ( img . fileName , githubOptions , data )
39
- const body : {
40
- content : {
41
- download_url : string
39
+ try {
40
+ const body : {
41
+ content : {
42
+ download_url : string
43
+ }
44
+ } = await ctx . request ( postConfig )
45
+ if ( body ) {
46
+ delete img . base64Image
47
+ delete img . buffer
48
+ if ( githubOptions . customUrl ) {
49
+ img . imgUrl = `${ githubOptions . customUrl } /${ encodeURI ( githubOptions . path ) } ${ encodeURIComponent ( img . fileName ) } `
50
+ } else {
51
+ img . imgUrl = body . content . download_url
52
+ }
53
+ } else {
54
+ throw new Error ( 'Server error, please try again' )
42
55
}
43
- } = await ctx . request ( postConfig )
44
- if ( body ) {
45
- delete img . base64Image
46
- delete img . buffer
47
- if ( githubOptions . customUrl ) {
48
- img . imgUrl = `${ githubOptions . customUrl } /${ githubOptions . path } ${ img . fileName } `
56
+ } catch ( e : any ) {
57
+ // handle duplicate images
58
+ if ( e . statusCode === 422 ) {
59
+ delete img . base64Image
60
+ delete img . buffer
61
+ if ( githubOptions . customUrl ) {
62
+ img . imgUrl = `${ githubOptions . customUrl } /${ encodeURI ( githubOptions . path ) } ${ encodeURIComponent ( img . fileName ) } `
63
+ } else {
64
+ img . imgUrl = `https://raw.githubusercontent.com/${ githubOptions . repo } /${ githubOptions . branch } /${ encodeURI ( githubOptions . path ) } ${ encodeURIComponent ( img . fileName ) } `
65
+ }
49
66
} else {
50
- img . imgUrl = body . content . download_url
67
+ throw e
51
68
}
52
- } else {
53
- throw new Error ( 'Server error, please try again' )
54
69
}
55
70
}
56
71
}
0 commit comments