-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfile_storage.go
191 lines (179 loc) · 3.89 KB
/
file_storage.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
package file
import (
"fmt"
"github.com/jlaffaye/ftp"
"io"
"net/textproto"
"os"
"strings"
"time"
)
// LocalFileSystem a FileStorage implementation backed by local file system
type LocalFileSystem struct {
}
func (fs *LocalFileSystem) Name() string {
return LocalFileStorage
}
func (fs *LocalFileSystem) Exists(fileName string) (bool, error) {
_, err := os.Stat(fileName)
if err != nil && os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func (fs *LocalFileSystem) Open(fileName, encoding string) (io.ReadCloser, error) {
file, err := os.Open(fileName)
return file, err
}
func (fs *LocalFileSystem) Create(fileName, encoding string) (io.WriteCloser, error) {
idx := strings.LastIndex(fileName, "/")
if idx > 0 {
path := fileName[0:idx]
os.MkdirAll(path, os.ModePerm)
}
return os.Create(fileName)
}
// FTPFileSystem a FileStorage implementation backed by ftp
type FTPFileSystem struct {
Hort string
Port int
User string
Password string
ConnTimeout time.Duration
}
func (fs *FTPFileSystem) Name() string {
return FTPFileStorage
}
func (fs *FTPFileSystem) connect() (*ftp.ServerConn, error) {
addr := fmt.Sprintf("%s:%d", fs.Hort, fs.Port)
/*conn, err := net.DialTimeout("tcp", addr, fs.ConnTimeout)
if err != nil {
return nil, err
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.SetReadBuffer(1000*1000)
tcpConn.SetWriteBuffer(1000*1000)
}
c, err := ftp.Dial(addr, ftp.DialWithNetConn(conn))*/
c, err := ftp.DialTimeout(addr, fs.ConnTimeout)
if err != nil {
return nil, err
}
err = c.Login(fs.User, fs.Password)
return c, err
}
func (fs *FTPFileSystem) Exists(fileName string) (bool, error) {
c, err := fs.connect()
if c != nil {
defer c.Quit()
}
if err != nil {
return false, err
}
_, err = c.FileSize(fileName)
if err == nil {
return true, nil
}
if err != nil {
if e, ok := err.(*textproto.Error); ok && e.Code == ftp.StatusFileUnavailable {
return false, nil
}
}
return false, err
}
func (fs *FTPFileSystem) Open(fileName, encoding string) (io.ReadCloser, error) {
c, err := fs.connect()
if err != nil {
if c != nil {
c.Quit()
}
return nil, err
}
r, err := c.Retr(fileName)
if err != nil {
c.Quit()
return nil, err
}
return &FTPFileInStream{
c: c,
r: r,
}, nil
}
func (fs *FTPFileSystem) Create(fileName, encoding string) (writer io.WriteCloser, err error) {
c, err := fs.connect()
if err != nil {
if c != nil {
defer c.Quit()
}
return nil, err
}
dirs := strings.Split(fileName, "/")
for _, dir := range dirs[:len(dirs)-1] {
if err = c.ChangeDir(dir); err != nil {
if e, ok := err.(*textproto.Error); ok && e.Code != ftp.StatusFileUnavailable {
return nil, err
}
if err = c.MakeDir(dir); err != nil {
if e, ok := err.(*textproto.Error); ok && e.Code != ftp.StatusFileUnavailable {
return nil, err
}
}
if err = c.ChangeDir(dir); err != nil {
return nil, err
}
}
}
name := dirs[len(dirs)-1]
r, w := io.Pipe()
ch := make(chan error)
go func() {
err = c.Stor(name, r)
ch <- err
}()
return &FTPFileOutStream{
c: c,
w: w,
ch: ch,
}, nil
}
// FTPFileInStream represents a ftp file input stream
type FTPFileInStream struct {
c *ftp.ServerConn
r io.ReadCloser
}
func (fis *FTPFileInStream) Read(p []byte) (n int, err error) {
return fis.r.Read(p)
}
func (fis *FTPFileInStream) Close() (err error) {
defer func() {
e := fis.c.Quit()
if err == nil {
err = e
}
}()
err = fis.r.Close()
return err
}
// FTPFileOutStream represents a ftp file output stream
type FTPFileOutStream struct {
c *ftp.ServerConn
w io.WriteCloser
ch chan error
}
func (fis *FTPFileOutStream) Write(p []byte) (n int, err error) {
return fis.w.Write(p)
}
func (fis *FTPFileOutStream) Close() (err error) {
defer func() {
err = <-fis.ch
e := fis.c.Quit()
if err == nil {
err = e
}
}()
err = fis.w.Close()
return err
}