feat: support extension for chromium

This commit is contained in:
ᴍᴏᴏɴD4ʀᴋ
2022-04-29 23:59:37 +08:00
parent d6cb101512
commit 26de1075ee
11 changed files with 139 additions and 29 deletions
+64
View File
@@ -3,6 +3,7 @@ package fileutil
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
@@ -37,12 +38,29 @@ func FolderExists(foldername string) bool {
return info.IsDir()
}
// FilesInFolder returns the files contains in the provided folder
func FilesInFolder(dir, filename string) ([]string, error) {
if !FolderExists(dir) {
return nil, errors.New(dir + " folder does not exist")
}
var files []string
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && strings.Contains(path, filename) {
files = append(files, path)
}
return err
})
return files, err
}
// ReadFile reads the file from the provided path
func ReadFile(filename string) (string, error) {
s, err := ioutil.ReadFile(filename)
return string(s), err
}
// CopyDir copies the directory from the source to the destination
// skip the file if you don't want to copy
func CopyDir(src, dst, skip string) error {
s := cp.Options{Skip: func(src string) (bool, error) {
return strings.Contains(strings.ToLower(src), skip), nil
@@ -50,23 +68,69 @@ func CopyDir(src, dst, skip string) error {
return cp.Copy(src, dst, s)
}
// CopyDirContains copies the directory from the source to the destination
// contain is the file if you want to copy
func CopyDirContains(src, dst, contain string) error {
var filelist []string
err := filepath.Walk(src, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && strings.Contains(strings.ToLower(f.Name()), contain) {
filelist = append(filelist, path)
}
return err
})
if err != nil {
return err
}
if err := os.MkdirAll(dst, 0755); err != nil {
return err
}
for index, file := range filelist {
// p = dir/index_file
p := fmt.Sprintf("%s/%d_%s", dst, index, BaseDir(file))
err = CopyFile(file, p)
if err != nil {
return err
}
}
return nil
}
// CopyFile copies the file from the source to the destination
func CopyFile(src, dst string) error {
// TODO: Handle read file error
d, err := ioutil.ReadFile(src)
if err != nil {
return err
}
err = ioutil.WriteFile(dst, d, 0777)
if err != nil {
return err
}
return nil
}
// Filename returns the filename from the provided path
func Filename(browser, item, ext string) string {
replace := strings.NewReplacer(" ", "_", ".", "_", "-", "_")
return strings.ToLower(fmt.Sprintf("%s_%s.%s", replace.Replace(browser), item, ext))
}
// ParentDir returns the parent directory of the provided path
func ParentDir(p string) string {
return filepath.Dir(filepath.Clean(p))
}
// BaseDir returns the base directory of the provided path
func BaseDir(p string) string {
return filepath.Base(p)
}
// ParentBaseDir returns the parent base directory of the provided path
func ParentBaseDir(p string) string {
return BaseDir(ParentDir(p))
}
// CompressDir compresses the directory into a zip file
func CompressDir(dir string) error {
files, err := ioutil.ReadDir(dir)
if err != nil {