@@ -3,6 +3,8 @@ import { getNow, pathJoin } from './helper'
33import { PluginConfig , ImgType } from './interface'
44import urlJoin from 'url-join'
55import { ImgInfo } from 'picgo/dist/utils/interfaces'
6+ const GithubUrl = 'https://api.github.com'
7+ const GiteeUrl = 'https://gitee.com/api/v5'
68
79export class Octo {
810 owner : string = ''
@@ -12,12 +14,14 @@ export class Octo {
1214 token : string = ''
1315 customUrl : string = ''
1416 octokit : Octokit = null
17+ origin : PluginConfig [ 'origin' ]
1518 constructor ( {
1619 repo,
1720 branch,
1821 path = '' ,
1922 token,
20- customUrl = ''
23+ customUrl = '' ,
24+ origin = 'github'
2125 } : PluginConfig ) {
2226 const [ owner , r ] = repo . split ( '/' )
2327 if ( ! r ) throw new Error ( 'Error in repo name' )
@@ -27,11 +31,15 @@ export class Octo {
2731 this . path = path
2832 this . token = token
2933 this . customUrl = customUrl
34+ this . origin = origin
3035 this . octokit = new Octokit ( {
36+ baseUrl : origin === 'github' ? GithubUrl : GiteeUrl ,
3137 auth : token ? `token ${ token } ` : undefined
3238 } )
3339 }
34-
40+ get isGithub ( ) {
41+ return this . origin === 'github'
42+ }
3543 async getTree ( sha ) : Promise < { path : string ; sha : string } [ ] > {
3644 const { owner, repo } = this
3745 const d = await this . octokit . git . getTree ( {
@@ -55,6 +63,14 @@ export class Octo {
5563 }
5664 return { sha, tree }
5765 }
66+ createFile ( params ) {
67+ const { isGithub } = this
68+ const request = this . octokit . request ( `/repos/:owner/:repo/contents/:path` , {
69+ method : isGithub ? 'PUT' : 'POST' ,
70+ ...params
71+ } )
72+ return request
73+ }
5874 async getDataJson ( ) : Promise < {
5975 lastSync : string
6076 data : any [ ]
@@ -97,7 +113,7 @@ export class Octo {
97113 }
98114 createDataJson ( data ) {
99115 const { owner, repo, branch, path } = this
100- return this . octokit . repos . createFile ( {
116+ return this . createFile ( {
101117 owner,
102118 repo,
103119 branch,
@@ -110,7 +126,7 @@ export class Octo {
110126 /* istanbul ignore next */
111127 const { owner, repo, branch, path = '' } = this
112128 const { fileName } = img
113- const d = await this . octokit . repos . createFile ( {
129+ const d = await this . createFile ( {
114130 owner,
115131 repo,
116132 path : pathJoin ( path , fileName ) ,
@@ -139,18 +155,19 @@ export class Octo {
139155 } )
140156 }
141157 parseUrl ( fileName ) {
142- const { owner, repo, path, customUrl, branch } = this
158+ const { origin , owner, repo, path, customUrl, branch } = this
143159 if ( customUrl ) {
144160 return urlJoin ( customUrl , path , fileName )
145161 }
146- return urlJoin (
162+ return origin === 'github' ? urlJoin (
147163 `https://raw.githubusercontent.com/` ,
148164 owner ,
149165 repo ,
150166 branch ,
151167 path ,
152168 fileName
153- )
169+ ) : urlJoin ( `https://gitee.com` , owner , repo , 'raw' , branch , path , fileName )
170+ // https://gitee.com/zwing/test/raw/master/57566062-a7752000-73fa-11e9-99c1-e3a0562bc41d.png
154171 }
155172}
156173
0 commit comments