-
Notifications
You must be signed in to change notification settings - Fork 180
/
archive-writer.go
226 lines (188 loc) · 5.68 KB
/
archive-writer.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
/*
* 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 (
"archive/tar"
"archive/zip"
"compress/gzip"
"context"
"io"
"path/filepath"
"strings"
"time"
"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/proto/tree"
)
type ArchiveWriter struct {
Router Handler
}
type walkFunction func(node *tree.Node) error
func (w *ArchiveWriter) walkObjectsWithCallback(ctx context.Context, nodePath string, cb walkFunction) error {
r, e := w.Router.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: nodePath}})
if e != nil {
return e
}
if r.Node.IsLeaf() {
cb(r.Node)
return nil
}
lNodeClient, err := w.Router.ListNodes(ctx, &tree.ListNodesRequest{
Node: &tree.Node{
Path: nodePath,
},
Recursive: true,
Limit: 0,
}, client.WithRequestTimeout(6*time.Hour))
if err != nil {
return err
}
defer lNodeClient.Close()
for {
clientResponse, err := lNodeClient.Recv()
if clientResponse == nil || err != nil {
break
}
n := clientResponse.Node
// Ignore folders
if !n.IsLeaf() || strings.HasSuffix(n.Path, common.PYDIO_SYNC_HIDDEN_FILE_META) {
continue
}
e := cb(n)
if e != nil {
log.Logger(ctx).Error("Error trying to add file to archive", zap.String("path", n.Path))
return e
}
}
return nil
}
func (w *ArchiveWriter) commonRoot(nodes []*tree.Node) string {
// TODO
// Assume nodes have same parent for now
if len(nodes) == 1 && !nodes[0].IsLeaf() {
return nodes[0].Path
} else {
return filepath.Dir(nodes[0].Path)
}
}
func (w *ArchiveWriter) ZipSelection(ctx context.Context, output io.Writer, nodes []*tree.Node, logsChannels ...chan string) (int64, error) {
z := zip.NewWriter(output)
defer z.Close()
var totalSizeWritten int64
parentRoot := w.commonRoot(nodes)
log.Logger(ctx).Debug("ZipSelection", zap.String("parent", parentRoot), zap.Any("selection", nodes))
for _, node := range nodes {
w.walkObjectsWithCallback(ctx, node.Path, func(n *tree.Node) error {
if n.Size <= 0 {
return nil
}
internalPath := strings.TrimPrefix(n.Path, parentRoot)
log.Logger(ctx).Debug("Adding file to archive: ", zap.String("path", internalPath), zap.Any("node", n))
header := &zip.FileHeader{
Name: internalPath,
Method: zip.Deflate,
UncompressedSize64: uint64(n.Size),
}
header.SetMode(0777)
header.SetModTime(n.GetModTime())
zW, e := z.CreateHeader(header)
if e != nil {
log.Logger(ctx).Error("Error while creating path", zap.String("path", internalPath), zap.Error(e))
return e
}
r, e1 := w.Router.GetObject(ctx, n, &GetRequestData{StartOffset: 0, Length: -1})
if e1 != nil {
log.Logger(ctx).Error("Error while getting object", zap.String("path", n.Path), zap.Error(e1))
return e1
}
defer r.Close()
written, e2 := io.Copy(zW, r)
if e2 != nil {
log.Logger(ctx).Error("Error while copying streams", zap.Error(e2))
return e2
}
totalSizeWritten += written
if len(logsChannels) > 0 {
logsChannels[0] <- "File " + internalPath + " added to archive"
}
return nil
})
}
log.Logger(ctx).Debug("Total Size Written", zap.Int64("size", totalSizeWritten))
return totalSizeWritten, nil
}
func (w *ArchiveWriter) TarSelection(ctx context.Context, output io.Writer, gzipFile bool, nodes []*tree.Node, logsChannel ...chan string) (int64, error) {
var tw *tar.Writer
var totalSizeWritten int64
if gzipFile {
// set up the gzip writer
gw := gzip.NewWriter(output)
defer gw.Close()
tw = tar.NewWriter(gw)
defer tw.Close()
} else {
tw = tar.NewWriter(output)
defer tw.Close()
}
parentRoot := w.commonRoot(nodes)
for _, node := range nodes {
err := w.walkObjectsWithCallback(ctx, node.Path, func(n *tree.Node) error {
internalPath := strings.TrimPrefix(n.Path, parentRoot)
if n.Size <= 0 {
return nil
}
header := &tar.Header{
Name: internalPath,
ModTime: n.GetModTime(),
Size: n.Size,
Mode: 0777,
}
if !n.IsLeaf() {
header.Typeflag = tar.TypeDir
} else {
header.Typeflag = tar.TypeReg
}
log.Logger(ctx).Debug("Adding file to archive: ", zap.String("path", internalPath), zap.Any("node", n))
e := tw.WriteHeader(header)
if e != nil {
log.Logger(ctx).Error("Error while creating path", zap.String("path", internalPath), zap.Error(e))
return e
}
reader, e1 := w.Router.GetObject(ctx, n, &GetRequestData{StartOffset: 0, Length: -1})
defer reader.Close()
if e1 != nil {
log.Logger(ctx).Error("Error while getting object and writing to tarball", zap.String("path", internalPath), zap.Error(e1))
return e1
}
size, _ := io.Copy(tw, reader)
totalSizeWritten += size
if len(logsChannel) > 0 {
logsChannel[0] <- "File " + internalPath + " added to archive"
}
return nil
})
if err != nil {
return totalSizeWritten, err
}
}
return totalSizeWritten, nil
}