-
Notifications
You must be signed in to change notification settings - Fork 1
/
webdav.go
203 lines (186 loc) · 4.87 KB
/
webdav.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
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/minio/minio-go/v7"
"golang.org/x/net/webdav"
"golang.org/x/sync/errgroup"
)
type Handler struct {
dummyDirs []string
}
func newHandler() *Handler {
return &Handler{
dummyDirs: []string{},
}
}
func (d *Handler) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
if readOnly {
return os.ErrPermission
}
mc := ctx.Value(minioClientCtxKey).(*minio.Client)
names := splitPath(name)
if len(names) == 0 {
return os.ErrInvalid
}
if len(names) == 1 {
if allowBucketsOps {
return mc.MakeBucket(ctx, names[0], minio.MakeBucketOptions{})
} else {
return os.ErrPermission
}
}
// d.dummyDirs = append(d.dummyDirs, strings.Join(names, "/"))
for i := len(names); i > 1; i-- {
list := mc.ListObjects(ctx, names[0], minio.ListObjectsOptions{
Prefix: strings.Join(names[1:i], "/") + "/",
})
isExist := false
for obj := range list {
if obj.Err != nil {
return handleMinioError(obj.Err)
}
isExist = true
}
if !isExist {
d.dummyDirs = append(d.dummyDirs, strings.Join(names[:i], "/"))
}
}
return nil
}
func (d *Handler) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
fmt.Println("OpenFile", name, flag, perm)
mc := ctx.Value(minioClientCtxKey).(*minio.Client)
names := splitPath(name)
return newFile(ctx, mc, names, d.dummyDirs, flag), nil
}
func (d *Handler) RemoveAll(ctx context.Context, name string) error {
if readOnly {
return os.ErrPermission
}
mc := ctx.Value(minioClientCtxKey).(*minio.Client)
names := splitPath(name)
if len(names) == 0 {
return os.ErrInvalid
}
if len(names) == 1 {
if allowBucketsOps {
return mc.RemoveBucket(ctx, names[0])
} else {
return os.ErrPermission
}
}
list := mc.ListObjects(ctx, names[0], minio.ListObjectsOptions{
Prefix: strings.Join(names[1:], "/"),
})
isExist := false
isDir := false
for obj := range list {
if obj.Err != nil {
return handleMinioError(obj.Err)
}
if obj.Key == strings.Join(names[1:], "/") {
isExist = true
isDir = false
if !preferDirectory {
break
}
}
if obj.Key != strings.Join(names[1:], "/") {
isExist = true
isDir = true
if preferDirectory {
break
}
}
}
if !isExist {
if isDummyDir(d.dummyDirs, names[0], strings.Join(names[1:], "/")) {
d.dummyDirs = deleteDummyDir(d.dummyDirs, names[0], strings.Join(names[1:], "/"))
return nil
}
return os.ErrNotExist
}
// Trace back to the parent folder where only the deletion target exists and save it.
for i := len(names) - 1; i > 1; i-- {
list := mc.ListObjects(ctx, names[0], minio.ListObjectsOptions{
Prefix: strings.Join(names[1:i], "/") + "/",
})
isExist := false
for obj := range list {
if obj.Err != nil {
return handleMinioError(obj.Err)
}
if !strings.HasPrefix(strings.Join(names[1:], "/")+"/", obj.Key) {
isExist = true
break
}
}
if isExist {
continue
}
d.dummyDirs = append(d.dummyDirs, strings.Join(names[:i], "/"))
}
if isDir {
eg := &errgroup.Group{}
list := mc.ListObjects(ctx, names[0], minio.ListObjectsOptions{
Prefix: strings.Join(names[1:], "/"),
Recursive: true,
})
for obj := range list {
obj := obj
if obj.Err != nil {
return handleMinioError(obj.Err)
}
if obj.Key != strings.Join(names[1:], "/") {
eg.Go(func() error {
return handleMinioError(mc.RemoveObject(ctx, names[0], obj.Key, minio.RemoveObjectOptions{}))
})
}
}
if err := eg.Wait(); err != nil {
return err
}
d.dummyDirs = deleteDummyDir(d.dummyDirs, names[0], strings.Join(names[1:], "/"))
} else {
if err := mc.RemoveObject(ctx, names[0], strings.Join(names[1:], "/"), minio.RemoveObjectOptions{}); err != nil {
return handleMinioError(err)
}
}
return nil
}
func (d *Handler) Rename(ctx context.Context, oldName, newName string) error {
if readOnly {
return os.ErrPermission
}
mc := ctx.Value(minioClientCtxKey).(*minio.Client)
oldNames := splitPath(oldName)
newNames := splitPath(newName)
dummyDirs, renameDummy := renameDummyDir(d.dummyDirs, oldNames[0], strings.Join(oldNames[1:], "/"), newNames[0], strings.Join(newNames[1:], "/"))
d.dummyDirs = dummyDirs
if renameDummy {
return nil
}
dst := minio.CopyDestOptions{
Bucket: newNames[0],
Object: strings.Join(newNames[1:], "/"),
}
src := minio.CopySrcOptions{
Bucket: oldNames[0],
Object: strings.Join(oldNames[1:], "/"),
}
if _, err := mc.CopyObject(ctx, dst, src); err != nil {
return handleMinioError(err)
}
if err := mc.RemoveObject(ctx, oldNames[0], strings.Join(oldNames[1:], "/"), minio.RemoveObjectOptions{}); err != nil {
return handleMinioError(err)
}
return nil
}
func (d *Handler) Stat(ctx context.Context, name string) (os.FileInfo, error) {
mc := ctx.Value(minioClientCtxKey).(*minio.Client)
names := splitPath(name)
return newFile(ctx, mc, names, d.dummyDirs, os.O_RDONLY).Stat()
}