You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
4.1 KiB
177 lines
4.1 KiB
3 years ago
|
package fileutil
|
||
|
|
||
|
import (
|
||
3 years ago
|
"archive/zip"
|
||
|
"bytes"
|
||
3 years ago
|
"errors"
|
||
3 years ago
|
"fmt"
|
||
3 years ago
|
"os"
|
||
3 years ago
|
"path"
|
||
|
"path/filepath"
|
||
3 years ago
|
"strings"
|
||
3 years ago
|
|
||
3 years ago
|
cp "github.com/otiai10/copy"
|
||
3 years ago
|
)
|
||
|
|
||
|
// FileExists checks if the file exists in the provided path
|
||
|
func FileExists(filename string) bool {
|
||
|
info, err := os.Stat(filename)
|
||
|
if os.IsNotExist(err) {
|
||
|
return false
|
||
|
}
|
||
|
if err != nil {
|
||
|
return false
|
||
|
}
|
||
|
return !info.IsDir()
|
||
|
}
|
||
|
|
||
|
// FolderExists checks if the folder exists
|
||
|
func FolderExists(foldername string) bool {
|
||
|
info, err := os.Stat(foldername)
|
||
|
if os.IsNotExist(err) {
|
||
|
return false
|
||
|
}
|
||
|
if err != nil {
|
||
|
return false
|
||
|
}
|
||
|
return info.IsDir()
|
||
|
}
|
||
|
|
||
3 years ago
|
// FilesInFolder returns the filepath contains in the provided folder
|
||
3 years ago
|
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 {
|
||
3 years ago
|
if !f.IsDir() && strings.HasSuffix(path, filename) {
|
||
3 years ago
|
files = append(files, path)
|
||
|
}
|
||
|
return err
|
||
|
})
|
||
|
return files, err
|
||
|
}
|
||
|
|
||
3 years ago
|
// ReadFile reads the file from the provided path
|
||
|
func ReadFile(filename string) (string, error) {
|
||
2 years ago
|
s, err := os.ReadFile(filename)
|
||
3 years ago
|
return string(s), err
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
// CopyDir copies the directory from the source to the destination
|
||
|
// skip the file if you don't want to copy
|
||
3 years ago
|
func CopyDir(src, dst, skip string) error {
|
||
2 years ago
|
s := cp.Options{Skip: func(info os.FileInfo, src, dst string) (bool, error) {
|
||
3 years ago
|
return strings.HasSuffix(strings.ToLower(src), skip), nil
|
||
3 years ago
|
}}
|
||
|
return cp.Copy(src, dst, s)
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
// CopyDirHasSuffix copies the directory from the source to the destination
|
||
|
// contain is the file if you want to copy, and rename copied filename with dir/index_filename
|
||
|
func CopyDirHasSuffix(src, dst, suffix string) error {
|
||
2 years ago
|
var files []string
|
||
3 years ago
|
err := filepath.Walk(src, func(path string, f os.FileInfo, err error) error {
|
||
3 years ago
|
if !f.IsDir() && strings.HasSuffix(strings.ToLower(f.Name()), suffix) {
|
||
2 years ago
|
files = append(files, path)
|
||
3 years ago
|
}
|
||
|
return err
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
if err := os.MkdirAll(dst, 0o700); err != nil {
|
||
3 years ago
|
return err
|
||
|
}
|
||
2 years ago
|
for index, file := range files {
|
||
3 years ago
|
// 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 {
|
||
2 years ago
|
s, err := os.ReadFile(src)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
err = os.WriteFile(dst, s, 0o600)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
// ItemName returns the filename from the provided path
|
||
|
func ItemName(browser, item, ext string) string {
|
||
3 years ago
|
replace := strings.NewReplacer(" ", "_", ".", "_", "-", "_")
|
||
|
return strings.ToLower(fmt.Sprintf("%s_%s.%s", replace.Replace(browser), item, ext))
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
func BrowserName(browser, user string) string {
|
||
|
replace := strings.NewReplacer(" ", "_", ".", "_", "-", "_", "Profile", "User")
|
||
|
return strings.ToLower(fmt.Sprintf("%s_%s", replace.Replace(browser), replace.Replace(user)))
|
||
|
}
|
||
|
|
||
3 years ago
|
// ParentDir returns the parent directory of the provided path
|
||
3 years ago
|
func ParentDir(p string) string {
|
||
3 years ago
|
return filepath.Dir(filepath.Clean(p))
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// BaseDir returns the base directory of the provided path
|
||
3 years ago
|
func BaseDir(p string) string {
|
||
|
return filepath.Base(p)
|
||
|
}
|
||
|
|
||
3 years ago
|
// ParentBaseDir returns the parent base directory of the provided path
|
||
3 years ago
|
func ParentBaseDir(p string) string {
|
||
|
return BaseDir(ParentDir(p))
|
||
|
}
|
||
|
|
||
3 years ago
|
// CompressDir compresses the directory into a zip file
|
||
3 years ago
|
func CompressDir(dir string) error {
|
||
2 years ago
|
files, err := os.ReadDir(dir)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return err
|
||
3 years ago
|
}
|
||
3 years ago
|
b := new(bytes.Buffer)
|
||
3 years ago
|
zw := zip.NewWriter(b)
|
||
|
for _, f := range files {
|
||
2 years ago
|
fw, err := zw.Create(f.Name())
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
name := path.Join(dir, f.Name())
|
||
|
content, err := os.ReadFile(name)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
_, err = fw.Write(content)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
err = os.Remove(name)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return err
|
||
3 years ago
|
}
|
||
|
}
|
||
|
if err := zw.Close(); err != nil {
|
||
|
return err
|
||
|
}
|
||
3 years ago
|
filename := filepath.Join(dir, fmt.Sprintf("%s.zip", dir))
|
||
2 years ago
|
outFile, err := os.Create(filepath.Clean(filename))
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
_, err = b.WriteTo(outFile)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
return outFile.Close()
|
||
3 years ago
|
}
|