-
Notifications
You must be signed in to change notification settings - Fork 0
/
flysystem.go
190 lines (163 loc) · 4.68 KB
/
flysystem.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
package flysystem
import (
"fmt"
"io"
"os"
"sync"
)
const (
DiskNameOSS = "oss"
DiskNameLocal = "local"
PathTypeFile = "file"
PathTypeDirectory = "directory"
ModePublicString = "public"
ModePrivateString = "private"
ModeFilePublic = 0644
ModeFilePrivate = 0600
ModeDirPublic = 0755
ModeDirPrivate = 0700
)
var (
FileModes = map[string]map[string]os.FileMode{
PathTypeFile: {
ModePublicString: ModeFilePublic,
ModePrivateString: ModeFilePrivate,
},
PathTypeDirectory: {
ModePublicString: ModeDirPublic,
ModePrivateString: ModeDirPrivate,
},
}
)
type IBFS interface {
// Exists Determine if the file exists
Exists(path string) (bool, error)
// WriteReader write file content and return full path
WriteReader(path string, reader io.Reader) (string, error)
// Write file content and return full path
Write(path string, contents []byte) (string, error)
// WriteStream Resource file write returns full path
WriteStream(path, resource string) (string, error)
// Read Read file
Read(path string) ([]byte, error)
// Delete Deleting files returns the number of deleted files
Delete(path string) (int64, error)
}
type IFS interface {
IBFS
// Size Get File Size
Size(path string) (int64, error)
// Update the file content and return the updated full path
Update(path string, contents []byte) (string, error)
// UpdateStream Return the updated full path based on resource file updates
UpdateStream(path, resource string) (string, error)
// DeleteDirectory Number of files deleted from the deleted directory
DeleteDirectory(dirname string) (int64, error)
// CreateDirectory create directory
CreateDirectory(dirname string) error
// MimeType Get File MimeType
MimeType(path string) (string, error)
// Move move file
Move(source, destination string) (bool, error)
// Copy copy file
Copy(source, destination string) (bool, error)
}
type IFlysystem interface {
IFS
Extend(adapter IAdapter, names ...string) IFlysystem
Disk(disk string) IFlysystem
FindAdapter() IAdapter
}
type Flysystem struct {
disk string
diskAdapters map[string]IAdapter
diskNames []string
l *sync.Mutex
}
func New() IFlysystem {
return &Flysystem{
diskAdapters: make(map[string]IAdapter),
l: &sync.Mutex{},
}
}
func NewAdapters(adapters ...IAdapter) IFlysystem {
f := New()
for _, adapter := range adapters {
f.Extend(adapter)
}
return f
}
// Extend 扩展
func (f *Flysystem) Extend(adapter IAdapter, names ...string) IFlysystem {
f.l.Lock()
defer f.l.Unlock()
name := adapter.DiskName()
if len(names) > 0 {
name = names[0]
}
f.diskAdapters[name] = adapter
f.diskNames = append(f.diskNames, name)
return f
}
func (f *Flysystem) Disk(disk string) IFlysystem {
return &Flysystem{
disk: disk,
diskAdapters: f.diskAdapters,
diskNames: f.diskNames,
}
}
// FindAdapter Find Adapter
func (f *Flysystem) FindAdapter() IAdapter {
var disk string
if f.disk != "" {
disk = f.disk
} else if len(f.diskNames) > 0 {
disk = f.diskNames[0]
}
if adapter, ok := f.diskAdapters[disk]; ok {
return adapter
}
panic(fmt.Sprintf("Unable to find %s disk", disk))
}
func (f *Flysystem) Exists(path string) (bool, error) {
return f.FindAdapter().Exists(path)
}
func (f *Flysystem) WriteReader(path string, reader io.Reader) (string, error) {
return f.FindAdapter().WriteReader(path, reader)
}
func (f *Flysystem) Write(path string, contents []byte) (string, error) {
return f.FindAdapter().Write(path, contents)
}
func (f *Flysystem) WriteStream(path, resource string) (string, error) {
return f.FindAdapter().WriteStream(path, resource)
}
func (f *Flysystem) Update(path string, contents []byte) (string, error) {
return f.FindAdapter().Update(path, contents)
}
func (f *Flysystem) UpdateStream(path, resource string) (string, error) {
return f.FindAdapter().UpdateStream(path, resource)
}
func (f *Flysystem) Read(path string) ([]byte, error) {
return f.FindAdapter().Read(path)
}
func (f *Flysystem) Delete(path string) (int64, error) {
return f.FindAdapter().Delete(path)
}
func (f *Flysystem) CreateDirectory(dirname string) error {
return f.FindAdapter().CreateDirectory(dirname)
}
func (f *Flysystem) DeleteDirectory(dirname string) (int64, error) {
return f.FindAdapter().DeleteDirectory(dirname)
}
func (f *Flysystem) MimeType(path string) (string, error) {
return f.FindAdapter().MimeType(path)
}
func (f *Flysystem) Size(path string) (int64, error) {
return f.FindAdapter().Size(path)
}
func (f *Flysystem) Move(source, destination string) (bool, error) {
return f.FindAdapter().Move(source, destination)
}
func (f *Flysystem) Copy(source, destination string) (bool, error) {
return f.FindAdapter().Copy(source, destination)
}