-
Notifications
You must be signed in to change notification settings - Fork 169
/
plugins.go
199 lines (176 loc) · 5.82 KB
/
plugins.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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package objects is in charge of exposing the content of the datasource with the S3 protocol.
package objects
import (
"context"
"os"
"path/filepath"
"strings"
"github.com/micro/go-micro"
"github.com/micro/go-micro/errors"
"github.com/pydio/cells/common/plugins"
"github.com/spf13/afero"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/service"
"github.com/pydio/cells/common/service/context"
)
var (
Name = common.SERVICE_GRPC_NAMESPACE_ + common.SERVICE_DATA_OBJECTS
ChildPrefix = common.SERVICE_GRPC_NAMESPACE_ + common.SERVICE_DATA_OBJECTS_
)
func init() {
plugins.Register(func() {
service.NewService(
service.Name(Name),
service.Tag(common.SERVICE_TAG_DATASOURCE),
service.Description("Starter for different sources objects"),
service.WithMicro(func(m micro.Service) error {
runner := service.NewChildrenRunner(m, Name, ChildPrefix)
m.Init(
micro.AfterStart(func() error {
ctx := m.Options().Context
conf := servicecontext.GetConfig(ctx)
runner.StartFromInitialConf(ctx, conf)
tree.RegisterNodeProviderHandler(m.Server(), NewTreeHandler())
runner.OnDeleteConfig(onDeleteObjectsConfig)
return nil
}))
tree.RegisterNodeProviderHandler(m.Server(), NewTreeHandler())
tree.RegisterNodeReceiverHandler(m.Server(), NewTreeHandler())
return runner.Watch(m.Options().Context)
}),
)
})
}
// Manage datasources deletion operations : clean minio server configuration folder
func onDeleteObjectsConfig(ctx context.Context, objectConfigName string) {
if err := DeleteMinioConfigDir(objectConfigName); err != nil {
log.Logger(ctx).Error("Could not remove configuration folder for object service " + objectConfigName)
} else {
log.Logger(ctx).Info("Removed configuration folder for object service " + objectConfigName)
}
}
func NewTreeHandler() *TreeHandler {
return &TreeHandler{
FS: afero.NewBasePathFs(afero.NewOsFs(), "/"),
}
}
type TreeHandler struct {
FS afero.Fs
}
func (t *TreeHandler) SymlinkInfo(path string, info os.FileInfo) (bool, tree.NodeType, string) {
if info.Mode()&os.ModeSymlink != 0 {
if t, e := os.Readlink(path); e == nil {
target, er := os.Stat(t)
if er != nil {
// If error on stat, try to resolve absolute path
if t, er = filepath.Abs(filepath.Join(filepath.Dir(path), t)); er == nil {
target, er = os.Stat(t)
}
}
if er == nil {
if target.IsDir() {
return true, tree.NodeType_COLLECTION, t
} else {
return true, tree.NodeType_LEAF, t
}
}
}
}
return false, tree.NodeType_UNKNOWN, ""
}
func (t *TreeHandler) FileInfoToNode(nodePath string, fileInfo os.FileInfo) *tree.Node {
node := &tree.Node{
Path: nodePath,
Size: fileInfo.Size(),
MTime: fileInfo.ModTime().Unix(),
MetaStore: map[string]string{
"permissions": fileInfo.Mode().String(),
"name": fileInfo.Name(),
},
}
if fileInfo.IsDir() {
node.Type = tree.NodeType_COLLECTION
} else {
if yes, typ, target := t.SymlinkInfo(nodePath, fileInfo); yes {
node.Type = typ
node.MetaStore["symlink"] = target
} else {
node.Type = tree.NodeType_LEAF
}
}
return node
}
func (t *TreeHandler) ReadNode(ctx context.Context, request *tree.ReadNodeRequest, response *tree.ReadNodeResponse) error {
if fileInfo, e := t.FS.Stat(request.Node.Path); e != nil {
return e
} else {
response.Node = t.FileInfoToNode(request.Node.Path, fileInfo)
}
return nil
}
func (t *TreeHandler) ListNodes(ctx context.Context, request *tree.ListNodesRequest, stream tree.NodeProvider_ListNodesStream) error {
defer stream.Close()
if fileInfos, e := afero.ReadDir(t.FS, request.Node.Path); e == nil {
for _, info := range fileInfos {
path := filepath.Join(request.Node.Path, info.Name())
if isSymLink, _, _ := t.SymlinkInfo(path, info); !isSymLink && (strings.HasPrefix(info.Name(), ".") || !info.IsDir()) {
continue
}
stream.Send(&tree.ListNodesResponse{
Node: t.FileInfoToNode(path, info),
})
}
return nil
} else {
return e
}
}
func (t *TreeHandler) CreateNode(ctx context.Context, request *tree.CreateNodeRequest, response *tree.CreateNodeResponse) error {
if request.Node.IsLeaf() {
if file, e := t.FS.Create(request.Node.Path); e != nil {
return e
} else {
fileInfo, _ := file.Stat()
response.Node = t.FileInfoToNode(request.Node.Path, fileInfo)
}
} else {
if e := t.FS.MkdirAll(request.Node.Path, 0755); e != nil {
return e
} else {
fileInfo, _ := t.FS.Stat(request.Node.Path)
response.Node = t.FileInfoToNode(request.Node.Path, fileInfo)
}
}
return nil
}
func (t *TreeHandler) UpdateNode(ctx context.Context, request *tree.UpdateNodeRequest, response *tree.UpdateNodeResponse) error {
return errors.BadRequest("not.implemented", "")
}
func (t *TreeHandler) DeleteNode(ctx context.Context, request *tree.DeleteNodeRequest, response *tree.DeleteNodeResponse) error {
if e := t.FS.Remove(request.Node.Path); e != nil {
return e
}
response.Success = true
return nil
}