This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
112 lines (92 loc) · 2.9 KB
/
driver.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
package MultipleDriver
import (
"errors"
"io"
"strings"
"github.com/goftp/server"
)
var _ server.Driver = &MultipleDriver{}
type MultipleDriver struct {
drivers map[string]server.Driver
}
// Init init
func (driver *MultipleDriver) Init(conn *server.Conn) {
}
func (driver *MultipleDriver) ChangeDir(path string) error {
for prefix, driver := range driver.drivers {
if strings.HasPrefix(path, prefix) {
return driver.ChangeDir(strings.TrimPrefix(path, prefix))
}
}
return errors.New("Not a directory")
}
func (driver *MultipleDriver) Stat(path string) (server.FileInfo, error) {
for prefix, driver := range driver.drivers {
if strings.HasPrefix(path, prefix) {
return driver.Stat(strings.TrimPrefix(path, prefix))
}
}
return nil, errors.New("Not a file")
}
func (driver *MultipleDriver) ListDir(path string, callback func(server.FileInfo) error) error {
for prefix, driver := range driver.drivers {
if strings.HasPrefix(path, prefix) {
return driver.ListDir(strings.TrimPrefix(path, prefix), callback)
}
}
return errors.New("Not a directory")
}
func (driver *MultipleDriver) DeleteDir(path string) error {
for prefix, driver := range driver.drivers {
if strings.HasPrefix(path, prefix) {
return driver.DeleteDir(strings.TrimPrefix(path, prefix))
}
}
return errors.New("Not a directory")
}
func (driver *MultipleDriver) DeleteFile(path string) error {
for prefix, driver := range driver.drivers {
if strings.HasPrefix(path, prefix) {
return driver.DeleteFile(strings.TrimPrefix(path, prefix))
}
}
return errors.New("Not a file")
}
func (driver *MultipleDriver) Rename(fromPath string, toPath string) error {
for prefix, driver := range driver.drivers {
if strings.HasPrefix(fromPath, prefix) {
return driver.Rename(strings.TrimPrefix(fromPath, prefix), strings.TrimPrefix(toPath, prefix))
}
}
return errors.New("Not a file")
}
func (driver *MultipleDriver) MakeDir(path string) error {
for prefix, driver := range driver.drivers {
if strings.HasPrefix(path, prefix) {
return driver.MakeDir(strings.TrimPrefix(path, prefix))
}
}
return errors.New("Not a directory")
}
func (driver *MultipleDriver) GetFile(path string, offset int64) (int64, io.ReadCloser, error) {
for prefix, driver := range driver.drivers {
if strings.HasPrefix(path, prefix) {
return driver.GetFile(strings.TrimPrefix(path, prefix), offset)
}
}
return 0, nil, errors.New("Not a file")
}
func (driver *MultipleDriver) PutFile(destPath string, data io.Reader, appendData bool) (int64, error) {
for prefix, driver := range driver.drivers {
if strings.HasPrefix(destPath, prefix) {
return driver.PutFile(strings.TrimPrefix(destPath, prefix), data, appendData)
}
}
return 0, errors.New("Not a file")
}
type MultipleDriverFactory struct {
drivers map[string]server.Driver
}
func (factory *MultipleDriverFactory) NewDriver() (server.Driver, error) {
return &MultipleDriver{factory.drivers}, nil
}