@@ -5,8 +5,10 @@ import (
5
5
"encoding/json"
6
6
"fmt"
7
7
"log"
8
+ "net/http"
8
9
"os"
9
10
"path/filepath"
11
+ "strings"
10
12
"wechatDataBackup/pkg/utils"
11
13
"wechatDataBackup/pkg/wechat"
12
14
@@ -18,9 +20,37 @@ const (
18
20
defaultConfig = "config"
19
21
configDefaultUserKey = "userConfig.defaultUser"
20
22
configUsersKey = "userConfig.users"
21
- appVersion = "v1.0.3"
23
+ configExportPathKey = "exportPath"
24
+ appVersion = "v1.0.4"
22
25
)
23
26
27
+ type FileLoader struct {
28
+ http.Handler
29
+ FilePrefix string
30
+ }
31
+
32
+ func NewFileLoader (prefix string ) * FileLoader {
33
+ return & FileLoader {FilePrefix : prefix }
34
+ }
35
+
36
+ func (h * FileLoader ) SetFilePrefix (prefix string ) {
37
+ h .FilePrefix = prefix
38
+ log .Println ("SetFilePrefix" , h .FilePrefix )
39
+ }
40
+
41
+ func (h * FileLoader ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
42
+ var err error
43
+ requestedFilename := h .FilePrefix + "\\ " + strings .TrimPrefix (req .URL .Path , "/" )
44
+ // log.Println("Requesting file:", requestedFilename)
45
+ fileData , err := os .ReadFile (requestedFilename )
46
+ if err != nil {
47
+ res .WriteHeader (http .StatusBadRequest )
48
+ res .Write ([]byte (fmt .Sprintf ("Could not load file %s" , requestedFilename )))
49
+ }
50
+
51
+ res .Write (fileData )
52
+ }
53
+
24
54
// App struct
25
55
type App struct {
26
56
ctx context.Context
@@ -29,6 +59,8 @@ type App struct {
29
59
defaultUser string
30
60
users []string
31
61
firstStart bool
62
+
63
+ FLoader * FileLoader
32
64
}
33
65
34
66
type WeChatInfo struct {
@@ -51,21 +83,38 @@ type WeChatAccountInfos struct {
51
83
Total int `json:"Total"`
52
84
}
53
85
86
+ type ErrorMessage struct {
87
+ ErrorStr string `json:"error"`
88
+ }
89
+
54
90
// NewApp creates a new App application struct
55
91
func NewApp () * App {
56
92
a := & App {}
57
93
94
+ a .FLoader = NewFileLoader (".\\ " )
58
95
viper .SetConfigName (defaultConfig )
59
96
viper .SetConfigType ("json" )
60
97
viper .AddConfigPath ("." )
61
98
if err := viper .ReadInConfig (); err == nil {
62
99
a .defaultUser = viper .GetString (configDefaultUserKey )
63
100
a .users = viper .GetStringSlice (configUsersKey )
101
+ prefix := viper .GetString (configExportPathKey )
102
+ if prefix != "" {
103
+ log .Println ("SetFilePrefix" , prefix )
104
+ a .FLoader .SetFilePrefix (prefix )
105
+ }
106
+
107
+ a .scanAccountByPath (prefix )
64
108
// log.Println(a.defaultUser)
65
109
// log.Println(a.users)
66
110
} else {
111
+ if a .scanAccountByPath (".\\ " ) != nil {
112
+ log .Println ("not config exist" )
113
+ }
114
+ }
115
+ log .Printf ("default: %s users: %v\n " , a .defaultUser , a .users )
116
+ if len (a .users ) == 0 {
67
117
a .firstStart = true
68
- log .Println ("not config exist" )
69
118
}
70
119
71
120
return a
@@ -93,6 +142,11 @@ func (a *App) GetWeChatAllInfo() string {
93
142
infoList .Info = make ([]WeChatInfo , 0 )
94
143
infoList .Total = 0
95
144
145
+ if a .provider != nil {
146
+ a .provider .WechatWechatDataProviderClose ()
147
+ a .provider = nil
148
+ }
149
+
96
150
a .infoList = wechat .GetWeChatAllInfo ()
97
151
for i := range a .infoList .Info {
98
152
var info WeChatInfo
@@ -135,12 +189,13 @@ func (a *App) ExportWeChatAllData(full bool, acountName string) {
135
189
return
136
190
}
137
191
138
- _ , err := os .Stat (".\\ User" )
192
+ prefixExportPath := a .FLoader .FilePrefix + "\\ User\\ "
193
+ _ , err := os .Stat (prefixExportPath )
139
194
if err != nil {
140
- os .Mkdir (". \\ User" , os .ModeDir )
195
+ os .Mkdir (prefixExportPath , os .ModeDir )
141
196
}
142
197
143
- expPath := ". \\ User \\ " + pInfo .AcountName
198
+ expPath := prefixExportPath + pInfo .AcountName
144
199
_ , err = os .Stat (expPath )
145
200
if err == nil {
146
201
if ! full {
@@ -177,7 +232,7 @@ func (a *App) ExportWeChatAllData(full bool, acountName string) {
177
232
}()
178
233
}
179
234
180
- func (a * App ) createWechatDataProvider (resPath string ) error {
235
+ func (a * App ) createWechatDataProvider (resPath string , prefix string ) error {
181
236
if a .provider != nil && a .provider .SelfInfo != nil && filepath .Base (resPath ) == a .provider .SelfInfo .UserName {
182
237
log .Println ("WechatDataProvider not need create:" , a .provider .SelfInfo .UserName )
183
238
return nil
@@ -189,7 +244,7 @@ func (a *App) createWechatDataProvider(resPath string) error {
189
244
log .Println ("createWechatDataProvider WechatWechatDataProviderClose" )
190
245
}
191
246
192
- provider , err := wechat .CreateWechatDataProvider (resPath )
247
+ provider , err := wechat .CreateWechatDataProvider (resPath , prefix )
193
248
if err != nil {
194
249
log .Println ("CreateWechatDataProvider failed:" , resPath )
195
250
return err
@@ -202,29 +257,52 @@ func (a *App) createWechatDataProvider(resPath string) error {
202
257
}
203
258
204
259
func (a * App ) WeChatInit () {
205
- expPath := ".\\ User\\ " + a .defaultUser
206
- if a .createWechatDataProvider (expPath ) == nil {
260
+ if len (a .defaultUser ) == 0 {
261
+ log .Println ("not defaultUser" )
262
+ return
263
+ }
264
+
265
+ expPath := a .FLoader .FilePrefix + "\\ User\\ " + a .defaultUser
266
+ prefixPath := "\\ User\\ " + a .defaultUser
267
+ wechat .ExportWeChatHeadImage (expPath )
268
+ if a .createWechatDataProvider (expPath , prefixPath ) == nil {
207
269
infoJson , _ := json .Marshal (a .provider .SelfInfo )
208
270
runtime .EventsEmit (a .ctx , "selfInfo" , string (infoJson ))
209
271
}
210
272
}
211
273
212
274
func (a * App ) GetWechatSessionList (pageIndex int , pageSize int ) string {
213
- expPath := ". \\ User \\ " + a . defaultUser
214
- if a . createWechatDataProvider ( expPath ) != nil {
215
- return ""
275
+ if a . provider == nil {
276
+ log . Println ( "provider not init" )
277
+ return "{ \" Total \" :0} "
216
278
}
217
279
log .Printf ("pageIndex: %d\n " , pageIndex )
218
280
list , err := a .provider .WeChatGetSessionList (pageIndex , pageSize )
219
281
if err != nil {
220
- return ""
282
+ return "{ \" Total \" :0} "
221
283
}
222
284
223
285
listStr , _ := json .Marshal (list )
224
286
log .Println ("GetWechatSessionList:" , list .Total )
225
287
return string (listStr )
226
288
}
227
289
290
+ func (a * App ) GetWechatContactList (pageIndex int , pageSize int ) string {
291
+ if a .provider == nil {
292
+ log .Println ("provider not init" )
293
+ return "{\" Total\" :0}"
294
+ }
295
+ log .Printf ("pageIndex: %d\n " , pageIndex )
296
+ list , err := a .provider .WeChatGetContactList (pageIndex , pageSize )
297
+ if err != nil {
298
+ return "{\" Total\" :0}"
299
+ }
300
+
301
+ listStr , _ := json .Marshal (list )
302
+ log .Println ("WeChatGetContactList:" , list .Total )
303
+ return string (listStr )
304
+ }
305
+
228
306
func (a * App ) GetWechatMessageListByTime (userName string , time int64 , pageSize int , direction string ) string {
229
307
log .Println ("GetWechatMessageList:" , userName , pageSize , time , direction )
230
308
if len (userName ) == 0 {
@@ -284,6 +362,7 @@ func (a *App) GetWechatMessageDate(userName string) string {
284
362
func (a * App ) setCurrentConfig () {
285
363
viper .Set (configDefaultUserKey , a .defaultUser )
286
364
viper .Set (configUsersKey , a .users )
365
+ viper .Set (configExportPathKey , a .FLoader .FilePrefix )
287
366
err := viper .SafeWriteConfig ()
288
367
if err != nil {
289
368
log .Println (err )
@@ -314,7 +393,9 @@ func (a *App) OpenFileOrExplorer(filePath string, explorer bool) string {
314
393
// filePath = root + filePath[1:]
315
394
// }
316
395
// log.Println("OpenFileOrExplorer:", filePath)
317
- err := utils .OpenFileOrExplorer (filePath , explorer )
396
+
397
+ path := a .FLoader .FilePrefix + filePath
398
+ err := utils .OpenFileOrExplorer (path , explorer )
318
399
if err != nil {
319
400
return "{\" result\" : \" OpenFileOrExplorer failed\" , \" status\" :\" failed\" }"
320
401
}
@@ -349,13 +430,14 @@ func (a *App) GetWechatLocalAccountInfo() string {
349
430
infos .Total = 0
350
431
infos .CurrentAccount = a .defaultUser
351
432
for i := range a .users {
352
- resPath := ". \\ User\\ " + a .users [i ]
433
+ resPath := a . FLoader . FilePrefix + " \\ User\\ " + a .users [i ]
353
434
if _ , err := os .Stat (resPath ); err != nil {
354
435
log .Println ("GetWechatLocalAccountInfo:" , resPath , err )
355
436
continue
356
437
}
357
438
358
- info , err := wechat .WechatGetAccountInfo (resPath , a .users [i ])
439
+ prefixResPath := "\\ User\\ " + a .users [i ]
440
+ info , err := wechat .WechatGetAccountInfo (resPath , prefixResPath , a .users [i ])
359
441
if err != nil {
360
442
log .Println ("GetWechatLocalAccountInfo" , err )
361
443
continue
@@ -386,3 +468,127 @@ func (a *App) WechatSwitchAccount(account string) bool {
386
468
387
469
return false
388
470
}
471
+
472
+ func (a * App ) GetExportPathStat () string {
473
+ path := a .FLoader .FilePrefix
474
+ log .Println ("utils.GetPathStat ++" )
475
+ stat , err := utils .GetPathStat (path )
476
+ log .Println ("utils.GetPathStat --" )
477
+ if err != nil {
478
+ log .Println ("GetPathStat error:" , path , err )
479
+ var msg ErrorMessage
480
+ msg .ErrorStr = fmt .Sprintf ("%s:%v" , path , err )
481
+ msgStr , _ := json .Marshal (msg )
482
+ return string (msgStr )
483
+ }
484
+
485
+ statString , _ := json .Marshal (stat )
486
+
487
+ return string (statString )
488
+ }
489
+
490
+ func (a * App ) ExportPathIsCanWrite () bool {
491
+ path := a .FLoader .FilePrefix
492
+ return utils .PathIsCanWriteFile (path )
493
+ }
494
+
495
+ func (a * App ) OpenExportPath () {
496
+ path := a .FLoader .FilePrefix
497
+ runtime .BrowserOpenURL (a .ctx , path )
498
+ }
499
+
500
+ func (a * App ) OpenDirectoryDialog () string {
501
+ dialogOptions := runtime.OpenDialogOptions {
502
+ Title : "选择导出路径" ,
503
+ }
504
+ selectedDir , err := runtime .OpenDirectoryDialog (a .ctx , dialogOptions )
505
+ if err != nil {
506
+ log .Println ("OpenDirectoryDialog:" , err )
507
+ return ""
508
+ }
509
+
510
+ if selectedDir == "" {
511
+ log .Println ("Cancel selectedDir" )
512
+ return ""
513
+ }
514
+
515
+ if selectedDir == a .FLoader .FilePrefix {
516
+ log .Println ("same path No need SetFilePrefix" )
517
+ return ""
518
+ }
519
+
520
+ if ! utils .PathIsCanWriteFile (selectedDir ) {
521
+ log .Println ("PathIsCanWriteFile:" , selectedDir , "error" )
522
+ return ""
523
+ }
524
+
525
+ a .FLoader .SetFilePrefix (selectedDir )
526
+ log .Println ("OpenDirectoryDialog:" , selectedDir )
527
+ a .scanAccountByPath (selectedDir )
528
+ return selectedDir
529
+ }
530
+
531
+ func (a * App ) scanAccountByPath (path string ) error {
532
+ infos := WeChatAccountInfos {}
533
+ infos .Info = make ([]wechat.WeChatAccountInfo , 0 )
534
+ infos .Total = 0
535
+ infos .CurrentAccount = ""
536
+
537
+ userPath := path + "\\ User\\ "
538
+ if _ , err := os .Stat (userPath ); err != nil {
539
+ return err
540
+ }
541
+
542
+ dirs , err := os .ReadDir (userPath )
543
+ if err != nil {
544
+ log .Println ("ReadDir" , err )
545
+ return err
546
+ }
547
+
548
+ for i := range dirs {
549
+ if ! dirs [i ].Type ().IsDir () {
550
+ continue
551
+ }
552
+ log .Println ("dirs[i].Name():" , dirs [i ].Name ())
553
+ resPath := path + "\\ User\\ " + dirs [i ].Name ()
554
+ prefixResPath := "\\ User\\ " + dirs [i ].Name ()
555
+ info , err := wechat .WechatGetAccountInfo (resPath , prefixResPath , dirs [i ].Name ())
556
+ if err != nil {
557
+ log .Println ("GetWechatLocalAccountInfo" , err )
558
+ continue
559
+ }
560
+
561
+ infos .Info = append (infos .Info , * info )
562
+ infos .Total += 1
563
+ }
564
+
565
+ users := make ([]string , 0 )
566
+ for i := 0 ; i < infos .Total ; i ++ {
567
+ users = append (users , infos .Info [i ].AccountName )
568
+ }
569
+
570
+ a .users = users
571
+ found := false
572
+ for i := range a .users {
573
+ if a .defaultUser == a .users [i ] {
574
+ found = true
575
+ }
576
+ }
577
+
578
+ if ! found {
579
+ a .defaultUser = ""
580
+ }
581
+ if a .defaultUser == "" && len (a .users ) > 0 {
582
+ a .defaultUser = a .users [0 ]
583
+ }
584
+
585
+ if len (a .users ) > 0 {
586
+ a .setCurrentConfig ()
587
+ }
588
+
589
+ return nil
590
+ }
591
+
592
+ func (a * App ) OepnLogFileExplorer () {
593
+ utils .OpenFileOrExplorer (".\\ app.log" , true )
594
+ }
0 commit comments