@@ -3,9 +3,24 @@ import { Notification } from 'electron'
33import { getIns } from './lib/octokit'
44import { PluginConfig } from 'picgo/dist/utils/interfaces'
55import { getNow , zip , unzip } from './lib/helper'
6- import { ImgType , PluginConfig as PlusConfig } from './lib/interface'
6+ import {
7+ ImgType ,
8+ PluginConfig as PlusConfig ,
9+ ImgZipType
10+ } from './lib/interface'
711const PluginName = 'picgo-plugin-github-plus'
812const UploaderName = 'githubPlus'
13+
14+ function initOcto ( ctx : picgo ) {
15+ const options : PlusConfig = ctx . getConfig ( 'picBed.githubPlus' )
16+ if ( ! options ) {
17+ throw new Error ( "Can't find github-plus config" )
18+ }
19+ const ins = getIns ( options )
20+ ins . authenticate ( )
21+ return ins
22+ }
23+
924function notic ( title , body ?: string ) {
1025 const notification = new Notification ( {
1126 title : 'GithubPlus: ' + title ,
@@ -14,75 +29,83 @@ function notic (title, body?: string) {
1429 notification . show ( )
1530}
1631
17- const guiMenu = ( ctx : picgo ) => {
18- return [
19- {
20- label : 'Sync github' ,
21- async handle ( ctx : picgo ) {
22- const options : PlusConfig = ctx . getConfig ( 'picBed.githubPlus' )
23- if ( ! options ) {
24- throw new Error ( "Can't find github-plus config" )
25- }
26- notic ( 'Sync...' )
27- const octokit = getIns ( options )
28- octokit . authenticate ( )
29- const githubDataJson = await octokit . getDataJson ( ) . catch ( e => {
30- notic ( 'Error in load dataJson' , e . message )
31- throw e
32- } )
33- const uploaded : ImgType [ ] = ctx . getConfig ( 'uploaded' )
34- const localDataJson = {
35- data : uploaded . filter ( each => each . type === UploaderName ) . map ( zip ) ,
36- lastSync : ( ctx . getConfig ( PluginName ) || { } ) . lastSync
37- }
38- const { sha, lastSync, data } = githubDataJson
39- if ( localDataJson . lastSync > lastSync ) {
40- try {
41- if ( sha ) {
42- await octokit . updateDataJson ( {
43- data : localDataJson ,
44- sha
45- } )
46- } else {
47- await octokit . createDataJson ( localDataJson )
48- }
49- } catch ( e ) {
50- notic ( 'Error in sync github' , e . message )
51- throw e
52- }
53- } else {
54- const newUploaded = data
55- . map ( each => {
56- const obj = unzip ( each )
57- return {
58- ...obj ,
59- type : UploaderName ,
60- imgUrl : octokit . parseUrl ( obj . fileName )
61- }
62- } )
63- . concat ( uploaded . filter ( each => each . type !== UploaderName ) )
64- ctx . saveConfig ( {
65- uploaded : newUploaded ,
66- [ PluginName ] : {
67- lastSync
68- }
32+ const SyncGithubMenu = {
33+ label : 'Sync github' ,
34+ async handle ( ctx : picgo ) {
35+ const octokit = initOcto ( ctx )
36+ notic ( 'Sync...' )
37+ const githubDataJson = await octokit . getDataJson ( ) . catch ( e => {
38+ notic ( 'Error in load dataJson' , e . message )
39+ throw e
40+ } )
41+ const uploaded : ImgType [ ] = ctx . getConfig ( 'uploaded' )
42+ const localDataJson = {
43+ data : uploaded . filter ( each => each . type === UploaderName ) . map ( zip ) ,
44+ lastSync : ( ctx . getConfig ( PluginName ) || { } ) . lastSync
45+ }
46+ const { sha, lastSync, data } = githubDataJson
47+ if ( localDataJson . lastSync > lastSync ) {
48+ try {
49+ if ( sha ) {
50+ await octokit . updateDataJson ( {
51+ data : localDataJson ,
52+ sha
6953 } )
54+ } else {
55+ await octokit . createDataJson ( localDataJson )
7056 }
71- notic ( 'Symc succeed' , 'Succeed to sync github' )
57+ } catch ( e ) {
58+ notic ( 'Error in sync github' , e . message )
59+ throw e
7260 }
61+ } else {
62+ const newUploaded = data
63+ . map ( each => {
64+ const obj = unzip ( each )
65+ return {
66+ ...obj ,
67+ type : UploaderName ,
68+ imgUrl : octokit . parseUrl ( obj . fileName )
69+ }
70+ } )
71+ . concat ( uploaded . filter ( each => each . type !== UploaderName ) )
72+ ctx . saveConfig ( {
73+ uploaded : newUploaded ,
74+ [ PluginName ] : {
75+ lastSync
76+ }
77+ } )
7378 }
74- ]
79+ notic ( 'Sync succeed' , 'Succeed to sync github' )
80+ }
81+ }
82+
83+ const PullGithubMenu = {
84+ label : 'Pull github' ,
85+ handle : async ( ctx : picgo ) => {
86+ const octokit = initOcto ( ctx )
87+ notic ( 'Pull...' )
88+ const { tree } = await octokit . getPathTree ( )
89+ // TODO: transform github obj to imgType
90+ const imgList : ImgType [ ] = tree
91+ . filter ( each => / \. ( j p g | p n g ) $ / . test ( each . path ) )
92+ . map ( each => {
93+ return unzip ( {
94+ f : each . path ,
95+ id : each . sha
96+ } )
97+ } )
98+ // const tree =
99+ }
100+ }
101+
102+ const guiMenu = ( ) => {
103+ return [ SyncGithubMenu , PullGithubMenu ]
75104}
76105
77106const handle = async ( ctx : picgo ) => {
78- const options : PlusConfig = ctx . getConfig ( 'picBed.githubPlus' )
79107 let output = ctx . output
80- // do something for uploading
81- if ( ! options ) {
82- throw new Error ( "Can't find github-plus config" )
83- }
84- const octokit = getIns ( options )
85- octokit . authenticate ( )
108+ const octokit = initOcto ( ctx )
86109 for ( let i in output ) {
87110 try {
88111 const img = output [ i ]
@@ -110,12 +133,7 @@ async function onRemove (files: ImgType[]) {
110133 const rms = files . filter ( each => each . type === UploaderName )
111134 if ( rms . length === 0 ) return
112135 const self : picgo = this
113- const options : PlusConfig = self . getConfig ( 'picBed.githubPlus' )
114- if ( ! options ) {
115- notic ( 'Error' , "Can't find github-plus config" )
116- return
117- }
118- const ins = getIns ( options )
136+ const ins = initOcto ( self )
119137 ins . authenticate ( )
120138 const fail = [ ]
121139 for ( let i = 0 ; i < rms . length ; i ++ ) {
0 commit comments