-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_command.go
205 lines (164 loc) · 5.11 KB
/
in_command.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
package in
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"github.com/frodenas/gcs-resource"
"github.com/frodenas/gcs-resource/versions"
)
type InCommand struct {
gcsClient gcsresource.GCSClient
}
func NewInCommand(gcsClient gcsresource.GCSClient) *InCommand {
return &InCommand{
gcsClient: gcsClient,
}
}
func (command *InCommand) Run(destinationDir string, request InRequest) (InResponse, error) {
if ok, message := request.Source.IsValid(); !ok {
return InResponse{}, errors.New(message)
}
err := command.createDirectory(destinationDir)
if err != nil {
return InResponse{}, err
}
if request.Source.Regexp != "" {
return command.inByRegex(destinationDir, request)
} else {
return command.inByVersionedFile(destinationDir, request)
}
}
func (command *InCommand) createDirectory(destinationDir string) error {
return os.MkdirAll(destinationDir, 0755)
}
func (command *InCommand) inByRegex(destinationDir string, request InRequest) (InResponse, error) {
bucketName := request.Source.Bucket
objectPath, err := command.pathToDownload(request)
if err != nil {
return InResponse{}, err
}
localPath := filepath.Join(destinationDir, filepath.Base(objectPath))
if err := command.downloadFile(bucketName, objectPath, 0, localPath); err != nil {
return InResponse{}, err
}
if request.Params.Unpack {
if err := command.unpackFile(localPath); err != nil {
return InResponse{}, err
}
}
version, ok := versions.Extract(objectPath, request.Source.Regexp)
if ok {
err := command.writeVersionFile(version.VersionNumber, destinationDir)
if err != nil {
return InResponse{}, err
}
}
url, err := command.gcsClient.URL(bucketName, objectPath, 0)
if err != nil {
return InResponse{}, err
}
if err := command.writeURLFile(url, destinationDir); err != nil {
return InResponse{}, err
}
return InResponse{
Version: gcsresource.Version{
Path: objectPath,
},
Metadata: command.metadata(objectPath, url),
}, nil
}
func (command *InCommand) pathToDownload(request InRequest) (string, error) {
if request.Version.Path != "" {
return request.Version.Path, nil
}
extractions := versions.GetBucketObjectVersions(command.gcsClient, request.Source)
if len(extractions) == 0 {
return "", errors.New("no extractions could be found - is your regexp correct?")
}
lastExtraction := extractions[len(extractions)-1]
return lastExtraction.Path, nil
}
func (command *InCommand) inByVersionedFile(destinationDir string, request InRequest) (InResponse, error) {
bucketName := request.Source.Bucket
objectPath := request.Source.VersionedFile
generation, err := request.Version.GenerationValue()
if err != nil {
return InResponse{}, err
}
localPath := filepath.Join(destinationDir, filepath.Base(objectPath))
if err := command.downloadFile(bucketName, objectPath, generation, localPath); err != nil {
return InResponse{}, err
}
if request.Params.Unpack {
if err := command.unpackFile(localPath); err != nil {
return InResponse{}, err
}
}
if err := command.writeGenerationFile(generation, destinationDir); err != nil {
return InResponse{}, err
}
url, err := command.gcsClient.URL(bucketName, objectPath, generation)
if err != nil {
return InResponse{}, err
}
if err := command.writeURLFile(url, destinationDir); err != nil {
return InResponse{}, err
}
return InResponse{
Version: gcsresource.Version{
Generation: fmt.Sprintf("%d", generation),
},
Metadata: command.metadata(objectPath, url),
}, nil
}
func (command *InCommand) writeVersionFile(version string, destinationDir string) error {
return ioutil.WriteFile(filepath.Join(destinationDir, "version"), []byte(version), 0644)
}
func (command *InCommand) writeGenerationFile(generation int64, destinationDir string) error {
return ioutil.WriteFile(filepath.Join(destinationDir, "generation"), []byte(strconv.FormatInt(generation, 10)), 0644)
}
func (command *InCommand) writeURLFile(url string, destinationDir string) error {
return ioutil.WriteFile(filepath.Join(destinationDir, "url"), []byte(url), 0644)
}
func (command *InCommand) downloadFile(bucketName string, objectPath string, generation int64, localPath string) error {
return command.gcsClient.DownloadFile(
bucketName,
objectPath,
generation,
localPath,
)
}
func (command *InCommand) unpackFile(sourcePath string) error {
var (
errorMessage = "failed to extract '%s' with the 'params.unpack' option enabled: %s"
fileName = filepath.Base(sourcePath)
)
mimeType, err := getMimeType(sourcePath)
if err != nil {
return fmt.Errorf(errorMessage, fileName, err)
}
if !isSupportedMimeType(mimeType) {
return fmt.Errorf(errorMessage, fileName, "unsupported MIME type "+mimeType)
}
if err := unpack(mimeType, sourcePath); err != nil {
return fmt.Errorf(errorMessage, fileName, err)
}
return nil
}
func (command *InCommand) metadata(objectPath string, url string) []gcsresource.MetadataPair {
objectFilename := filepath.Base(objectPath)
metadata := []gcsresource.MetadataPair{
gcsresource.MetadataPair{
Name: "filename",
Value: objectFilename,
},
gcsresource.MetadataPair{
Name: "url",
Value: url,
},
}
return metadata
}