forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxfs.go
261 lines (228 loc) · 5.87 KB
/
xfs.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package xfs
import (
"archive/tar"
"archive/zip"
"bytes"
"encoding/base64"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/spf13/afero"
"github.com/spf13/afero/tarfs"
"github.com/spf13/afero/zipfs"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/xiang/config"
)
// Stor 文件系统实例
var Stor Xfs
// Xfs 文件系统
type Xfs struct {
Root string // Root
afero.Afero
}
// Fs 文件系统 Interface
type Fs interface {
afero.Fs
}
// File file fs
type File interface {
afero.File
}
func init() {
Stor = New(config.Conf.Storage.Path)
}
// New 创建文件系统
func New(path string) Xfs {
pinfo := strings.Split(path, "://")
fs := "fs"
root := path
if len(pinfo) == 2 {
fs = pinfo[0]
root = strings.TrimPrefix(path, fmt.Sprintf("%s://", fs))
}
xfs := Xfs{
Root: root,
}
switch fs {
case "fs", "file":
xfs.Fs = afero.NewBasePathFs(afero.NewOsFs(), root)
break
case "mem", "memory":
xfs.Fs = afero.NewMemMapFs()
break
default:
exception.New("尚未支持 %s 文件系统", 500, fs).Throw()
}
// exists := xfs.MustDirExists("/")
// if !exists && root != "/" {
// xfs.MustMkdirAll("/", os.ModePerm)
// }
return xfs
}
// NewZip 创建Zip文件系统
func NewZip(zipfile string) Xfs {
r, err := zip.OpenReader(zipfile)
if err != nil {
exception.New("ZIP文件打开失败 %s ", 500, err).Throw()
}
return Xfs{
Root: "/",
Afero: afero.Afero{
Fs: zipfs.New(&r.Reader),
},
}
}
// NewTar tar文件系统
func NewTar(tarfile string) Xfs {
var buf bytes.Buffer
r := tar.NewReader(&buf)
return Xfs{
Root: "/",
Afero: afero.Afero{
Fs: tarfs.New(r),
},
}
}
// Encode Base64编码
func Encode(content []byte) string {
return base64.StdEncoding.EncodeToString(content)
}
// Decode Base64编码
func Decode(content []byte) []byte {
var data []byte
_, err := base64.StdEncoding.Decode(data, content)
if err != nil {
exception.New("文件解码失败 %s ", 500, err).Throw()
}
return data
}
// DecodeString Base64编码
func DecodeString(content string) string {
dst, err := base64.StdEncoding.DecodeString(content)
if err != nil {
exception.New("文件解码失败 %s ", 500, err).Throw()
}
return string(dst)
}
// MustOpen 打开文件
func (xfs *Xfs) MustOpen(filename string) File {
file, err := xfs.Open(filename)
if err != nil {
exception.New("打开文件失败 %s 失败 ", 500, filename).Throw()
}
return file
}
// MustReadFile 阅读文件内容
func (xfs *Xfs) MustReadFile(filename string) []byte {
bytes, err := xfs.ReadFile(filename)
if err != nil {
exception.New("阅读文件内容失败 %s 失败 %s ", 500, filename, err).Throw()
}
return bytes
}
// MustReadDir 阅读文件夹内容
func (xfs *Xfs) MustReadDir(dirname string) []os.FileInfo {
fileinfos, err := xfs.ReadDir(dirname)
if err != nil {
exception.New("阅读文件夹失败 %s 失败 ", 500, dirname).Throw()
}
return fileinfos
}
// MustExists 检查文件是否存在
func (xfs *Xfs) MustExists(filename string) bool {
has, err := xfs.Exists(filename)
if err != nil {
exception.New("检查文件失败 %s 失败 ", 500, filename).Throw()
}
return has
}
// MustDirExists 检查文件夹是否存在
func (xfs *Xfs) MustDirExists(dirname string) bool {
res, err := xfs.DirExists(dirname)
if err != nil {
exception.New("检查文件夹 %s 失败 ", 500, dirname).Throw()
}
return res
}
// MustMkdirAll 创建文件夹及子文件夹
func (xfs *Xfs) MustMkdirAll(dirname string, pterm fs.FileMode) {
err := xfs.MkdirAll(dirname, pterm)
if err != nil {
exception.New("创建文件夹及子文件 %s 失败 ", 500, dirname).Throw()
}
}
// MustMkdir 创建文件夹
func (xfs *Xfs) MustMkdir(dirname string, pterm fs.FileMode) {
err := xfs.Mkdir(dirname, pterm)
if err != nil {
exception.New("创建文件夹 %s 失败 ", 500, dirname).Throw()
}
}
// MustIsDir 是否为文件夹
func (xfs *Xfs) MustIsDir(dirname string) bool {
isdir, err := xfs.IsDir(dirname)
if err != nil {
exception.New("创建文件夹 %s 失败 ", 500, dirname).Throw()
}
return isdir
}
// MustIsEmpty 文件是否为空
func (xfs *Xfs) MustIsEmpty(filename string) bool {
isEmpty, err := xfs.IsEmpty(filename)
if err != nil {
exception.New("检查文件是否为空 %s 失败 ", 500, filename).Throw()
}
return isEmpty
}
// GetTempDir 临时目录
func (xfs *Xfs) GetTempDir(subPath string) string {
return afero.GetTempDir(xfs.Fs, subPath)
}
//MustTempDir 创建临时文件夹
func (xfs *Xfs) MustTempDir(dirname string, prefix string) string {
name, err := xfs.TempDir(dirname, prefix)
if err != nil {
exception.New("创建文件夹 %s %s 失败 ", 500, dirname, prefix).Throw()
}
return name
}
// MustTempFile 临时文件
func (xfs *Xfs) MustTempFile(dirname, prefix string) File {
f, err := xfs.TempFile(dirname, prefix)
if err != nil {
exception.New("创建临时文件 %s %s 失败 ", 500, dirname, prefix).Throw()
}
return f
}
// MustWalk 文件夹遍历
func (xfs *Xfs) MustWalk(root string, walkFn filepath.WalkFunc) {
err := xfs.Walk(root, walkFn)
if err != nil {
exception.New("遍历文件夹 %s 失败 ", 500, root).Throw()
}
}
// MustWriteFile 写入文件
func (xfs *Xfs) MustWriteFile(filename string, data []byte, perm os.FileMode) {
err := xfs.WriteFile(filename, data, perm)
if err != nil {
exception.New("写入文件 %s 失败 ", 500, filename).Throw()
}
}
// MustFileContainsBytes 文件是否包含内容
func (xfs *Xfs) MustFileContainsBytes(filename string, subslice []byte) bool {
has, err := xfs.FileContainsBytes(filename, subslice)
if err != nil {
exception.New("检查文件是否包含内容 %s 失败 ", 500, filename).Throw()
}
return has
}
// MustSafeWriteReader 检查文件能否写入
func (xfs *Xfs) MustSafeWriteReader(filename string, r io.Reader) {
err := xfs.SafeWriteReader(filename, r)
if err != nil {
exception.New("检查文件是否可以写入 Reader %s 失败 ", 500, filename).Throw()
}
}