11package chromium
22
33import (
4- "os "
4+ "io/fs "
55 "path/filepath"
66 "strings"
77
@@ -21,19 +21,27 @@ type chromium struct {
2121}
2222
2323// New create instance of chromium browser, fill item's path if item is existed.
24- func New (name , storage , profilePath string , items []item.Item ) (* chromium , error ) {
24+ func New (name , storage , profilePath string , items []item.Item ) ([] * chromium , error ) {
2525 c := & chromium {
26- name : name ,
27- storage : storage ,
26+ name : name ,
27+ storage : storage ,
28+ profilePath : profilePath ,
29+ items : items ,
2830 }
29- itemsPaths , err := c .getItemPath ( profilePath , items )
31+ multiItemPaths , err := c .getMultiItemPath ( c . profilePath , c . items )
3032 if err != nil {
3133 return nil , err
3234 }
33- c .profilePath = profilePath
34- c .itemPaths = itemsPaths
35- c .items = typeutil .Keys (itemsPaths )
36- return c , err
35+ var chromiumList []* chromium
36+ for user , itemPaths := range multiItemPaths {
37+ chromiumList = append (chromiumList , & chromium {
38+ name : fileutil .BrowserName (name , user ),
39+ items : typeutil .Keys (itemPaths ),
40+ itemPaths : itemPaths ,
41+ storage : storage ,
42+ })
43+ }
44+ return chromiumList , nil
3745}
3846
3947func (c * chromium ) Name () string {
@@ -81,28 +89,49 @@ func (c *chromium) copyItemToLocal() error {
8189 return nil
8290}
8391
84- func (c * chromium ) getItemPath (profilePath string , items []item.Item ) (map [item.Item ]string , error ) {
85- var itemPaths = make (map [item.Item ]string )
92+ func (c * chromium ) getMultiItemPath (profilePath string , items []item.Item ) (map [string ]map [item.Item ]string , error ) {
93+ // multiItemPaths is a map of user to item path, map[profile 1][item's name & path key pair]
94+ multiItemPaths := make (map [string ]map [item.Item ]string )
8695 parentDir := fileutil .ParentDir (profilePath )
87- baseDir := fileutil .BaseDir (profilePath )
88- err := filepath .Walk (parentDir , chromiumWalkFunc (items , itemPaths , baseDir ))
96+ err := filepath .Walk (parentDir , chromiumWalkFunc (items , multiItemPaths ))
8997 if err != nil {
90- return itemPaths , err
98+ return nil , err
99+ }
100+ var keyPath string
101+ var dir string
102+ for userDir , v := range multiItemPaths {
103+ for _ , p := range v {
104+ if strings .HasSuffix (p , item .ChromiumKey .FileName ()) {
105+ keyPath = p
106+ dir = userDir
107+ break
108+ }
109+ }
110+ }
111+ t := make (map [string ]map [item.Item ]string )
112+ for userDir , v := range multiItemPaths {
113+ if userDir == dir {
114+ continue
115+ }
116+ t [userDir ] = v
117+ t [userDir ][item.ChromiumKey ] = keyPath
118+ fillLocalStoragePath (t [userDir ], item .ChromiumLocalStorage )
91119 }
92- fillLocalStoragePath (itemPaths , item .ChromiumLocalStorage )
93- return itemPaths , nil
120+ return t , nil
94121}
95122
96- func chromiumWalkFunc (items []item.Item , itemPaths map [item.Item ]string , baseDir string ) filepath.WalkFunc {
97- return func (path string , info os .FileInfo , err error ) error {
98- for _ , it := range items {
99- switch {
100- case it . FileName () == info . Name ():
101- if it == item . ChromiumKey {
102- itemPaths [ it ] = path
123+ func chromiumWalkFunc (items []item.Item , multiItemPaths map [string ] map [ item.Item ]string ) filepath.WalkFunc {
124+ return func (path string , info fs .FileInfo , err error ) error {
125+ for _ , v := range items {
126+ if info . Name () == v . FileName () {
127+ parentBaseDir := fileutil . ParentBaseDir ( path )
128+ if parentBaseDir == "System Profile" {
129+ continue
103130 }
104- if strings .Contains (path , baseDir ) {
105- itemPaths [it ] = path
131+ if _ , exist := multiItemPaths [parentBaseDir ]; exist {
132+ multiItemPaths [parentBaseDir ][v ] = path
133+ } else {
134+ multiItemPaths [parentBaseDir ] = map [item.Item ]string {v : path }
106135 }
107136 }
108137 }
0 commit comments