-
Notifications
You must be signed in to change notification settings - Fork 16
/
appstore.go
212 lines (180 loc) · 5.1 KB
/
appstore.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
package appstore
import (
"bytes"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"github.com/rs/zerolog/log"
"github.com/saucelabs/saucectl/internal/msg"
"github.com/saucelabs/saucectl/internal/requesth"
"github.com/saucelabs/saucectl/internal/storage"
)
// UploadResponse represents the response as is returned by the app store.
type UploadResponse struct {
Item Item `json:"item"`
}
// ListResponse represents the response as is returned by the app store.
type ListResponse struct {
Items []Item `json:"items"`
Links Links `json:"links"`
Page int `json:"page"`
PerPage int `json:"per_page"`
TotalItems int `json:"total_items"`
}
// Links represents the pagination information returned by the app store.
type Links struct {
Self string `json:"self"`
Prev string `json:"prev"`
Next string `json:"next"`
}
// Item represents the metadata about the uploaded file.
type Item struct {
ID string `json:"id"`
ETag string `json:"etag"`
}
// AppStore implements a remote file storage for storage.ProjectUploader.
// See https://wiki.saucelabs.com/display/DOCS/Application+Storage for more details.
type AppStore struct {
HTTPClient *http.Client
URL string
Username string
AccessKey string
}
// New returns an implementation for AppStore
func New(url, username, accessKey string, timeout time.Duration) *AppStore {
return &AppStore{
HTTPClient: &http.Client{Timeout: timeout},
URL: url,
Username: username,
AccessKey: accessKey,
}
}
// Upload uploads file to remote storage
func (s *AppStore) Upload(name string) (storage.ArtifactMeta, error) {
body, contentType, err := readFile(name)
if err != nil {
return storage.ArtifactMeta{}, err
}
request, err := createRequest(fmt.Sprintf("%s/v1/storage/upload", s.URL), s.Username, s.AccessKey, body, contentType)
if err != nil {
return storage.ArtifactMeta{}, err
}
resp, err := s.HTTPClient.Do(request)
if err != nil {
if err.(*url.Error).Timeout() {
msg.LogUploadTimeoutSuggestion()
return storage.ArtifactMeta{}, errors.New("failed to upload project")
}
return storage.ArtifactMeta{}, err
}
defer resp.Body.Close()
if resp.StatusCode != 201 {
b, err := io.ReadAll(resp.Body)
if err != nil {
return storage.ArtifactMeta{}, err
}
log.Error().Msgf("Failed to upload project. Invalid response %d, body: %v", resp.StatusCode, string(b))
return storage.ArtifactMeta{}, errors.New("failed to upload project")
}
var ur UploadResponse
if err := json.NewDecoder(resp.Body).Decode(&ur); err != nil {
return storage.ArtifactMeta{}, err
}
return storage.ArtifactMeta{ID: ur.Item.ID}, err
}
func readFile(fileName string) (*bytes.Buffer, string, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, "", err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
defer writer.Close()
part, err := writer.CreateFormFile("payload", filepath.Base(file.Name()))
if err != nil {
return nil, "", err
}
io.Copy(part, file)
return body, writer.FormDataContentType(), nil
}
func createRequest(url, username, accesskey string, body *bytes.Buffer, contentType string) (*http.Request, error) {
req, err := requesth.New(http.MethodPost, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
req.SetBasicAuth(username, accesskey)
return req, nil
}
// Find looks for a file having the same signature.
func (s *AppStore) Find(filename string) (storage.ArtifactMeta, error) {
if filename == "" {
return storage.ArtifactMeta{}, nil
}
hash, err := calculateBundleHash(filename)
if err != nil {
return storage.ArtifactMeta{}, err
}
queryString := ""
for {
request, err := createLocateRequest(fmt.Sprintf("%s/v1/storage/list", s.URL), s.Username, s.AccessKey, queryString)
if err != nil {
return storage.ArtifactMeta{}, err
}
lr, err := s.executeLocateRequest(request)
if err != nil {
return storage.ArtifactMeta{}, err
}
for _, item := range lr.Items {
if item.ETag == hash {
return storage.ArtifactMeta{ID: item.ID}, nil
}
}
queryString = lr.Links.Next
if queryString == "" {
return storage.ArtifactMeta{}, nil
}
}
}
func calculateBundleHash(filename string) (string, error) {
fs, err := os.Open(filename)
if err != nil {
return "", err
}
defer fs.Close()
hsh := md5.New()
if _, err := io.Copy(hsh, fs); err != nil {
return "", err
}
hash := fmt.Sprintf("%x", hsh.Sum(nil))
return hash, nil
}
func createLocateRequest(url, username, accesskey string, queryString string) (*http.Request, error) {
req, err := requesth.New(http.MethodGet, fmt.Sprintf("%s%s", url, queryString), nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(username, accesskey)
return req, nil
}
func (s *AppStore) executeLocateRequest(request *http.Request) (ListResponse, error) {
resp, err := s.HTTPClient.Do(request)
if err != nil {
return ListResponse{}, err
}
defer resp.Body.Close()
var lr ListResponse
if err := json.NewDecoder(resp.Body).Decode(&lr); err != nil {
return ListResponse{}, err
}
return lr, nil
}