feat: extension support (#78)

This commit is contained in:
Levi authored and GitHub committed 2023-10-16 09:47:31 +08:00
1 parent 82d316af83
commit c073fc0a67
118 files changed
+6971 -780

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
package util
import (
"net/url"
"regexp"
"strings"
)
// Match url with pattern by chrome extension match pattern style
// https://developer.chrome.com/docs/extensions/mv3/match_patterns/
func Match(pattern string, u string) bool {
scheme, host, path := parsePattern(pattern)
url, err := url.Parse(u)
if err != nil {
return false
}
if scheme != "*" && scheme != url.Scheme {
return false
}
if !matchHost(host, url.Hostname()) {
return false
}
if !matchPath(path, url.Path) {
return false
}
return true
}
func parsePattern(pattern string) (scheme string, host string, path string) {
parts := strings.Split(pattern, "://")
if len(parts) == 2 {
scheme = parts[0]
pattern = parts[1]
} else {
scheme = ""
}
parts = strings.SplitN(pattern, "/", 2)
if len(parts) == 2 {
host = parts[0]
path = "/" + parts[1]
} else {
host = pattern
path = "/"
}
return
}
func matchHost(pattern string, host string) bool {
if pattern == "*" {
return true
}
if strings.HasPrefix(pattern, "*.") {
return strings.HasSuffix(host, pattern[1:])
}
return pattern == host
}
func matchPath(pattern string, path string) bool {
if pattern == "*" {
return true
}
if !strings.HasSuffix(pattern, "*") && !strings.HasSuffix(pattern, "/") {
pattern += "/"
}
if !strings.HasSuffix(path, "/") {
path += "/"
}
if strings.Contains(pattern, "*") {
pattern = strings.Replace(pattern, "*", ".*", -1)
matched, _ := regexp.MatchString("^"+pattern+"$", path)
return matched
}
return pattern == path
}
+36
View File
@@ -0,0 +1,36 @@
package util
import "testing"
func TestMatch(t *testing.T) {
tests := []struct {
pattern string
urls []string
want bool
}{
{"*://*/*", []string{"https://www.google.com/", "http://example.org/foo/bar.html"}, true},
{"https://*/*", []string{"https://www.google.com", "https://example.org/foo/bar.html"}, true},
{"*://www.google.com", []string{"https://www.google.com/", "https://www.google.com"}, true},
{"*://*.google.com/", []string{"https://a.www.google.com/", "https://c.www.google.com/", "https://www.google.com/"}, true},
{"https://*/foo*", []string{"https://www.google.com/foo", "https://example.com/foo/bar.html"}, true},
{"https://www.google.com/*/b/*", []string{"https://www.google.com/a/b", "https://www.google.com/a/b/c"}, true},
{"https://*.google.com/foo*bar", []string{"https://www.google.com/foo/baz/bar", "https://docs.google.com/foobar"}, true},
{"https://www.google.com/*abc*", []string{"https://www.google.com/abc", "https://www.google.com/123abc", "https://www.google.com/abc456", "https://www.google.com/123abc456"}, true},
{"https://example.org/foo/bar.html", []string{"https://example.org/foo/bar.html"}, true},
{"http://127.0.0.1/*", []string{"http://127.0.0.1/", "http://127.0.0.1/foo/bar.html"}, true},
{"*://mail.google.com/*", []string{"http://mail.google.com/foo/baz/bar", "https://mail.google.com/foobar"}, true},
{"https://www.google.com/", []string{"http://www.google.com/"}, false},
{"www.google.com/", []string{"http://www.google.com/", "https://www.google.com/"}, false},
{"www.google.com/*c", []string{"https://www.google.com/a", "https://www.google.com/b"}, false},
{"https://*.example.org/*", []string{"https://www.google.com", "https://docs.google.com"}, false},
}
for _, tt := range tests {
t.Run(tt.pattern, func(t *testing.T) {
for _, url := range tt.urls {
if got := Match(tt.pattern, url); got != tt.want {
t.Errorf("Match() = %v, want %v", got, tt.want)
}
}
})
}
}
+99 -14
View File
@@ -3,8 +3,10 @@ package util
import (
"errors"
"fmt"
"io"
"os"
syspath "path"
"path/filepath"
"strings"
)
@@ -64,22 +66,105 @@ func CheckDuplicateAndRename(path string) (string, error) {
}
}
// GetSingleDir get the top level single folder name,if not exist, return empty string
func GetSingleDir(paths []string) string {
if len(paths) == 0 {
return ""
// CopyDir Copy all files to the target directory, if the file already exists, it will be overwritten.
// Remove target file if the source file is not exist.
func CopyDir(source string, target string, excludeDir ...string) error {
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
split := strings.Split(paths[0], "/")
if len(split) == 0 || split[0] == "" {
return ""
}
dir := split[0]
for i := 1; i < len(paths); i++ {
if !strings.HasPrefix(paths[i], dir) {
return ""
if err := filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if len(excludeDir) > 0 {
for _, dir := range excludeDir {
if info.IsDir() && info.Name() == dir {
return filepath.SkipDir
}
}
}
return nil
}
relPath, err := filepath.Rel(source, path)
if err != nil {
return err
}
targetPath := filepath.Join(target, relPath)
if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil {
return err
}
if err := copyForce(path, targetPath); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return dir
if err := filepath.Walk(target, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if len(excludeDir) > 0 {
for _, dir := range excludeDir {
if info.IsDir() && info.Name() == dir {
return filepath.SkipDir
}
}
}
return nil
}
relPath, err := filepath.Rel(target, path)
if err != nil {
return err
}
targetPath := filepath.Join(target, relPath)
sourcePath := filepath.Join(source, relPath)
// if source file is not exist, remove target file
if _, err := os.Stat(sourcePath); os.IsNotExist(err) {
if err := SafeRemove(targetPath); err != nil {
return err
}
}
return nil
}); err != nil {
return err
}
return nil
}
// RmAndMkDirAll remove and create directory, if the directory already exists, it will be overwritten.
func RmAndMkDirAll(path string) error {
if err := os.RemoveAll(path); err != nil {
return err
}
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
return nil
}
// copy file, if the target file already exists, it will be overwritten.
func copyForce(source string, target string) error {
sourceFile, err := os.Open(source)
if err != nil {
return err
}
defer sourceFile.Close()
targetFile, err := os.Create(target)
if err != nil {
return err
}
defer targetFile.Close()
_, err = io.Copy(targetFile, sourceFile)
if err != nil {
return err
}
return nil
}
func CreateDirIfNotExist(dir string) error {
@@ -92,7 +177,7 @@ func CreateDirIfNotExist(dir string) error {
// IsExistsFile check file exists and is a file
func IsExistsFile(path string) bool {
info, err := os.Stat(path)
// 判断路径是否存在,并且是一个文件
// if file exists and is a file
if err == nil && !info.IsDir() {
return true
}
+1 -62
View File
@@ -78,7 +78,7 @@ func TestFilepath(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Filepath(tt.args.path, tt.args.originName, tt.args.customName); got != tt.want {
t.Errorf("Filepath() = %v, want %v", got, tt.want)
t.Errorf("SingleFilepath() = %v, want %v", got, tt.want)
}
})
}
@@ -105,67 +105,6 @@ func TestSafeRemove(t *testing.T) {
}
}
func TestGetSingleDir(t *testing.T) {
type args struct {
paths []string
}
tests := []struct {
name string
args args
want string
}{
{
name: "empty",
args: args{
paths: []string{},
},
want: "",
},
{
name: "blank",
args: args{
paths: []string{""},
},
want: "",
},
{
name: "blank multi",
args: args{
paths: []string{"", "a"},
},
want: "",
},
{
name: "multi dir not single",
args: args{
paths: []string{"a", "b"},
},
want: "",
},
{
name: "single dir",
args: args{
paths: []string{"a"},
},
want: "a",
},
{
name: "multi dir single",
args: args{
paths: []string{"a/b", "a/c"},
},
want: "a",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetSingleDir(tt.args.paths); got != tt.want {
t.Errorf("IsSingleDir() = %v, want %v", got, tt.want)
}
})
}
}
func TestCheckDuplicateAndRename(t *testing.T) {
doCheckDuplicateAndRename(t, []string{}, "a.txt", "a.txt")
doCheckDuplicateAndRename(t, []string{"a.txt"}, "a.txt", "a (1).txt")