-
Notifications
You must be signed in to change notification settings - Fork 29
/
copy.go
288 lines (257 loc) · 8.67 KB
/
copy.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
package storage
import (
"archive/tar"
"archive/zip"
"bytes"
"fmt"
"github.com/viant/toolbox"
"io"
"io/ioutil"
"path"
"strings"
)
type CopyHandler func(sourceObject Object, source io.Reader, destinationService Service, destinationURL string) error
type ModificationHandler func(reader io.ReadCloser) (io.ReadCloser, error)
func urlPath(URL string) string {
var result = URL
schemaPosition := strings.Index(URL, "://")
if schemaPosition != -1 {
result = string(URL[schemaPosition+3:])
}
pathRoot := strings.Index(result, "/")
if pathRoot > 0 {
result = string(result[pathRoot:])
}
if strings.HasSuffix(result, "/") {
result = string(result[:len(result)-1])
}
return result
}
func truncatePath(path string) string {
if len(path) <= 1 {
return path
}
if strings.HasSuffix(path, "/") {
return string(path[:len(path)-1])
}
return path
}
func copyStorageContent(sourceService Service, sourceURL string, destinationService Service, destinationURL string, modifyContentHandler ModificationHandler, subPath string, copyHandler CopyHandler) error {
sourceListURL := sourceURL
if subPath != "" {
sourceListURL = toolbox.URLPathJoin(sourceURL, subPath)
}
objects, err := sourceService.List(sourceListURL)
if err != nil {
return err
}
var objectRelativePath string
sourceURLPath := urlPath(sourceURL)
for _, object := range objects {
var objectURLPath = urlPath(object.URL())
if object.IsFolder() {
if truncatePath(sourceURLPath) == truncatePath(objectURLPath) {
continue
}
if subPath != "" && objectURLPath == toolbox.URLPathJoin(sourceURLPath, subPath) {
continue
}
}
if len(objectURLPath) > len(sourceURLPath) {
objectRelativePath = objectURLPath[len(sourceURLPath):]
if strings.HasPrefix(objectRelativePath, "/") {
objectRelativePath = string(objectRelativePath[1:])
}
}
var destinationObjectURL = destinationURL
if objectRelativePath != "" {
destinationObjectURL = toolbox.URLPathJoin(destinationURL, objectRelativePath)
}
if object.IsContent() {
reader, err := sourceService.Download(object)
if err != nil {
err = fmt.Errorf("unable download, %v -> %v, %v", object.URL(), destinationObjectURL, err)
return err
}
defer reader.Close()
if modifyContentHandler != nil {
content, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
reader = ioutil.NopCloser(bytes.NewReader(content))
reader, err = modifyContentHandler(reader)
if err != nil {
err = fmt.Errorf("unable modify content, %v %v %v", object.URL(), destinationObjectURL, err)
return err
}
}
if subPath == "" {
_, sourceName := path.Split(object.URL())
_, destinationName := path.Split(destinationURL)
if strings.HasSuffix(destinationObjectURL, "/") {
destinationObjectURL = toolbox.URLPathJoin(destinationObjectURL, sourceName)
} else {
destinationObject, _ := destinationService.StorageObject(destinationObjectURL)
if destinationObject != nil && destinationObject.IsFolder() {
destinationObjectURL = toolbox.URLPathJoin(destinationObjectURL, sourceName)
} else if destinationName != sourceName {
if !strings.Contains(destinationName, ".") {
destinationObjectURL = toolbox.URLPathJoin(destinationURL, sourceName)
}
}
}
}
err = copyHandler(object, reader, destinationService, destinationObjectURL)
if err != nil {
return err
}
} else {
err = copyStorageContent(sourceService, sourceURL, destinationService, destinationURL, modifyContentHandler, objectRelativePath, copyHandler)
if err != nil {
return err
}
}
}
return nil
}
func copySourceToDestination(sourceObject Object, reader io.Reader, destinationService Service, destinationURL string) error {
err := destinationService.Upload(destinationURL, reader)
if err != nil {
err = fmt.Errorf("unable upload, %v %v %v", sourceObject.URL(), destinationURL, err)
}
return err
}
func getArchiveCopyHandler(archive *zip.Writer, parentURL string) CopyHandler {
return func(sourceObject Object, reader io.Reader, destinationService Service, destinationURL string) error {
var _, relativePath = toolbox.URLSplit(destinationURL)
if destinationURL != parentURL {
relativePath = strings.Replace(destinationURL, parentURL, "", 1)
}
header, err := zip.FileInfoHeader(sourceObject.FileInfo())
if err != nil {
return err
}
header.Method = zip.Deflate
header.Name = relativePath
writer, err := archive.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, reader)
return err
}
}
//Copy downloads objects from source URL to upload them to destination URL.
func Copy(sourceService Service, sourceURL string, destinationService Service, destinationURL string, modifyContentHandler ModificationHandler, copyHandler CopyHandler) (err error) {
if copyHandler == nil {
copyHandler = copySourceToDestination
}
if strings.HasSuffix(sourceURL, "//") {
sourceURL = string(sourceURL[:len(sourceURL)-1])
}
err = copyStorageContent(sourceService, sourceURL, destinationService, destinationURL, modifyContentHandler, "", copyHandler)
if err != nil {
err = fmt.Errorf("failed to copy %v -> %v: %v", sourceURL, destinationURL, err)
}
return err
}
//Archive archives supplied URL assets into zip writer
func Archive(service Service, URL string, writer *zip.Writer) error {
memService := NewMemoryService()
var destURL = "mem:///dev/nul"
return Copy(service, URL, memService, destURL, nil, getArchiveCopyHandler(writer, destURL))
}
func getArchiveCopyHandlerWithFilter(archive *zip.Writer, parentURL string, predicate func(candidate Object) bool) CopyHandler {
return func(sourceObject Object, reader io.Reader, destinationService Service, destinationURL string) error {
if !predicate(sourceObject) {
return nil
}
var _, relativePath = toolbox.URLSplit(destinationURL)
if destinationURL != parentURL {
relativePath = strings.Replace(destinationURL, parentURL, "", 1)
}
header, err := zip.FileInfoHeader(sourceObject.FileInfo())
if err != nil {
return err
}
header.Method = zip.Store
if strings.HasPrefix(relativePath, "/") {
relativePath = string(relativePath[1:])
}
header.Name = relativePath
writer, err := archive.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, reader)
return err
}
}
//Archive archives supplied URL assets into zip writer with supplied filter
func ArchiveWithFilter(service Service, URL string, writer *zip.Writer, predicate func(candidate Object) bool) error {
memService := NewMemoryService()
var destURL = "mem:///dev/nul"
return Copy(service, URL, memService, destURL, nil, getArchiveCopyHandlerWithFilter(writer, destURL, predicate))
}
func getTarCopyHandler(archive *tar.Writer, destParentURL, parentURL string, dirs map[string]bool) CopyHandler {
if strings.HasSuffix(parentURL, "/") {
parentURL = string(parentURL[:len(parentURL)-2])
}
_, root := path.Split(destParentURL)
if root == "." {
root = ""
}
return func(sourceObject Object, reader io.Reader, destinationService Service, destinationURL string) error {
var _, relativePath = toolbox.URLSplit(destinationURL)
if destinationURL != parentURL {
relativePath = strings.Replace(destinationURL, parentURL, "", 1)
}
if strings.HasPrefix(relativePath, "/") {
relativePath = string(relativePath[1:])
}
relativePath = path.Join(root, relativePath)
parent, _ := path.Split(relativePath)
if parent != "" && !dirs[parent] {
tarHeader := &tar.Header{
Name: parent,
Size: int64(0),
Mode: int64(sourceObject.FileInfo().Mode()),
ModTime: sourceObject.FileInfo().ModTime(),
}
if err := archive.WriteHeader(tarHeader); err != nil {
return fmt.Errorf(" unable to write tar header, %v", err)
}
dirs[parent] = true
}
contents := new(bytes.Buffer)
if _, err := io.Copy(contents, reader); err != nil {
return err
}
data := contents.Bytes()
tarHeader := &tar.Header{
Name: relativePath,
Size: int64(len(data)),
Mode: int64(sourceObject.FileInfo().Mode()),
ModTime: sourceObject.FileInfo().ModTime(),
}
if err := archive.WriteHeader(tarHeader); err != nil {
return fmt.Errorf(" unable to write tar header, %v", err)
}
if _, err := archive.Write(data); err != nil {
return fmt.Errorf(" unable to write tar content, %v", err)
}
return nil
}
}
//Tar tar archives supplied URL assets into zip writer
func Tar(service Service, URL string, writer *tar.Writer, includeOwnerDir bool) error {
memService := NewMemoryService()
var destURL = "mem:///dev/nul"
var dirs = make(map[string]bool)
ownerDir := ""
if includeOwnerDir {
ownerDir = URL
}
return Copy(service, URL, memService, destURL, nil, getTarCopyHandler(writer, ownerDir, destURL, dirs))
}