-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.ts
277 lines (269 loc) · 6.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import picgo from 'picgo'
import { getIns } from './lib/octokit'
import { PluginConfig } from 'picgo/dist/utils/interfaces'
import { getNow, zip, unzip } from './lib/helper'
import { ImgType, PluginConfig as PlusConfig } from './lib/interface'
const PluginName = 'picgo-plugin-github-plus'
const UploaderName = 'githubPlus'
function initOcto (ctx: picgo) {
const options: PlusConfig = ctx.getConfig('picBed.githubPlus')
if (!options) {
throw new Error("Can't find github-plus config")
}
const ins = getIns(options)
return ins
}
function notic (showNotification: Function, title: string, body?: string) {
showNotification({
title: 'GithubPlus: ' + title,
body
})
}
const SyncGithubMenu = {
label: 'Sync origin',
async handle (ctx: picgo, { showNotification }) {
const octokit = initOcto(ctx)
notic(showNotification, 'Sync origin...')
const githubDataJson = await octokit.getDataJson().catch(e => {
ctx.log.error(e)
notic(showNotification, 'Error at load dataJson', e.message)
throw e
})
// FIXME: 新版本可能拿不到uploaded, 加个默认
const uploaded: ImgType[] = ctx.getConfig('uploaded') || []
const localDataJson = {
data: uploaded.filter(each => each.type === UploaderName).map(zip),
lastSync: (ctx.getConfig(PluginName) || {}).lastSync
}
const { sha, lastSync, data } = githubDataJson
if (localDataJson.lastSync > lastSync) {
try {
if (sha) {
await octokit.updateDataJson({
data: localDataJson,
sha
})
} else {
await octokit.createDataJson(localDataJson)
}
} catch (e) {
ctx.log.error(e)
notic(showNotification, 'Error at sync origin', e.message)
throw e
}
} else {
const newUploaded = data
.map(each => {
const obj = unzip(each)
return {
...obj,
type: UploaderName,
imgUrl: octokit.parseUrl(obj.fileName)
}
})
.concat(uploaded.filter(each => each.type !== UploaderName))
ctx.saveConfig({
uploaded: newUploaded,
[PluginName]: {
lastSync
}
})
}
notic(showNotification, 'Sync successful', 'Succeed to sync origin. Please reload PicGo')
}
}
const PullGithubMenu = {
label: 'Pull origin',
handle: async (ctx: picgo, { showNotification }) => {
const octokit = initOcto(ctx)
notic(showNotification, 'Pull img from origin...')
try {
const { tree } = await octokit.getPathTree()
const imgList: ImgType[] = tree
.filter(each => /\.(jpg|png|jpeg|gif|webp)$/.test(each.path))
.map(each => {
const unzipImg = unzip({
f: each.path,
s: each.sha
})
return {
...unzipImg,
type: UploaderName,
imgUrl: octokit.parseUrl(each.path)
}
})
const uploaded: ImgType[] = (ctx
.getConfig('uploaded') || [])
.filter(each => each.type !== UploaderName)
uploaded.unshift(...imgList)
ctx.saveConfig({
uploaded,
[PluginName]: {
lastSync: getNow()
}
})
notic(showNotification, 'Pull successful', 'Succeed to pull from origin, Please reload PicGo')
} catch (e) {
ctx.log.error(e)
notic(showNotification, 'Error at pull from origin', e.message)
}
}
}
const guiMenu = ctx => {
return [SyncGithubMenu, PullGithubMenu]
}
const handle = async (ctx: picgo) => {
let output = ctx.output
const octokit = initOcto(ctx)
const ret = []
const len = output.length
let index = 0
async function up () {
const img = output[index]
if (index >= len) return
if (!img) {
index++
return up()
}
return octokit
.upload(img)
.then(({ imgUrl, sha }) => {
img.imgUrl = imgUrl
img.sha = sha
ret.push(img)
index++
return up()
})
.catch(e => {
ctx.log.error(e)
ctx.emit('notification', {
title: 'GithubPlus: 上传失败',
body: e.message,
text: ''
})
index++
return up()
})
}
await up()
ctx.saveConfig({
[PluginName]: {
lastSync: getNow()
}
})
ctx.output = ret
return ctx
}
async function onRemove (files: ImgType[], { showNotification }) {
// console.log('1111 =?', this)
const rms = files.filter(each => each.type === UploaderName)
if (rms.length === 0) return
const self: picgo = this
const ins = initOcto(self)
const fail = []
for (let i = 0; i < rms.length; i++) {
const each = rms[i]
await ins.removeFile(each).catch((e) => {
self.log.error(e)
fail.push(each)
})
}
if (fail.length) {
// 确保主线程已经把文件从data.json删掉
const uploaded: ImgType[] = self.getConfig('uploaded') || []
uploaded.unshift(...fail)
self.saveConfig({
uploaded,
[PluginName]: {
lastSync: getNow()
}
})
}
notic(
showNotification,
'删除提示',
fail.length === 0 ? '成功同步删除' : `删除失败${fail.length}个`
)
}
const config = (ctx: picgo): PluginConfig[] => {
let userConfig = ctx.getConfig(`picBed.${UploaderName}`)
if (!userConfig) {
userConfig = {}
}
const conf = [
{
name: 'repo',
type: 'input',
default: userConfig.repo || '',
required: true
},
{
name: 'branch',
type: 'input',
default: userConfig.branch || 'master',
required: false
},
{
name: 'token',
type: 'input',
default: userConfig.token || '',
required: true
},
{
name: 'path',
type: 'input',
default: userConfig.path || '',
required: false
},
{
name: 'customUrl',
type: 'input',
default: userConfig.customUrl || '',
required: false
},
{
name: 'origin',
type: 'list',
default: userConfig.type || 'github',
required: true,
choices: [{
name: 'github',
value: 'github'
}, {
name: 'gitee',
value: 'gitee'
}]
}
]
return conf
}
const syncConfig = (ctx: picgo): PluginConfig[] => {
let userConfig = ctx.getConfig(PluginName)
if (!userConfig) {
userConfig = {}
}
const conf = [
{
name: 'lastSync',
type: 'input',
default: userConfig.lastSync || '',
required: false
}
]
return conf
}
export = (ctx: picgo) => {
const register = () => {
// const { githubPlus } = ctx.getConfig('picBed')
// if (!githubPlus.token) return
// authenticate(githubPlus.token)
ctx.helper.uploader.register(UploaderName, { handle, config })
ctx.on('remove', onRemove)
}
return {
register,
guiMenu, // <-- 在这里注册
uploader: UploaderName,
config: syncConfig
}
}