Skip to content

Commit 7da8006

Browse files
committed
server: smb/ftp/sftp add copy function, add recycle test for memory
fix upyun copy/move/rename
1 parent 7cd6662 commit 7da8006

File tree

17 files changed

+432
-156
lines changed

17 files changed

+432
-156
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@
1414
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1515
.glide/
1616
*.bak
17-
_tmp
17+
_tmp
18+
19+
.DS_Store
20+
docs/

server/pkg/driver/base/base.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/honmaple/maple-file/server/pkg/driver"
8+
"github.com/honmaple/maple-file/server/pkg/util"
89
)
910

1011
type Option struct {
@@ -19,7 +20,7 @@ type Option struct {
1920
}
2021

2122
func (opt *Option) getActualPath(path string) string {
22-
path = filepath.Clean(path)
23+
path = util.CleanPath(path)
2324
if opt.RootPath == "" {
2425
return path
2526
}

server/pkg/driver/base/hook.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/honmaple/maple-file/server/pkg/driver"
9+
"github.com/honmaple/maple-file/server/pkg/util"
910
)
1011

1112
type HookOption struct {
@@ -25,7 +26,7 @@ type hookFS struct {
2526
var _ driver.FS = (*hookFS)(nil)
2627

2728
func (d *hookFS) getActualPath(path string) string {
28-
path = filepath.Clean(path)
29+
path = util.CleanPath(path)
2930
if d.opt.PathFn == nil {
3031
return path
3132
}

server/pkg/driver/errors.go

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ package driver
22

33
import (
44
"errors"
5-
"sync"
65

7-
"github.com/go-playground/locales/en"
8-
"github.com/go-playground/locales/zh"
9-
"github.com/go-playground/universal-translator"
10-
"github.com/go-playground/validator/v10"
11-
zh_translations "github.com/go-playground/validator/v10/translations/zh"
6+
"github.com/honmaple/maple-file/server/pkg/util"
127
)
138

149
var (
@@ -17,31 +12,7 @@ var (
1712
ErrSrcNotExist = errors.New("src is not exists")
1813
ErrDstIsExist = errors.New("dst already exists")
1914
ErrDriverNotExist = errors.New("driver is not exists")
20-
)
15+
ErrOpenDirectory = errors.New("can't open a directory")
2116

22-
var (
23-
uni *ut.UniversalTranslator
24-
validate *validator.Validate
25-
once sync.Once
17+
VerifyOption = util.VerifyOption
2618
)
27-
28-
func VerifyOption(value interface{}) error {
29-
once.Do(func() {
30-
en := en.New()
31-
uni = ut.New(en, en, zh.New())
32-
validate = validator.New(validator.WithRequiredStructEnabled())
33-
34-
trans, _ := uni.GetTranslator("zh")
35-
zh_translations.RegisterDefaultTranslations(validate, trans)
36-
})
37-
38-
trans, _ := uni.GetTranslator("zh")
39-
40-
if err := validate.Struct(value); err != nil {
41-
if e, ok := err.(validator.ValidationErrors); ok && len(e) > 0 {
42-
return errors.New(e[0].Translate(trans))
43-
}
44-
return err
45-
}
46-
return nil
47-
}

server/pkg/driver/fs.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"io/fs"
8-
"path/filepath"
97
)
108

119
type (
@@ -21,7 +19,6 @@ type (
2119
Create(string) (FileWriter, error)
2220
Close() error
2321
}
24-
WalkDirFunc func(string, File, error) error
2522

2623
Option interface {
2724
NewFS() (FS, error)
@@ -44,50 +41,6 @@ func (Base) Close() error { return nil
4441

4542
var allOptions map[string]OptionCreator
4643

47-
func walkDir(ctx context.Context, srcFS FS, root string, d File, walkDirFn WalkDirFunc) error {
48-
if err := walkDirFn(root, d, nil); err != nil || !d.IsDir() {
49-
if err == fs.SkipDir && d.IsDir() {
50-
err = nil
51-
}
52-
return err
53-
}
54-
55-
files, err := srcFS.List(ctx, filepath.Join(d.Path(), d.Name()))
56-
if err != nil {
57-
err = walkDirFn(root, d, err)
58-
if err != nil {
59-
if err == fs.SkipDir && d.IsDir() {
60-
err = nil
61-
}
62-
return err
63-
}
64-
return err
65-
}
66-
for _, file := range files {
67-
name := filepath.Join(root, file.Name())
68-
if err := walkDir(ctx, srcFS, name, file, walkDirFn); err != nil {
69-
if err == fs.SkipDir {
70-
break
71-
}
72-
return err
73-
}
74-
}
75-
return nil
76-
}
77-
78-
func WalkDir(ctx context.Context, srcFS FS, root string, walkDirFn WalkDirFunc) error {
79-
info, err := srcFS.Get(ctx, root)
80-
if err != nil {
81-
err = walkDirFn(root, nil, err)
82-
} else {
83-
err = walkDir(ctx, srcFS, root, info, walkDirFn)
84-
}
85-
if err == fs.SkipDir || err == fs.SkipAll {
86-
return nil
87-
}
88-
return err
89-
}
90-
9144
func DriverFS(driver string, option string) (FS, error) {
9245
creator, ok := allOptions[driver]
9346
if !ok {

server/pkg/driver/ftp/ftp.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"io/fs"
78
"path/filepath"
89
"time"
910

@@ -50,6 +51,9 @@ func (d *FTP) Open(path string) (driver.FileReader, error) {
5051
if err != nil {
5152
return nil, err
5253
}
54+
if info.Type == ftp.EntryTypeFolder {
55+
return nil, &fs.PathError{Op: "open", Path: path, Err: driver.ErrOpenDirectory}
56+
}
5357

5458
rangeFunc := func(offset, length int64) (io.ReadCloser, error) {
5559
return d.client.RetrFrom(path, uint64(offset))
@@ -84,7 +88,7 @@ func (d *FTP) Move(ctx context.Context, src, dst string) error {
8488
}
8589

8690
func (d *FTP) Copy(ctx context.Context, src, dst string) error {
87-
return driver.ErrNotSupport
91+
return driver.Copy(ctx, d, src, dst)
8892
}
8993

9094
func (d *FTP) Rename(ctx context.Context, path, newName string) error {

server/pkg/driver/local/local.go

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Local struct {
2929
}
3030

3131
func (d *Local) getActualPath(path string) string {
32-
return filepath.Join(d.opt.Path, filepath.Clean(path))
32+
return filepath.Join(d.opt.Path, path)
3333
}
3434

3535
func (d *Local) List(ctx context.Context, path string, metas ...driver.Meta) ([]driver.File, error) {
@@ -81,47 +81,42 @@ func (d *Local) Move(ctx context.Context, src, dst string) error {
8181
return os.Rename(d.getActualPath(src), filepath.Join(d.getActualPath(dst), filepath.Base(src)))
8282
}
8383

84-
func (d *Local) copyFile(ctx context.Context, src, dst string) (err error) {
85-
in, err := os.Open(src)
84+
func (d *Local) copyFile(ctx context.Context, src, dst string) error {
85+
srcFile, err := os.Open(src)
8686
if err != nil {
87-
return
87+
return err
8888
}
89-
defer in.Close()
89+
defer srcFile.Close()
9090

91-
out, err := os.Create(dst)
91+
dstFile, err := os.Create(dst)
9292
if err != nil {
93-
return
93+
return err
9494
}
9595

96-
if _, err = util.CopyWithContext(ctx, out, in); err != nil {
97-
out.Close()
98-
return
96+
if _, err = util.CopyWithContext(ctx, dstFile, srcFile); err != nil {
97+
dstFile.Close()
98+
return err
9999
}
100100

101-
if err = out.Close(); err != nil {
102-
return
101+
if err = dstFile.Close(); err != nil {
102+
return err
103103
}
104104

105-
info, err := in.Stat()
105+
info, err := os.Stat(src)
106106
if err != nil {
107-
return
107+
return err
108108
}
109-
err = os.Chmod(dst, info.Mode())
110-
return
109+
return os.Chmod(dst, info.Mode())
111110
}
112111

113112
func (d *Local) copyDir(ctx context.Context, src, dst string) error {
114-
stat, err := os.Stat(dst)
115-
if err != nil && !os.IsNotExist(err) {
116-
return err
117-
}
113+
info, err := os.Stat(src)
118114
if err != nil {
119-
if err := d.MakeDir(ctx, dst); err != nil {
120-
return err
121-
}
115+
return err
122116
}
123-
if !stat.IsDir() {
124-
return fmt.Errorf("cannot copy dir to file: %s", dst)
117+
118+
if err := os.MkdirAll(dst, info.Mode()); err != nil {
119+
return err
125120
}
126121

127122
files, err := os.ReadDir(src)
@@ -188,8 +183,10 @@ func New(opt *Option) (driver.FS, error) {
188183
if err := driver.VerifyOption(opt); err != nil {
189184
return nil, err
190185
}
186+
if util.CleanPath(opt.Path) != opt.Path {
187+
return nil, fs.ErrInvalid
188+
}
191189

192-
opt.Path = filepath.Clean(opt.Path)
193190
opt.DirPerm = 0755
194191

195192
d := &Local{opt: opt}

server/pkg/driver/memory/memory.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (d *Memory) Rename(ctx context.Context, path, newName string) error {
9797
if info.IsDir() {
9898
newFiles := make(map[string]*fstest.MapFile)
9999
for key, value := range d.client {
100-
if strings.HasPrefix(key, path) {
100+
if util.IsSubPath(path, key) {
101101
newFiles[filepath.Join(dst, strings.TrimPrefix(key, path))] = value
102102
delete(d.client, key)
103103
}
@@ -117,10 +117,12 @@ func (d *Memory) Move(ctx context.Context, src, dst string) error {
117117
if err != nil {
118118
return err
119119
}
120+
dst = filepath.Join(dst, filepath.Base(src))
121+
120122
if info.IsDir() {
121123
newFiles := make(map[string]*fstest.MapFile)
122124
for key, value := range d.client {
123-
if strings.HasPrefix(key, src) {
125+
if util.IsSubPath(src, key) {
124126
newFiles[filepath.Join(dst, strings.TrimPrefix(key, src))] = value
125127
delete(d.client, key)
126128
}
@@ -130,7 +132,7 @@ func (d *Memory) Move(ctx context.Context, src, dst string) error {
130132
}
131133
return nil
132134
}
133-
d.client[filepath.Join(dst, filepath.Base(src))] = d.client[src]
135+
d.client[dst] = d.client[src]
134136
delete(d.client, src)
135137
return nil
136138
}
@@ -140,10 +142,12 @@ func (d *Memory) Copy(ctx context.Context, src, dst string) error {
140142
if err != nil {
141143
return err
142144
}
145+
dst = filepath.Join(dst, filepath.Base(src))
146+
143147
if info.IsDir() {
144148
newFiles := make(map[string]*fstest.MapFile)
145149
for key, value := range d.client {
146-
if strings.HasPrefix(key, src) {
150+
if util.IsSubPath(src, key) {
147151
newFiles[filepath.Join(dst, strings.TrimPrefix(key, src))] = value
148152
}
149153
}
@@ -152,7 +156,7 @@ func (d *Memory) Copy(ctx context.Context, src, dst string) error {
152156
}
153157
return nil
154158
}
155-
d.client[filepath.Join(dst, filepath.Base(src))] = d.client[src]
159+
d.client[dst] = d.client[src]
156160
return nil
157161
}
158162

0 commit comments

Comments
 (0)