-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
165 lines (152 loc) · 3.66 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package lib
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
cp "github.com/otiai10/copy"
)
// 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()
}
// FilesInFolder returns the filepath 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.HasSuffix(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 := os.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(info os.FileInfo, src, dst string) (bool, error) {
return strings.HasSuffix(strings.ToLower(src), skip), nil
}}
return cp.Copy(src, dst, s)
}
// 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 {
var files []string
err := filepath.Walk(src, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && strings.HasSuffix(strings.ToLower(f.Name()), suffix) {
files = append(files, path)
}
return err
})
if err != nil {
return err
}
if err := os.MkdirAll(dst, 0o700); err != nil {
return err
}
for index, file := range files {
// 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 {
s, err := os.ReadFile(src)
if err != nil {
return err
}
err = os.WriteFile(dst, s, 0o600)
if err != nil {
return err
}
return nil
}
// 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 := os.ReadDir(dir)
if err != nil {
return err
}
b := new(bytes.Buffer)
zw := zip.NewWriter(b)
for _, f := range files {
fw, err := zw.Create(f.Name())
if err != nil {
return err
}
name := path.Join(dir, f.Name())
content, err := os.ReadFile(name)
if err != nil {
return err
}
_, err = fw.Write(content)
if err != nil {
return err
}
err = os.Remove(name)
if err != nil {
return err
}
}
if err := zw.Close(); err != nil {
return err
}
filename := filepath.Join(dir, fmt.Sprintf("%s.zip", dir))
outFile, err := os.Create(filepath.Clean(filename))
if err != nil {
return err
}
_, err = b.WriteTo(outFile)
if err != nil {
return err
}
return outFile.Close()
}