forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler-archive.go
359 lines (314 loc) · 11.6 KB
/
handler-archive.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* 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"
"encoding/json"
"io"
"path"
"path/filepath"
"strings"
"time"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"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/docstore"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/utils/permissions"
)
type selectionProvider interface {
getSelectionByUuid(ctx context.Context, selectionUuid string) (bool, []*tree.Node, error)
deleteSelectionByUuid(ctx context.Context, selectionUuid string)
}
type ArchiveHandler struct {
AbstractHandler
selectionProvider selectionProvider
}
func NewArchiveHandler() *ArchiveHandler {
a := &ArchiveHandler{}
a.selectionProvider = a
return a
}
// Override the response of GetObject if it is sent on a folder key : create an archive on-the-fly.
func (a *ArchiveHandler) GetObject(ctx context.Context, node *tree.Node, requestData *GetRequestData) (io.ReadCloser, error) {
originalPath := node.Path
if ok, format, archivePath, innerPath := a.isArchivePath(originalPath); ok && len(innerPath) > 0 {
extractor := &ArchiveReader{Router: a.next}
statResp, _ := a.next.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: archivePath}})
archiveNode := statResp.Node
log.Logger(ctx).Debug("[ARCHIVE:GET] "+archivePath+" -- "+innerPath, zap.Any("archiveNode", archiveNode))
if format == "zip" {
return extractor.ReadChildZip(ctx, archiveNode, innerPath)
} else {
reader, writer := io.Pipe()
gzip := false
if format == "tar.gz" {
gzip = true
}
go func() {
extractor.ReadChildTar(ctx, gzip, writer, archiveNode, innerPath)
}()
return reader, nil
}
}
readCloser, err := a.next.GetObject(ctx, node, requestData)
if err != nil {
if selectionUuid := a.selectionFakeName(originalPath); selectionUuid != "" {
ok, nodes, er := a.selectionProvider.getSelectionByUuid(ctx, selectionUuid)
if er != nil {
return readCloser, er
}
if ok && len(nodes) > 0 {
ext := strings.Trim(path.Ext(originalPath), ".")
r, w := io.Pipe()
go func() {
defer w.Close()
defer func() {
// Delete selection after download
a.selectionProvider.deleteSelectionByUuid(context.Background(), selectionUuid)
}()
a.generateArchiveFromSelection(ctx, w, nodes, ext)
}()
return r, nil
}
} else if testFolder := a.archiveFolderName(originalPath); len(testFolder) > 0 {
r, w := io.Pipe()
go func() {
defer w.Close()
a.generateArchiveFromFolder(ctx, w, originalPath)
}()
return r, nil
}
}
return readCloser, err
}
// Override the response of ReadNode to create a fake stat for archive file
func (a *ArchiveHandler) ReadNode(ctx context.Context, in *tree.ReadNodeRequest, opts ...client.CallOption) (*tree.ReadNodeResponse, error) {
originalPath := in.Node.Path
if ok, format, archivePath, innerPath := a.isArchivePath(originalPath); ok && len(innerPath) > 0 {
log.Logger(ctx).Debug("[ARCHIVE:READ] " + originalPath + " => " + archivePath + " -- " + innerPath)
extractor := &ArchiveReader{Router: a.next}
statResp, _ := a.next.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: archivePath}})
archiveNode := statResp.Node
var statNode *tree.Node
var err error
if format == "zip" {
statNode, err = extractor.StatChildZip(ctx, archiveNode, innerPath)
} else {
gzip := false
if format == "tar.gz" {
gzip = true
}
statNode, err = extractor.StatChildTar(ctx, gzip, archiveNode, innerPath)
}
if err == nil {
if statNode.Size == 0 {
statNode.Size = -1
}
statNode.SetMeta(common.META_NAMESPACE_NODENAME, filepath.Base(statNode.Path))
}
return &tree.ReadNodeResponse{Node: statNode}, err
}
response, err := a.next.ReadNode(ctx, in, opts...)
if err != nil {
// Check if it's a selection Uuid
if selectionUuid := a.selectionFakeName(originalPath); selectionUuid != "" {
ok, nodes, er := a.selectionProvider.getSelectionByUuid(ctx, selectionUuid)
if er != nil {
return response, er
}
if ok && len(nodes) > 0 {
// Send a fake stat
fakeNode := &tree.Node{
Path: path.Dir(originalPath) + "selection.zip",
Type: tree.NodeType_LEAF,
Size: -1,
Etag: selectionUuid,
MTime: time.Now().Unix(),
MetaStore: map[string]string{"name": "selection.zip"},
}
return &tree.ReadNodeResponse{Node: fakeNode}, nil
}
} else if folderName := a.archiveFolderName(originalPath); folderName != "" {
// Check if it's a folder
fakeNode, err := a.archiveFakeStat(ctx, originalPath)
if err == nil && fakeNode != nil {
response = &tree.ReadNodeResponse{
Node: fakeNode,
}
return response, nil
}
}
}
return response, err
}
func (a *ArchiveHandler) ListNodes(ctx context.Context, in *tree.ListNodesRequest, opts ...client.CallOption) (tree.NodeProvider_ListNodesClient, error) {
if ok, format, archivePath, innerPath := a.isArchivePath(in.Node.Path); ok {
extractor := &ArchiveReader{Router: a.next}
statResp, e := a.next.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: archivePath}})
if e != nil {
return nil, e
}
archiveNode := statResp.Node
if in.Limit == 1 && innerPath == "" {
archiveNode.Type = tree.NodeType_COLLECTION
streamer := NewWrappingStreamer()
go func() {
defer streamer.Close()
log.Logger(ctx).Debug("[ARCHIVE:LISTNODE/READ]", zap.String("path", archiveNode.Path))
streamer.Send(&tree.ListNodesResponse{Node: archiveNode})
}()
return streamer, nil
}
log.Logger(ctx).Debug("[ARCHIVE:LIST] "+archivePath+" -- "+innerPath, zap.Any("archiveNode", archiveNode))
var children []*tree.Node
var err error
if format == "zip" {
children, err = extractor.ListChildrenZip(ctx, archiveNode, innerPath)
} else {
gzip := false
if format == "tar.gz" {
gzip = true
}
children, err = extractor.ListChildrenTar(ctx, gzip, archiveNode, innerPath)
}
streamer := NewWrappingStreamer()
if err != nil {
return streamer, err
}
go func() {
defer streamer.Close()
for _, child := range children {
log.Logger(ctx).Debug("[ARCHIVE:LISTNODE]", zap.String("path", child.Path))
streamer.Send(&tree.ListNodesResponse{Node: child})
}
}()
return streamer, nil
}
return a.next.ListNodes(ctx, in, opts...)
}
func (a *ArchiveHandler) isArchivePath(nodePath string) (ok bool, format string, archivePath string, innerPath string) {
formats := []string{"zip", "tar", "tar.gz"}
for _, f := range formats {
test := strings.SplitN(nodePath, "."+f+"/", 2)
if len(test) == 2 {
return true, f, test[0] + "." + f, test[1]
}
if strings.HasSuffix(nodePath, "."+f) {
return true, f, nodePath, ""
}
}
return false, "", "", ""
}
func (a *ArchiveHandler) selectionFakeName(nodePath string) string {
if strings.HasSuffix(nodePath, "-selection.zip") || strings.HasSuffix(nodePath, "-selection.tar") || strings.HasSuffix(nodePath, "-selection.tar.gz") {
fName := path.Base(nodePath)
return strings.TrimSuffix(strings.TrimSuffix(strings.TrimSuffix(fName, "-selection.zip"), "-selection.tar.gz"), "-selection.tar")
}
return ""
}
func (a *ArchiveHandler) archiveFolderName(nodePath string) string {
if strings.HasSuffix(nodePath, ".zip") || strings.HasSuffix(nodePath, ".tar") || strings.HasSuffix(nodePath, ".tar.gz") {
fName := strings.TrimSuffix(strings.TrimSuffix(strings.TrimSuffix(nodePath, ".zip"), ".gz"), ".tar")
return strings.Trim(fName, "/")
}
return ""
}
func (a *ArchiveHandler) archiveFakeStat(ctx context.Context, nodePath string) (node *tree.Node, e error) {
if noExt := a.archiveFolderName(nodePath); noExt != "" {
n, er := a.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: noExt}})
if er == nil && n != nil {
n.Node.Type = tree.NodeType_LEAF
n.Node.Path = nodePath
n.Node.Size = -1 // This will avoid a Content-Length discrepancy
n.Node.SetMeta(common.META_NAMESPACE_NODENAME, filepath.Base(nodePath))
log.Logger(ctx).Debug("This is a zip, sending folder info instead", zap.Any("node", n.Node))
return n.Node, nil
}
}
return nil, errors.NotFound(VIEWS_LIBRARY_NAME, "Could not find corresponding folder for archive")
}
func (a *ArchiveHandler) generateArchiveFromFolder(ctx context.Context, writer io.Writer, nodePath string) (bool, error) {
if noExt := a.archiveFolderName(nodePath); noExt != "" {
n, er := a.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: noExt}})
if er == nil && n != nil {
ext := strings.Trim(path.Ext(nodePath), ".")
err := a.generateArchiveFromSelection(ctx, writer, []*tree.Node{n.Node}, ext)
return true, err
}
}
return false, nil
}
// generateArchiveFromSelection Create a zip/tar/tar.gz on the fly
func (a *ArchiveHandler) generateArchiveFromSelection(ctx context.Context, writer io.Writer, selection []*tree.Node, format string) error {
archiveWriter := &ArchiveWriter{
Router: a,
}
var err error
if format == "zip" {
log.Logger(ctx).Debug("This is a zip, create a zip on the fly")
_, err = archiveWriter.ZipSelection(ctx, writer, selection)
} else if format == "tar" {
log.Logger(ctx).Debug("This is a tar, create a tar on the fly")
_, err = archiveWriter.TarSelection(ctx, writer, false, selection)
} else if format == "gz" {
log.Logger(ctx).Debug("This is a tar.gz, create a tar.gz on the fly")
_, err = archiveWriter.TarSelection(ctx, writer, true, selection)
}
return err
}
// getSelectionByUuid loads a selection stored in DocStore service by its id.
func (a *ArchiveHandler) getSelectionByUuid(ctx context.Context, selectionUuid string) (bool, []*tree.Node, error) {
var data []*tree.Node
dcClient := docstore.NewDocStoreClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_DOCSTORE, defaults.NewClient())
if resp, e := dcClient.GetDocument(ctx, &docstore.GetDocumentRequest{
StoreID: common.DOCSTORE_ID_SELECTIONS,
DocumentID: selectionUuid,
}); e == nil {
doc := resp.Document
username, _ := permissions.FindUserNameInContext(ctx)
if username != doc.Owner {
return false, data, errors.Forbidden("selection.forbidden", "this selection does not belong to you")
}
if er := json.Unmarshal([]byte(doc.Data), &data); er != nil {
return false, data, er
} else {
return true, data, nil
}
} else {
return false, data, nil
}
}
// deleteSelectionByUuid Delete selection
func (a *ArchiveHandler) deleteSelectionByUuid(ctx context.Context, selectionUuid string) {
dcClient := docstore.NewDocStoreClient(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_DOCSTORE, defaults.NewClient())
_, e := dcClient.DeleteDocuments(ctx, &docstore.DeleteDocumentsRequest{
StoreID: common.DOCSTORE_ID_SELECTIONS,
DocumentID: selectionUuid,
})
if e != nil {
log.Logger(ctx).Error("Could not delete selection")
} else {
log.Logger(ctx).Debug("Deleted selection after download " + selectionUuid)
}
}