forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler-version.go
204 lines (183 loc) · 6.65 KB
/
handler-version.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
204
/*
* 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 views
import (
"context"
"io"
"github.com/micro/go-micro/client"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/proto/tree"
)
// VersionHandler capture ListNodes and GetObject calls to find existing nodes versions and retrieve them.
type VersionHandler struct {
AbstractHandler
versionClient tree.NodeVersionerClient
}
func (v *VersionHandler) getVersionClient() tree.NodeVersionerClient {
if v.versionClient == nil {
v.versionClient = tree.NewNodeVersionerClient(common.ServiceGrpcNamespace_+common.ServiceVersions, defaults.NewClient())
}
return v.versionClient
}
// Create list of nodes if the Versions are required
func (v *VersionHandler) ListNodes(ctx context.Context, in *tree.ListNodesRequest, opts ...client.CallOption) (tree.NodeProvider_ListNodesClient, error) {
ctx, err := v.wrapContext(ctx)
if err != nil {
return nil, err
}
if in.WithVersions {
streamer := NewWrappingStreamer()
resp, e := v.next.ReadNode(ctx, &tree.ReadNodeRequest{Node: in.Node})
if e != nil {
return streamer, e
}
versionStream, er := v.getVersionClient().ListVersions(ctx, &tree.ListVersionsRequest{Node: resp.Node})
if er != nil {
return streamer, er
}
go func() {
defer versionStream.Close()
defer streamer.Close()
log.Logger(ctx).Debug("should list versions of object", zap.Any("node", resp.Node), zap.Error(er))
for {
vResp, vE := versionStream.Recv()
if vE != nil {
break
}
if vResp == nil {
continue
}
log.Logger(ctx).Debug("received version", zap.Any("version", vResp))
vNode := resp.Node
vNode.Etag = string(vResp.Version.Data)
vNode.MTime = vResp.Version.MTime
vNode.Size = vResp.Version.Size
vNode.SetMeta("versionId", vResp.Version.Uuid)
vNode.SetMeta("versionDescription", vResp.Version.Description)
streamer.Send(&tree.ListNodesResponse{
Node: vNode,
})
}
}()
return streamer, nil
} else {
return v.next.ListNodes(ctx, in, opts...)
}
}
func (v *VersionHandler) ReadNode(ctx context.Context, req *tree.ReadNodeRequest, opts ...client.CallOption) (*tree.ReadNodeResponse, error) {
if vId := req.Node.GetStringMeta("versionId"); vId != "" {
// Load Info from Version Service?
log.Logger(ctx).Debug("Reading Node with Version ID", zap.String("versionId", vId))
node := req.Node
if len(node.Uuid) == 0 {
resp, e := v.next.ReadNode(ctx, &tree.ReadNodeRequest{Node: node})
if e != nil {
return nil, e
}
node = resp.Node
}
log.Logger(ctx).Debug("Reading Node with Version ID - Found node")
vResp, err := v.getVersionClient().HeadVersion(ctx, &tree.HeadVersionRequest{Node: node, VersionId: vId})
if err != nil {
return nil, err
}
log.Logger(ctx).Debug("Reading Node with Version ID - Found version", zap.Any("version", vResp.Version))
node.Etag = string(vResp.Version.Data)
node.MTime = vResp.Version.MTime
node.Size = vResp.Version.Size
return &tree.ReadNodeResponse{Node: node}, nil
}
return v.next.ReadNode(ctx, req, opts...)
}
// GetObject redirects to Version Store if request contains a VersionID
func (v *VersionHandler) GetObject(ctx context.Context, node *tree.Node, requestData *GetRequestData) (io.ReadCloser, error) {
ctx, err := v.wrapContext(ctx)
if err != nil {
return nil, err
}
if len(requestData.VersionId) > 0 {
// We are trying to load a specific versionId => switch to vID store
if len(node.Uuid) == 0 {
resp, e := v.next.ReadNode(ctx, &tree.ReadNodeRequest{Node: node})
if e != nil {
return nil, e
}
node = resp.Node
}
vResp, err := v.getVersionClient().HeadVersion(ctx, &tree.HeadVersionRequest{Node: node, VersionId: requestData.VersionId})
if err != nil {
return nil, err
}
node = vResp.Version.GetLocation()
// Append Version information
node.Size = vResp.Version.Size
node.Etag = string(vResp.Version.Data)
node.MTime = vResp.Version.MTime
// Refresh context from location
dsName := node.GetStringMeta(common.MetaNamespaceDatasourceName)
source, e := v.clientsPool.GetDataSourceInfo(dsName)
if e != nil {
return nil, e
}
branchInfo := BranchInfo{LoadedSource: source}
ctx = WithBranchInfo(ctx, "in", branchInfo)
log.Logger(ctx).Debug("GetObject With VersionId", zap.Any("node", node))
}
return v.next.GetObject(ctx, node, requestData)
}
// CopyObject intercept request with a SrcVersionId to read original from Version Store
func (v *VersionHandler) CopyObject(ctx context.Context, from *tree.Node, to *tree.Node, requestData *CopyRequestData) (int64, error) {
ctx, err := v.wrapContext(ctx)
if err != nil {
return 0, err
}
log.Logger(ctx).Debug("CopyObject Has VersionId?", zap.Any("from", from), zap.Any("to", to), zap.Any("requestData", requestData))
if len(requestData.SrcVersionId) > 0 {
// We are trying to load a specific versionId => switch to vID store
if len(from.Uuid) == 0 {
resp, e := v.next.ReadNode(ctx, &tree.ReadNodeRequest{Node: from})
if e != nil {
return 0, e
}
from = resp.Node
}
vResp, err := v.getVersionClient().HeadVersion(ctx, &tree.HeadVersionRequest{Node: from, VersionId: requestData.SrcVersionId})
if err != nil {
return 0, err
}
if requestData.Metadata == nil {
requestData.Metadata = make(map[string]string, 1)
}
requestData.Metadata[common.XAmzMetaNodeUuid] = from.Uuid // Make sure to keep Uuid!
from = vResp.GetVersion().GetLocation()
// Refresh context from location
source, e := v.clientsPool.GetDataSourceInfo(from.GetStringMeta(common.MetaNamespaceDatasourceName))
if e != nil {
return 0, e
}
srcInfo := BranchInfo{LoadedSource: source}
ctx = WithBranchInfo(ctx, "from", srcInfo)
log.Logger(ctx).Debug("CopyObject With VersionId", zap.Any("from", from), zap.Any("branchInfo", srcInfo), zap.Any("to", to))
}
return v.next.CopyObject(ctx, from, to, requestData)
}