Skip to content

Commit d3b1275

Browse files
authored
Fix file size upload param (#204)
* Fix file size upload param * Fix vendor * Fix? * Query file size only once * Revert unintentional change
1 parent 60e219b commit d3b1275

File tree

20 files changed

+729
-67
lines changed

20 files changed

+729
-67
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.22
1414
github.com/bitrise-io/go-xcode v1.0.18
1515
github.com/bitrise-io/go-xcode/v2 v2.0.0-alpha.44
16+
github.com/docker/go-units v0.5.0
1617
github.com/google/go-cmp v0.6.0
1718
github.com/gorilla/mux v1.8.0
1819
github.com/hashicorp/go-retryablehttp v0.7.7

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
5252
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5353
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5454
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55+
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
56+
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
5557
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa h1:RDBNVkRviHZtvDvId8XSGPu3rmpmSe+wKRcEWNgsfWU=
5658
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa/go.mod h1:KnogPXtdwXqoenmZCw6S+25EAm2MkxbG0deNDu4cbSA=
5759
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=

report/api/api.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net/http"
99
"net/http/httputil"
10+
"os"
1011

1112
"github.com/bitrise-io/go-utils/retry"
1213
"github.com/bitrise-io/go-utils/v2/log"
@@ -79,7 +80,15 @@ func (t *TestReportClient) CreateReport(params CreateReportParameters) (CreateRe
7980

8081
// UploadAsset ...
8182
func (t *TestReportClient) UploadAsset(url, path, contentType string) error {
82-
return uploaders.UploadArtifact(url, path, contentType)
83+
fileInfo, err := os.Stat(path)
84+
if err != nil {
85+
return err
86+
}
87+
artifact := uploaders.ArtifactArgs {
88+
Path: path,
89+
FileSize: fileInfo.Size(),
90+
}
91+
return uploaders.UploadArtifact(url, artifact, contentType)
8392
}
8493

8594
// FinishReport ...

uploaders/aabuploader.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func DeployAAB(item deployment.DeployableItem, artifacts []string, buildURL, tok
5757
info := androidartifact.ParseArtifactPath(pth)
5858

5959
aabInfoMap := map[string]interface{}{
60-
"file_size_bytes": fmt.Sprintf("%f", fileSize),
60+
"file_size_bytes": fmt.Sprintf("%d", fileSize),
6161
"app_info": appInfo,
6262
"module": info.Module,
6363
"product_flavour": info.ProductFlavour,
@@ -83,12 +83,16 @@ func DeployAAB(item deployment.DeployableItem, artifacts []string, buildURL, tok
8383
// ---
8484

8585
const AABContentType = "application/octet-stream aab"
86-
uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "android-apk", AABContentType)
86+
artifact := ArtifactArgs {
87+
Path: pth,
88+
FileSize: fileSize,
89+
}
90+
uploadURL, artifactID, err := createArtifact(buildURL, token, artifact, "android-apk", AABContentType)
8791
if err != nil {
8892
return ArtifactURLs{}, fmt.Errorf("failed to create apk artifact: %s %w", pth, err)
8993
}
9094

91-
if err := UploadArtifact(uploadURL, pth, AABContentType); err != nil {
95+
if err := UploadArtifact(uploadURL, artifact, AABContentType); err != nil {
9296
return ArtifactURLs{}, fmt.Errorf("failed to upload apk artifact, error: %s", err)
9397
}
9498

uploaders/apkuploader.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func DeployAPK(item deployment.DeployableItem, artifacts []string, buildURL, tok
4848
info := androidartifact.ParseArtifactPath(pth)
4949

5050
apkInfoMap := map[string]interface{}{
51-
"file_size_bytes": fmt.Sprintf("%f", fileSize),
51+
"file_size_bytes": fmt.Sprintf("%d", fileSize),
5252
"app_info": appInfo,
5353
"module": info.Module,
5454
"product_flavour": info.ProductFlavour,
@@ -67,12 +67,16 @@ func DeployAPK(item deployment.DeployableItem, artifacts []string, buildURL, tok
6767
// ---
6868

6969
const APKContentType = "application/vnd.android.package-archive"
70-
uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "android-apk", APKContentType)
70+
artifact := ArtifactArgs {
71+
Path: pth,
72+
FileSize: fileSize,
73+
}
74+
uploadURL, artifactID, err := createArtifact(buildURL, token, artifact, "android-apk", APKContentType)
7175
if err != nil {
7276
return ArtifactURLs{}, fmt.Errorf("failed to create apk artifact: %s %w", pth, err)
7377
}
7478

75-
if err := UploadArtifact(uploadURL, pth, APKContentType); err != nil {
79+
if err := UploadArtifact(uploadURL, artifact, APKContentType); err != nil {
7680
return ArtifactURLs{}, fmt.Errorf("failed to upload apk artifact, error: %s", err)
7781
}
7882

uploaders/common.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,37 @@ import (
1515
"time"
1616

1717
"github.com/bitrise-steplib/steps-deploy-to-bitrise-io/deployment"
18+
19+
"github.com/docker/go-units"
1820

1921
"github.com/bitrise-io/go-utils/log"
2022
"github.com/bitrise-io/go-utils/retry"
2123
"github.com/bitrise-io/go-utils/urlutil"
2224
)
2325

24-
// ArtifactURLs ...
2526
type ArtifactURLs struct {
2627
PublicInstallPageURL string
2728
PermanentDownloadURL string
2829
DetailsPageURL string
2930
}
3031

31-
// AppDeploymentMetaData ...
3232
type AppDeploymentMetaData struct {
3333
ArtifactInfo map[string]interface{}
3434
NotifyUserGroups string
3535
NotifyEmails string
3636
IsEnablePublicPage bool
3737
}
3838

39-
func createArtifact(buildURL, token, artifactPth, artifactType, contentType string) (string, string, error) {
39+
type ArtifactArgs struct {
40+
Path string
41+
FileSize int64 // bytes
42+
}
43+
44+
func createArtifact(buildURL, token string, artifact ArtifactArgs, artifactType, contentType string) (string, string, error) {
4045
// create form data
41-
artifactName := filepath.Base(artifactPth)
42-
fileSize, err := fileSizeInBytes(artifactPth)
43-
if err != nil {
44-
return "", "", fmt.Errorf("failed to get file size, error: %s", err)
45-
}
46+
artifactName := filepath.Base(artifact.Path)
4647

47-
megaBytes := fileSize / 1024.0 / 1024.0
48-
roundedMegaBytes := int(roundPlus(megaBytes, 2))
49-
50-
if roundedMegaBytes < 1 {
51-
log.Printf("file size: %dB", int(fileSize))
52-
} else {
53-
log.Printf("file size: %dMB", roundedMegaBytes)
54-
}
48+
log.Printf("file size: %s", units.BytesSize(float64(artifact.FileSize)))
5549

5650
if strings.TrimSpace(token) == "" {
5751
return "", "", fmt.Errorf("provided API token is empty")
@@ -62,7 +56,7 @@ func createArtifact(buildURL, token, artifactPth, artifactType, contentType stri
6256
"title": {artifactName},
6357
"filename": {artifactName},
6458
"artifact_type": {artifactType},
65-
"file_size_bytes": {fmt.Sprintf("%d", int(fileSize))},
59+
"file_size_bytes": {fmt.Sprintf("%d", artifact.FileSize)},
6660
"content_type": {contentType},
6761
}
6862
// ---
@@ -137,14 +131,13 @@ func createArtifact(buildURL, token, artifactPth, artifactType, contentType stri
137131
return artifactResponse.UploadURL, fmt.Sprintf("%d", artifactResponse.ID), nil
138132
}
139133

140-
// UploadArtifact ...
141-
func UploadArtifact(uploadURL, artifactPth, contentType string) error {
134+
func UploadArtifact(uploadURL string, artifact ArtifactArgs, contentType string) error {
142135
netClient := &http.Client{
143136
Timeout: 10 * time.Minute,
144137
}
145138

146139
return retry.Times(3).Wait(5).Try(func(attempt uint) error {
147-
file, err := os.Open(artifactPth)
140+
file, err := os.Open(artifact.Path)
148141
if err != nil {
149142
return fmt.Errorf("failed to open artifact, error: %s", err)
150143
}
@@ -154,14 +147,9 @@ func UploadArtifact(uploadURL, artifactPth, contentType string) error {
154147
}
155148
}()
156149

157-
fileInfo, err := file.Stat()
158-
if err != nil {
159-
return fmt.Errorf("failed to get file info for %s, error: %s", artifactPth, err)
160-
}
161-
162150
// Initializes request body to nil to send a Content-Length of 0: https://github.com/golang/go/issues/20257#issuecomment-299509391
163151
var reqBody io.Reader
164-
if fileInfo.Size() > 0 {
152+
if artifact.FileSize > 0 {
165153
reqBody = io.NopCloser(file)
166154
}
167155

@@ -174,8 +162,8 @@ func UploadArtifact(uploadURL, artifactPth, contentType string) error {
174162
request.Header.Add("Content-Type", contentType)
175163
}
176164

177-
request.Header.Add("X-Upload-Content-Length", strconv.FormatInt(fileInfo.Size(), 10)) // header used by Google Cloud Storage signed URLs
178-
request.ContentLength = fileInfo.Size()
165+
request.Header.Add("X-Upload-Content-Length", strconv.FormatInt(artifact.FileSize, 10)) // header used by Google Cloud Storage signed URLs
166+
request.ContentLength = artifact.FileSize
179167

180168
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
181169
defer cancel()

uploaders/common_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package uploaders
22

33
import (
4-
"os"
5-
"io"
64
"image"
75
"image/png"
6+
"io"
87
"math/rand"
98
"net/http"
109
"net/http/httptest"
10+
"os"
1111
"path/filepath"
1212
"strconv"
1313
"testing"
14+
15+
"github.com/stretchr/testify/require"
1416
)
1517

1618
func Test_uploadArtifact(t *testing.T) {
@@ -90,7 +92,13 @@ func Test_uploadArtifact(t *testing.T) {
9092
}
9193
for _, tt := range tests {
9294
t.Run(tt.name, func(t *testing.T) {
93-
if err := UploadArtifact(tt.uploadURL, tt.artifactPth, tt.contentType); (err != nil) != tt.wantErr {
95+
fileInfo, err := os.Stat(tt.artifactPth)
96+
require.NoError(t, err)
97+
artifact := ArtifactArgs {
98+
Path: tt.artifactPth,
99+
FileSize: fileInfo.Size(),
100+
}
101+
if err := UploadArtifact(tt.uploadURL, artifact, tt.contentType); (err != nil) != tt.wantErr {
94102
t.Errorf("UploadArtifact() error = %v, wantErr %v", err, tt.wantErr)
95103
}
96104
})

uploaders/fileuploader.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ import (
88

99
// DeployFile ...
1010
func DeployFile(item deployment.DeployableItem, buildURL, token string) (ArtifactURLs, error) {
11-
pth := item.Path
12-
uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "file", "")
11+
fileSize, err := fileSizeInBytes(item.Path)
1312
if err != nil {
14-
return ArtifactURLs{}, fmt.Errorf("failed to create file artifact: %s %w", pth, err)
13+
return ArtifactURLs{}, fmt.Errorf("get file size: %s", err)
14+
}
15+
artifact := ArtifactArgs {
16+
Path: item.Path,
17+
FileSize: fileSize,
18+
}
19+
uploadURL, artifactID, err := createArtifact(buildURL, token, artifact, "file", "")
20+
if err != nil {
21+
return ArtifactURLs{}, fmt.Errorf("create file artifact: %s %w", artifact.Path, err)
1522
}
1623

17-
if err := UploadArtifact(uploadURL, pth, ""); err != nil {
24+
if err := UploadArtifact(uploadURL, artifact, ""); err != nil {
1825
return ArtifactURLs{}, fmt.Errorf("failed to upload file artifact, error: %s", err)
1926
}
2027

uploaders/ipauploader.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,24 @@ func DeployIPA(item deployment.DeployableItem, buildURL, token, notifyUserGroups
3232
}
3333

3434
ipaInfoMap := map[string]interface{}{
35-
"file_size_bytes": fmt.Sprintf("%f", fileSize),
35+
"file_size_bytes": fmt.Sprintf("%d", fileSize),
3636
"app_info": appInfo,
3737
"provisioning_info": provisioningInfo,
3838
}
3939

4040
logger.Printf("ipa infos: %v", appInfo)
4141

4242
const IPAContentType = "application/octet-stream ipa"
43-
uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "ios-ipa", IPAContentType)
43+
artifact := ArtifactArgs {
44+
Path: pth,
45+
FileSize: fileSize,
46+
}
47+
uploadURL, artifactID, err := createArtifact(buildURL, token, artifact, "ios-ipa", IPAContentType)
4448
if err != nil {
4549
return ArtifactURLs{}, fmt.Errorf("failed to create ipa artifact from %s: %w", pth, err)
4650
}
4751

48-
if err := UploadArtifact(uploadURL, pth, IPAContentType); err != nil {
52+
if err := UploadArtifact(uploadURL, artifact, IPAContentType); err != nil {
4953
return ArtifactURLs{}, fmt.Errorf("failed to upload ipa (%s): %w", pth, err)
5054
}
5155

uploaders/utils.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
package uploaders
22

33
import (
4-
"fmt"
5-
"math"
6-
7-
"github.com/bitrise-io/go-utils/pathutil"
4+
"os"
85
)
96

10-
func round(f float64) float64 {
11-
return math.Floor(f + .5)
12-
}
13-
14-
func roundPlus(f float64, places int) float64 {
15-
shift := math.Pow(10, float64(places))
16-
return round(f*shift) / shift
17-
}
18-
19-
func fileSizeInBytes(pth string) (float64, error) {
20-
info, exist, err := pathutil.PathCheckAndInfos(pth)
7+
func fileSizeInBytes(pth string) (int64, error) {
8+
finfo, err := os.Stat(pth)
219
if err != nil {
2210
return 0, err
2311
}
24-
if !exist {
25-
return 0, fmt.Errorf("file not exist at: %s", pth)
26-
}
27-
return float64(info.Size()), nil
12+
return finfo.Size(), nil
2813
}

0 commit comments

Comments
 (0)