-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilex.go
145 lines (125 loc) · 2.68 KB
/
filex.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
package filex
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"io"
"log"
"mime/multipart"
"os"
"path/filepath"
)
// FileX is a interface
type FileX interface {
CreateMultipart(path string, filename string, file *multipart.FileHeader) (string, error)
CreateImage(imgByte []byte, path string) (string, error)
CreateFile(path string, fileName string, data string) (string, error)
Delete(path string) (string, error)
DeleteDir(path string) error
Mkdir(dirName string) bool
Read(path string) []byte
}
type fileX struct {
}
func (f *fileX) Mkdir(dirName string) bool {
src, err := os.Stat(dirName)
if os.IsNotExist(err) {
errDir := os.MkdirAll(dirName, 0755)
if errDir != nil {
log.Println(errDir)
return false
}
return true
}
if src.Mode().IsRegular() {
log.Println(dirName, "Already exist.")
return false
}
return false
}
func (f *fileX) CreateMultipart(path string, filename string, file *multipart.FileHeader) (string, error) {
src, err := file.Open()
if err != nil {
return "", err
}
defer func(src multipart.File) {
_ = src.Close()
}(src)
ext := filepath.Ext(file.Filename)
filePath := fmt.Sprintf("%s%s", filename, ext)
f.Mkdir(path)
pathFile := fmt.Sprintf("%s/%s", path, filePath)
dst, err := os.Create(pathFile)
if err != nil {
return "", err
}
defer func(dst *os.File) {
_ = dst.Close()
}(dst)
if _, err = io.Copy(dst, src); err != nil {
return "", err
}
return pathFile, err
}
func (f *fileX) Delete(path string) (string, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return "", err
}
if err = os.Remove(path); err != nil {
return "", err
}
return path, nil
}
func (f *fileX) DeleteDir(path string) error {
return os.RemoveAll(path)
}
func (f *fileX) CreateImage(imgByte []byte, path string) (string, error) {
img, _, err := image.Decode(bytes.NewReader(imgByte))
if err != nil {
log.Println(err)
return "", err
}
out, _ := os.Create(path)
defer func(out *os.File) {
_ = out.Close()
}(out)
var opts jpeg.Options
opts.Quality = 100
err = jpeg.Encode(out, img, &opts)
if err != nil {
log.Println(err)
return "", err
}
return "", nil
}
func (f *fileX) CreateFile(path string, fileName string, data string) (string, error) {
f.Mkdir(path)
pathFile := fmt.Sprintf("%s/%s", path, fileName)
file, err := os.Create(pathFile)
if err != nil {
fmt.Println(err)
}
_, err = file.WriteString(data)
if err != nil {
_ = file.Close()
return "", err
}
err = file.Close()
if err != nil {
return "", err
}
return pathFile, err
}
func (f *fileX) Read(path string) []byte {
data, err := os.ReadFile(path)
if err != nil {
return []byte{}
}
return data
}
// New a instance
func New() FileX {
return &fileX{}
}