-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrive.go
195 lines (161 loc) · 4.18 KB
/
drive.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
package local
import (
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/xbsoftware/wfs"
)
type LocalDrive struct {
root string
}
type localFile struct {
path string
id string
}
func (l localFile) GetPath() string {
return l.path
}
func (l localFile) ClientID() string {
return l.id
}
func (l localFile) IsFolder() bool {
fi, err := os.Stat(l.GetPath())
if err == nil && fi.IsDir() {
return true
}
return false
}
func (l localFile) Contains(target wfs.FileID) bool {
if strings.HasPrefix(l.GetPath(), target.GetPath()+string(filepath.Separator)) {
return true
}
return false
}
type localFileInfo struct {
os.FileInfo
f wfs.FileID
}
func (i localFileInfo) File() wfs.FileID {
return i.f
}
func NewLocalDrive(path string, config *wfs.DriveConfig) (wfs.Drive, error) {
root, err := filepath.Abs(path)
if err != nil {
return nil, errors.New("Invalid path: " + root)
}
d := LocalDrive{root: root}
return wfs.NewDrive(&d, config), nil
}
func (l *LocalDrive) Comply(f wfs.FileID, operation int) bool {
return strings.Contains(filepath.Clean(f.GetPath()), l.root)
}
func (l *LocalDrive) ToFileID(id string) wfs.FileID {
return localFile{filepath.Clean(filepath.Join(l.root, filepath.FromSlash(id))), id}
}
func (l *LocalDrive) GetParent(f wfs.FileID) wfs.FileID {
return localFile{filepath.Dir(f.GetPath()), filepath.Dir(f.ClientID())}
}
func (l *LocalDrive) newLocalFile(path string) localFile {
return localFile{path, filepath.ToSlash(strings.Replace(path, l.root, "", 1))}
}
func (l *LocalDrive) Remove(f wfs.FileID) error {
return os.RemoveAll(f.GetPath())
}
func (l *LocalDrive) Read(f wfs.FileID) (io.ReadSeeker, error) {
file, err := os.Open(f.GetPath())
if err != nil {
return nil, errors.New("Can't open file for reading")
}
return file, nil
}
func (l *LocalDrive) Write(f wfs.FileID, data io.Reader) error {
file, err := os.Create(f.GetPath())
if err != nil {
return errors.New("Can't open file for writing")
}
defer file.Close()
_, err = io.Copy(file, data)
if err != nil {
return errors.New("Can't write data")
}
return err
}
func (l *LocalDrive) Make(f wfs.FileID, name string, isFolder bool) (wfs.FileID, error) {
full := filepath.Join(f.GetPath(), name)
out := l.newLocalFile(full)
if isFolder {
return out, os.MkdirAll(full, os.FileMode(int(0700)))
} else {
file, err := os.Create(full)
if err != nil {
return out, err
}
return out, file.Close()
}
}
func (l *LocalDrive) Copy(source, target wfs.FileID, name string, isFolder bool) (wfs.FileID, error) {
full := filepath.Join(target.GetPath(), name)
if isFolder {
return l.newLocalFile(full), copyDir(source.GetPath(), full)
}
//copy file
return l.newLocalFile(full), copyFile(source.GetPath(), full)
}
// Move renames(moves) a file or a folder
func (l *LocalDrive) Move(source, target wfs.FileID, name string, isFolder bool) (wfs.FileID, error) {
full := filepath.Join(target.GetPath(), name)
return l.newLocalFile(full), os.Rename(source.GetPath(), full)
}
// Info returns info about a single file
func (l *LocalDrive) Info(f wfs.FileID) (wfs.FileInfo, error) {
file, err := os.Stat(f.GetPath())
if err != nil {
return nil, err
}
return localFileInfo{file, f}, nil
}
//
func (l *LocalDrive) List(f wfs.FileID) ([]wfs.FileInfo, error) {
full := f.GetPath()
files, err := ioutil.ReadDir(full)
if err != nil {
return nil, err
}
info := make([]wfs.FileInfo, 0, len(files))
for i := range files {
info = append(info, localFileInfo{
files[i],
l.newLocalFile(filepath.Join(full, files[i].Name())),
})
}
return info, nil
}
func (l *LocalDrive) Search(f wfs.FileID, search string) ([]wfs.FileInfo, error) {
matches, err := glob(f.GetPath(), search)
if err != nil {
return nil, err
}
out := make([]wfs.FileInfo, len(matches))
for i := range matches {
info, _ := os.Stat(matches[i])
out[i] = localFileInfo{info, l.newLocalFile(matches[i])}
}
return out, nil
}
func (l *LocalDrive) Exists(f wfs.FileID, name string) bool {
full := f.GetPath()
if name != "" {
full = filepath.Join(full, name)
}
_, err := os.Stat(full)
if err != nil {
return false
}
return true
}
func (l *LocalDrive) Stats() (uint64, uint64, error) {
return getFSSize(l.root)
}