@@ -5,8 +5,10 @@ import (
55 "encoding/json"
66 "fmt"
77 "log"
8+ "net/http"
89 "os"
910 "path/filepath"
11+ "strings"
1012 "wechatDataBackup/pkg/utils"
1113 "wechatDataBackup/pkg/wechat"
1214
@@ -18,9 +20,37 @@ const (
1820 defaultConfig = "config"
1921 configDefaultUserKey = "userConfig.defaultUser"
2022 configUsersKey = "userConfig.users"
21- appVersion = "v1.0.3"
23+ configExportPathKey = "exportPath"
24+ appVersion = "v1.0.4"
2225)
2326
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+
2454// App struct
2555type App struct {
2656 ctx context.Context
@@ -29,6 +59,8 @@ type App struct {
2959 defaultUser string
3060 users []string
3161 firstStart bool
62+
63+ FLoader * FileLoader
3264}
3365
3466type WeChatInfo struct {
@@ -51,21 +83,38 @@ type WeChatAccountInfos struct {
5183 Total int `json:"Total"`
5284}
5385
86+ type ErrorMessage struct {
87+ ErrorStr string `json:"error"`
88+ }
89+
5490// NewApp creates a new App application struct
5591func NewApp () * App {
5692 a := & App {}
5793
94+ a .FLoader = NewFileLoader (".\\ " )
5895 viper .SetConfigName (defaultConfig )
5996 viper .SetConfigType ("json" )
6097 viper .AddConfigPath ("." )
6198 if err := viper .ReadInConfig (); err == nil {
6299 a .defaultUser = viper .GetString (configDefaultUserKey )
63100 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 )
64108 // log.Println(a.defaultUser)
65109 // log.Println(a.users)
66110 } 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 {
67117 a .firstStart = true
68- log .Println ("not config exist" )
69118 }
70119
71120 return a
@@ -93,6 +142,11 @@ func (a *App) GetWeChatAllInfo() string {
93142 infoList .Info = make ([]WeChatInfo , 0 )
94143 infoList .Total = 0
95144
145+ if a .provider != nil {
146+ a .provider .WechatWechatDataProviderClose ()
147+ a .provider = nil
148+ }
149+
96150 a .infoList = wechat .GetWeChatAllInfo ()
97151 for i := range a .infoList .Info {
98152 var info WeChatInfo
@@ -135,12 +189,13 @@ func (a *App) ExportWeChatAllData(full bool, acountName string) {
135189 return
136190 }
137191
138- _ , err := os .Stat (".\\ User" )
192+ prefixExportPath := a .FLoader .FilePrefix + "\\ User\\ "
193+ _ , err := os .Stat (prefixExportPath )
139194 if err != nil {
140- os .Mkdir (". \\ User" , os .ModeDir )
195+ os .Mkdir (prefixExportPath , os .ModeDir )
141196 }
142197
143- expPath := ". \\ User \\ " + pInfo .AcountName
198+ expPath := prefixExportPath + pInfo .AcountName
144199 _ , err = os .Stat (expPath )
145200 if err == nil {
146201 if ! full {
@@ -177,7 +232,7 @@ func (a *App) ExportWeChatAllData(full bool, acountName string) {
177232 }()
178233}
179234
180- func (a * App ) createWechatDataProvider (resPath string ) error {
235+ func (a * App ) createWechatDataProvider (resPath string , prefix string ) error {
181236 if a .provider != nil && a .provider .SelfInfo != nil && filepath .Base (resPath ) == a .provider .SelfInfo .UserName {
182237 log .Println ("WechatDataProvider not need create:" , a .provider .SelfInfo .UserName )
183238 return nil
@@ -189,7 +244,7 @@ func (a *App) createWechatDataProvider(resPath string) error {
189244 log .Println ("createWechatDataProvider WechatWechatDataProviderClose" )
190245 }
191246
192- provider , err := wechat .CreateWechatDataProvider (resPath )
247+ provider , err := wechat .CreateWechatDataProvider (resPath , prefix )
193248 if err != nil {
194249 log .Println ("CreateWechatDataProvider failed:" , resPath )
195250 return err
@@ -202,29 +257,52 @@ func (a *App) createWechatDataProvider(resPath string) error {
202257}
203258
204259func (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 {
207269 infoJson , _ := json .Marshal (a .provider .SelfInfo )
208270 runtime .EventsEmit (a .ctx , "selfInfo" , string (infoJson ))
209271 }
210272}
211273
212274func (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} "
216278 }
217279 log .Printf ("pageIndex: %d\n " , pageIndex )
218280 list , err := a .provider .WeChatGetSessionList (pageIndex , pageSize )
219281 if err != nil {
220- return ""
282+ return "{ \" Total \" :0} "
221283 }
222284
223285 listStr , _ := json .Marshal (list )
224286 log .Println ("GetWechatSessionList:" , list .Total )
225287 return string (listStr )
226288}
227289
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+
228306func (a * App ) GetWechatMessageListByTime (userName string , time int64 , pageSize int , direction string ) string {
229307 log .Println ("GetWechatMessageList:" , userName , pageSize , time , direction )
230308 if len (userName ) == 0 {
@@ -284,6 +362,7 @@ func (a *App) GetWechatMessageDate(userName string) string {
284362func (a * App ) setCurrentConfig () {
285363 viper .Set (configDefaultUserKey , a .defaultUser )
286364 viper .Set (configUsersKey , a .users )
365+ viper .Set (configExportPathKey , a .FLoader .FilePrefix )
287366 err := viper .SafeWriteConfig ()
288367 if err != nil {
289368 log .Println (err )
@@ -314,7 +393,9 @@ func (a *App) OpenFileOrExplorer(filePath string, explorer bool) string {
314393 // filePath = root + filePath[1:]
315394 // }
316395 // log.Println("OpenFileOrExplorer:", filePath)
317- err := utils .OpenFileOrExplorer (filePath , explorer )
396+
397+ path := a .FLoader .FilePrefix + filePath
398+ err := utils .OpenFileOrExplorer (path , explorer )
318399 if err != nil {
319400 return "{\" result\" : \" OpenFileOrExplorer failed\" , \" status\" :\" failed\" }"
320401 }
@@ -349,13 +430,14 @@ func (a *App) GetWechatLocalAccountInfo() string {
349430 infos .Total = 0
350431 infos .CurrentAccount = a .defaultUser
351432 for i := range a .users {
352- resPath := ". \\ User\\ " + a .users [i ]
433+ resPath := a . FLoader . FilePrefix + " \\ User\\ " + a .users [i ]
353434 if _ , err := os .Stat (resPath ); err != nil {
354435 log .Println ("GetWechatLocalAccountInfo:" , resPath , err )
355436 continue
356437 }
357438
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 ])
359441 if err != nil {
360442 log .Println ("GetWechatLocalAccountInfo" , err )
361443 continue
@@ -386,3 +468,127 @@ func (a *App) WechatSwitchAccount(account string) bool {
386468
387469 return false
388470}
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