@@ -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 ...
2526type ArtifactURLs struct {
2627 PublicInstallPageURL string
2728 PermanentDownloadURL string
2829 DetailsPageURL string
2930}
3031
31- // AppDeploymentMetaData ...
3232type 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 ()
0 commit comments