|
|
@ -5,7 +5,6 @@ import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
|
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"path" |
|
|
|
"path" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
@ -55,7 +54,7 @@ func FilesInFolder(dir, filename string) ([]string, error) { |
|
|
|
|
|
|
|
|
|
|
|
// ReadFile reads the file from the provided path
|
|
|
|
// ReadFile reads the file from the provided path
|
|
|
|
func ReadFile(filename string) (string, error) { |
|
|
|
func ReadFile(filename string) (string, error) { |
|
|
|
s, err := ioutil.ReadFile(filename) |
|
|
|
s, err := os.ReadFile(filename) |
|
|
|
return string(s), err |
|
|
|
return string(s), err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -71,20 +70,20 @@ func CopyDir(src, dst, skip string) error { |
|
|
|
// CopyDirHasSuffix copies the directory from the source to the destination
|
|
|
|
// 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
|
|
|
|
// contain is the file if you want to copy, and rename copied filename with dir/index_filename
|
|
|
|
func CopyDirHasSuffix(src, dst, suffix string) error { |
|
|
|
func CopyDirHasSuffix(src, dst, suffix string) error { |
|
|
|
var filelist []string |
|
|
|
var files []string |
|
|
|
err := filepath.Walk(src, func(path string, f os.FileInfo, err error) error { |
|
|
|
err := filepath.Walk(src, func(path string, f os.FileInfo, err error) error { |
|
|
|
if !f.IsDir() && strings.HasSuffix(strings.ToLower(f.Name()), suffix) { |
|
|
|
if !f.IsDir() && strings.HasSuffix(strings.ToLower(f.Name()), suffix) { |
|
|
|
filelist = append(filelist, path) |
|
|
|
files = append(files, path) |
|
|
|
} |
|
|
|
} |
|
|
|
return err |
|
|
|
return err |
|
|
|
}) |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
if err := os.MkdirAll(dst, 0o755); err != nil { |
|
|
|
if err := os.MkdirAll(dst, 0o700); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
for index, file := range filelist { |
|
|
|
for index, file := range files { |
|
|
|
// p = dir/index_file
|
|
|
|
// p = dir/index_file
|
|
|
|
p := fmt.Sprintf("%s/%d_%s", dst, index, BaseDir(file)) |
|
|
|
p := fmt.Sprintf("%s/%d_%s", dst, index, BaseDir(file)) |
|
|
|
err = CopyFile(file, p) |
|
|
|
err = CopyFile(file, p) |
|
|
@ -97,20 +96,19 @@ func CopyDirHasSuffix(src, dst, suffix string) error { |
|
|
|
|
|
|
|
|
|
|
|
// CopyFile copies the file from the source to the destination
|
|
|
|
// CopyFile copies the file from the source to the destination
|
|
|
|
func CopyFile(src, dst string) error { |
|
|
|
func CopyFile(src, dst string) error { |
|
|
|
// TODO: Handle read file error
|
|
|
|
s, err := os.ReadFile(src) |
|
|
|
d, err := ioutil.ReadFile(src) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
err = ioutil.WriteFile(dst, d, 0o777) |
|
|
|
err = os.WriteFile(dst, s, 0o600) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Filename returns the filename from the provided path
|
|
|
|
// ItemName returns the filename from the provided path
|
|
|
|
func Filename(browser, item, ext string) string { |
|
|
|
func ItemName(browser, item, ext string) string { |
|
|
|
replace := strings.NewReplacer(" ", "_", ".", "_", "-", "_") |
|
|
|
replace := strings.NewReplacer(" ", "_", ".", "_", "-", "_") |
|
|
|
return strings.ToLower(fmt.Sprintf("%s_%s.%s", replace.Replace(browser), item, ext)) |
|
|
|
return strings.ToLower(fmt.Sprintf("%s_%s.%s", replace.Replace(browser), item, ext)) |
|
|
|
} |
|
|
|
} |
|
|
@ -137,26 +135,27 @@ func ParentBaseDir(p string) string { |
|
|
|
|
|
|
|
|
|
|
|
// CompressDir compresses the directory into a zip file
|
|
|
|
// CompressDir compresses the directory into a zip file
|
|
|
|
func CompressDir(dir string) error { |
|
|
|
func CompressDir(dir string) error { |
|
|
|
files, err := ioutil.ReadDir(dir) |
|
|
|
files, err := os.ReadDir(dir) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
b := new(bytes.Buffer) |
|
|
|
b := new(bytes.Buffer) |
|
|
|
zw := zip.NewWriter(b) |
|
|
|
zw := zip.NewWriter(b) |
|
|
|
for _, f := range files { |
|
|
|
for _, f := range files { |
|
|
|
fw, _ := zw.Create(f.Name()) |
|
|
|
fw, err := zw.Create(f.Name()) |
|
|
|
fileName := path.Join(dir, f.Name()) |
|
|
|
if err != nil { |
|
|
|
fileContent, err := ioutil.ReadFile(fileName) |
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
name := path.Join(dir, f.Name()) |
|
|
|
|
|
|
|
content, err := os.ReadFile(name) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
zw.Close() |
|
|
|
|
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
_, err = fw.Write(fileContent) |
|
|
|
_, err = fw.Write(content) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
zw.Close() |
|
|
|
|
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
err = os.Remove(fileName) |
|
|
|
err = os.Remove(name) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -165,7 +164,7 @@ func CompressDir(dir string) error { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
filename := filepath.Join(dir, fmt.Sprintf("%s.zip", dir)) |
|
|
|
filename := filepath.Join(dir, fmt.Sprintf("%s.zip", dir)) |
|
|
|
outFile, err := os.Create(filename) |
|
|
|
outFile, err := os.Create(filepath.Clean(filename)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -173,5 +172,5 @@ func CompressDir(dir string) error { |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
return outFile.Close() |
|
|
|
} |
|
|
|
} |
|
|
|