-
Notifications
You must be signed in to change notification settings - Fork 4
/
drive.go
390 lines (309 loc) · 7.93 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package wfs
import (
"errors"
"io"
"log"
"path"
"sort"
"strings"
)
// Drive represents an isolated file system
type DriveFacade struct {
adapter Adapter
list *ListConfig
operation *OperationConfig
verbose bool
policy Policy
}
// NewLocalDrive returns new LocalDrive object
// which represents the file folder on local drive
// due to ForceRootPolicy all operations outside of the root folder will be blocked
func NewDrive(adapter Adapter, config *DriveConfig) Drive {
drive := DriveFacade{adapter:adapter, policy:adapter }
if config != nil {
drive.verbose = config.Verbose
drive.list = config.List
drive.operation = config.Operation
if config.Policy != nil {
drive.policy = CombinedPolicy{
[]Policy{
*config.Policy,
drive.policy,
},
}
}
}
if drive.list == nil {
drive.list = &ListConfig{}
}
if drive.operation == nil {
drive.operation = &OperationConfig{}
}
return &drive
}
// allow method checks is operation on object allowed or not
func (d *DriveFacade) allow(id FileID, operation int) bool {
return d.policy.Comply(id, operation)
}
func (d *DriveFacade) Search(id, search string, config ...*ListConfig) ([]File, error) {
path := d.adapter.ToFileID(id)
if d.verbose {
log.Printf("Search %s at %s", search, id)
}
if !d.allow(path, ReadOperation) {
return nil, errors.New("Access Denied")
}
data, err := d.adapter.Search(path, search)
if err != nil {
return nil, err
}
out := make([]File, 0)
for _, file := range data {
out = append(out, File{file.Name(), id, file.Size(), file.ModTime().Unix(), GetType(file.Name(), file.IsDir()), nil})
}
return out, nil
}
// List method returns array of files from the target folder
func (d *DriveFacade) List(id string, config ...*ListConfig) ([]File, error) {
path := d.adapter.ToFileID(id)
if d.verbose {
log.Printf("List %s", id)
}
if !d.allow(path, ReadOperation) {
return nil, errors.New("Access Denied")
}
var list *ListConfig
if len(config) > 0 {
list = config[0]
} else {
list = d.list
}
if d.verbose {
log.Printf("with config %+v", config)
}
return d.listFolder(path, list, nil)
}
// Remove deletes a file or a folder
func (d *DriveFacade) Remove(id string) error {
path := d.adapter.ToFileID(id)
if d.verbose {
log.Printf("Remove %s", id)
}
if !d.allow(path, WriteOperation) {
return errors.New("Access Denied")
}
return d.adapter.Remove(path)
}
// Read returns content of a file
func (d *DriveFacade) Read(id string) (io.ReadSeeker, error) {
path := d.adapter.ToFileID(id)
if d.verbose {
log.Printf("Read %s", id)
}
if !d.allow(path, ReadOperation) {
return nil, errors.New("Access Denied")
}
return d.adapter.Read(path)
}
// Write saves content to a file
func (d *DriveFacade) Write(id string, data io.Reader) error {
if d.verbose {
log.Printf("Write %s", id)
}
path := d.adapter.ToFileID(id)
if !d.allow(path, WriteOperation) {
return errors.New("Access Denied")
}
err := d.adapter.Write(path, data)
if err != nil {
return err
}
return nil
}
// Exists checks is file / folder with defined path does exist
func (d *DriveFacade) Exists(id string) bool {
path := d.adapter.ToFileID(id)
if !d.allow(path, ReadOperation) {
return false
}
return d.adapter.Exists(path, "")
}
// Info returns info about a single file / folder
func (d *DriveFacade) Info(id string) (File, error) {
path := d.adapter.ToFileID(id)
if !d.allow(path, ReadOperation) {
return File{}, errors.New("Access Denied")
}
info,err := d.adapter.Info(path)
if err != nil {
return File{}, errors.New("Access denied")
}
return File{ID: info.File().ClientID(), Name:info.Name(), Size: info.Size(), Date: info.ModTime().Unix(), Type: GetType(info.Name(), info.IsDir()), Files: nil }, nil
}
//Mkdir creates a new folder
func (d *DriveFacade) Make(id, name string, isFolder bool) (string, error) {
if d.verbose {
log.Printf("Make folder %s", id)
}
path := d.adapter.ToFileID(id)
if !d.allow(path, WriteOperation) {
return "", errors.New("Access Denied")
}
if d.operation.PreventNameCollision {
var err error
name, err = d.checkName(path, name, isFolder)
if err != nil {
return "", err
}
}
path, err := d.adapter.Make(path, name, isFolder)
if err != nil {
return "", err
}
return path.ClientID(), nil
}
// Copy makes a copy of file or a folder
func (d *DriveFacade) Copy(source, target, name string) (string, error) {
if d.verbose {
log.Printf("Copy %s to %s", source, target)
}
if name == "" {
name = path.Base(source)
}
from := d.adapter.ToFileID(source)
to := d.adapter.ToFileID(target)
if !d.allow(from, ReadOperation) || !d.allow(to, WriteOperation) {
return "", errors.New("Access Denied")
}
st := from.IsFolder()
if st && to.Contains(from) {
return "", errors.New("Can't copy folder into self")
}
if d.operation.PreventNameCollision {
var err error
name, err = d.checkName(to, name, st)
if err != nil {
return "", err
}
}
copied, err := d.adapter.Copy(from, to, name, st)
if err != nil {
return "", err
}
return copied.ClientID(), nil
}
// Move renames(moves) a file or a folder
func (d *DriveFacade) Move(source, target, name string) (string, error) {
if d.verbose {
log.Printf("Move %s to %s", source, target)
}
if name == "" {
name = path.Base(source)
}
from := d.adapter.ToFileID(source)
st := from.IsFolder()
var to FileID
if target == "" {
to = d.adapter.GetParent(from)
} else {
to = d.adapter.ToFileID(target)
if st && to.Contains(from) {
return "", errors.New("Can't copy folder into self")
}
}
if !d.allow(from, WriteOperation) || !d.allow(to, WriteOperation) {
return "", errors.New("Access Denied")
}
if d.operation.PreventNameCollision {
var err error
name, err = d.checkName(to, name, st)
if err != nil {
return "", err
}
}
moved, err := d.adapter.Move(from, to, name, st)
if err != nil {
return "", err
}
return moved.ClientID(), nil
}
func (d *DriveFacade) listFolder(path FileID, config *ListConfig, res []File) ([]File, error) {
list, er := d.adapter.List(path)
if er != nil {
return nil, er
}
needSortData := false
if config.Nested || res == nil {
res = make([]File, 0, len(list))
needSortData = true
}
for _, file := range list {
skipFile := false
if config.Exclude != nil && config.Exclude(file.Name()) {
continue
}
if config.Include != nil && !config.Include(file.Name()) {
skipFile = true
}
isDir := file.IsDir()
if !isDir && (config.SkipFiles || skipFile) {
continue
}
id := file.File().ClientID()
fs := File{file.Name(), id, file.Size(), file.ModTime().Unix(), GetType(file.Name(), file.IsDir()), nil}
if isDir && config.SubFolders {
sub, err := d.listFolder(file.File(),
config, res)
fs.Type = "folder"
if err != nil {
return nil, err
}
if !config.Nested {
res = sub
} else if len(sub) > 0 {
fs.Files = sub
}
}
if !skipFile {
res = append(res, fs)
}
}
// sort files and folders by name, folders first
if needSortData {
sort.Slice(res, func(i, j int) bool {
aFolder := res[i].Type == "folder"
bFolder := res[j].Type == "folder"
if (aFolder || bFolder) && res[i].Type != res[j].Type {
return aFolder
}
return strings.ToUpper(res[i].Name) < strings.ToUpper(res[j].Name)
})
}
return res, nil
}
func (d *DriveFacade) checkName(p FileID, name string, isFolder bool) (string, error) {
counter := 0
for d.adapter.Exists(p, name) {
ext := path.Ext(name)
if isFolder || ext == "" {
name = name + ".new"
} else {
index := len(name) - len(ext)
name = name[:index] + ".new" + name[index:]
}
counter++
if counter > 20 {
return name, errors.New("Can't create a new name for the file")
}
}
return name, nil
}
// WithOperationConfig makes a copy of drive with new operation config
func (d *DriveFacade) WithOperationConfig(config *OperationConfig) Drive {
copy := *d
copy.operation = config
return ©
}
func (d *DriveFacade) Stats() (uint64, uint64, error) {
return d.adapter.Stats()
}