Skip to content

Commit b84ea0d

Browse files
committed
feat: 🎸 support for gitee
1 parent 89ce845 commit b84ea0d

4 files changed

Lines changed: 53 additions & 17 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ plugin for [PicGo](https://github.com/Molunerfinn/PicGo)
1717
- token: github `access token`
1818
- path: file path
1919
- customUrl: used to insead of `https://raw.githubusercontent.com/:owner/:repo/:branch/:path/:filename`, eg: `${customUrl}/path/filename.jpg`
20+
- origin: `github` or `gitee`, default `github`
2021

2122
makesure the `customUrl` can access your `repo`
2223

2324
![](https://zwing.site/imgur/57566062-a7752000-73fa-11e9-99c1-e3a0562bc41d.png)
2425

2526
### Menu
2627

27-
- Sync github: Just sync `data.json` (use latest updated)
28-
- Pull github: Pull all `img` info from github (**force** and **override** local `data.json`)
28+
- Sync origin: Just sync `data.json` (use latest updated)
29+
- Pull origin: Pull all `img` info from origin (**force** and **override** local `data.json`)
30+
31+
## Support gitee
32+
33+
由于`gitee`文件大小有`1mb`限制, 所以超过`1mb`的文件无法通过外链获取

index.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ function notic (showNotification: Function, title: string, body?: string) {
2222
}
2323

2424
const SyncGithubMenu = {
25-
label: 'Sync github',
25+
label: 'Sync origin',
2626
async handle (ctx: picgo, { showNotification }) {
2727
const octokit = initOcto(ctx)
28-
notic(showNotification, 'Sync github...')
28+
notic(showNotification, 'Sync origin...')
2929
const githubDataJson = await octokit.getDataJson().catch(e => {
3030
ctx.log.error(e)
3131
notic(showNotification, 'Error at load dataJson', e.message)
@@ -49,7 +49,7 @@ const SyncGithubMenu = {
4949
}
5050
} catch (e) {
5151
ctx.log.error(e)
52-
notic(showNotification, 'Error at sync github', e.message)
52+
notic(showNotification, 'Error at sync origin', e.message)
5353
throw e
5454
}
5555
} else {
@@ -70,15 +70,15 @@ const SyncGithubMenu = {
7070
}
7171
})
7272
}
73-
notic(showNotification, 'Sync successful', 'Succeed to sync github')
73+
notic(showNotification, 'Sync successful', 'Succeed to sync origin')
7474
}
7575
}
7676

7777
const PullGithubMenu = {
78-
label: 'Pull github',
78+
label: 'Pull origin',
7979
handle: async (ctx: picgo, { showNotification }) => {
8080
const octokit = initOcto(ctx)
81-
notic(showNotification, 'Pull img from github...')
81+
notic(showNotification, 'Pull img from origin...')
8282
try {
8383
const { tree } = await octokit.getPathTree()
8484
const imgList: ImgType[] = tree
@@ -104,10 +104,10 @@ const PullGithubMenu = {
104104
lastSync: getNow()
105105
}
106106
})
107-
notic(showNotification, 'Pull successful', 'Succeed to pull from github')
107+
notic(showNotification, 'Pull successful', 'Succeed to pull from origin')
108108
} catch (e) {
109109
ctx.log.error(e)
110-
notic(showNotification, 'Error at pull from github', e.message)
110+
notic(showNotification, 'Error at pull from origin', e.message)
111111
}
112112
}
113113
}
@@ -226,6 +226,19 @@ const config = (ctx: picgo): PluginConfig[] => {
226226
type: 'input',
227227
default: userConfig.customUrl || '',
228228
required: false
229+
},
230+
{
231+
name: 'origin',
232+
type: 'list',
233+
default: userConfig.type || 'github',
234+
required: true,
235+
choices: [{
236+
name: 'github',
237+
value: 'github'
238+
}, {
239+
name: 'gitee',
240+
value: 'gitee'
241+
}]
229242
}
230243
]
231244
return conf

lib/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface PluginConfig {
44
path?: string,
55
token: string,
66
customUrl?: string
7+
origin?: 'github' | 'gitee'
78
}
89

910
export type ImgType = {

lib/octokit.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { getNow, pathJoin } from './helper'
33
import { PluginConfig, ImgType } from './interface'
44
import urlJoin from 'url-join'
55
import { ImgInfo } from 'picgo/dist/utils/interfaces'
6+
const GithubUrl = 'https://api.github.com'
7+
const GiteeUrl = 'https://gitee.com/api/v5'
68

79
export 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

Comments
 (0)