fix: find chromium key failed on windows

This commit is contained in:
ᴍᴏᴏɴD4ʀᴋ
2022-04-20 01:45:36 +08:00
parent 2de62ac1bb
commit 8c2f72ccb6
6 changed files with 29 additions and 35 deletions
+16 -17
View File
@@ -2,6 +2,7 @@ package browser
import (
"os"
"path/filepath"
"strings"
"hack-browser-data/internal/browingdata"
@@ -42,16 +43,15 @@ func pickChromium(name, profile string) []Browser {
// TODO: add support for 「all」 flag and set profilePath
if name == "all" {
for _, v := range chromiumList {
if !fileutil.FolderExists(filepath.Clean(v.profilePath)) {
log.Noticef("find browser %s failed, profile folder is not exist", v.name)
continue
}
if b, err := chromium.New(v.name, v.storage, v.profilePath, v.items); err == nil {
log.Noticef("find browser %s success", b.Name())
browsers = append(browsers, b)
} else {
if err == chromium.ErrProfilePathNotFound {
log.Errorf("find browser %s failed, profile folder is not exist, maybe not installed", v.name)
continue
} else {
log.Errorf("new chromium error: %s", err.Error())
}
log.Errorf("new chromium error: %s", err.Error())
}
}
}
@@ -59,15 +59,14 @@ func pickChromium(name, profile string) []Browser {
if profile == "" {
profile = c.profilePath
}
if !fileutil.FolderExists(filepath.Clean(profile)) {
log.Fatalf("find browser %s failed, profile folder is not exist", c.name)
}
b, err := chromium.New(c.name, c.storage, profile, c.items)
if err != nil {
if err == chromium.ErrProfilePathNotFound {
log.Fatalf("find browser %s failed, profile folder is not exist, maybe not installed", c.name)
} else {
log.Fatalf("new chromium error:", err)
return nil
}
log.Fatalf("new chromium error:", err)
}
log.Noticef("find browser %s success", b.Name())
browsers = append(browsers, b)
}
return browsers
@@ -83,17 +82,17 @@ func pickFirefox(name, profile string) []Browser {
} else {
profile = fileutil.ParentDir(profile)
}
if !fileutil.FolderExists(filepath.Clean(profile)) {
log.Noticef("find browser firefox %s failed, profile folder is not exist", v.name)
continue
}
if multiFirefox, err := firefox.New(v.name, v.storage, profile, v.items); err == nil {
for _, b := range multiFirefox {
log.Noticef("find browser firefox %s success", b.Name())
browsers = append(browsers, b)
}
} else {
if err == firefox.ErrProfilePathNotFound {
log.Errorf("find browser firefox %s failed, profile folder is not exist", v.name)
} else {
log.Error(err)
}
log.Error(err)
}
}
return browsers
+4 -10
View File
@@ -1,7 +1,6 @@
package chromium
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
@@ -9,6 +8,7 @@ import (
"hack-browser-data/internal/browingdata"
"hack-browser-data/internal/item"
"hack-browser-data/internal/log"
"hack-browser-data/internal/utils/fileutil"
"hack-browser-data/internal/utils/typeutil"
)
@@ -22,20 +22,12 @@ type chromium struct {
itemPaths map[item.Item]string
}
var (
ErrProfilePathNotFound = errors.New("profile path not found")
)
// New create instance of chromium browser, fill item's path if item is existed.
func New(name, storage, profilePath string, items []item.Item) (*chromium, error) {
c := &chromium{
name: name,
storage: storage,
}
// TODO: Handle file path is not exist
if !fileutil.FolderExists(profilePath) {
return nil, ErrProfilePathNotFound
}
itemsPaths, err := c.getItemPath(profilePath, items)
if err != nil {
return nil, err
@@ -72,7 +64,7 @@ func (c *chromium) BrowsingData() (*browingdata.Data, error) {
func (c *chromium) copyItemToLocal() error {
for i, path := range c.itemPaths {
if fileutil.FolderExists(path) {
if err := fileutil.CopyDir(path, i.String()); err != nil {
if err := fileutil.CopyDir(path, i.String(), "lock"); err != nil {
return err
}
} else {
@@ -95,6 +87,8 @@ func (c *chromium) getItemPath(profilePath string, items []item.Item) (map[item.
var itemPaths = make(map[item.Item]string)
parentDir := fileutil.ParentDir(profilePath)
baseDir := fileutil.BaseDir(profilePath)
log.Infof("%s profile parentDir: %s", c.name, parentDir)
log.Infof("%s profile baseDir: %s", c.name, baseDir)
err := filepath.Walk(parentDir, chromiumWalkFunc(items, itemPaths, baseDir))
if err != nil {
return itemPaths, err
+1 -3
View File
@@ -28,9 +28,7 @@ var (
// New returns a new firefox instance.
func New(name, storage, profilePath string, items []item.Item) ([]*firefox, error) {
if !fileutil.FolderExists(profilePath) {
return nil, ErrProfilePathNotFound
}
f := &firefox{
name: name,
storage: storage,