-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
257522b
commit 3903149
Showing
6 changed files
with
176 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package appstore | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
func (c *Client) uploadAsset(ctx context.Context, asset *Asset, data []byte) (*Asset, error) { | ||
wg := sync.WaitGroup{} | ||
errs := make(chan error, len(asset.Attr.Operations)) | ||
|
||
for _, op := range asset.Attr.Operations { | ||
wg.Add(1) | ||
|
||
go func(op UploadOperation) { | ||
defer wg.Done() | ||
|
||
buff := bytes.NewBuffer(data[op.Offset : op.Offset+op.Length]) | ||
err := c.uploadAssetChunk(ctx, op, buff) | ||
if err != nil { | ||
errs <- fmt.Errorf("failed to upload asset chunk: %w", err) | ||
} | ||
}(op) | ||
} | ||
|
||
wg.Wait() | ||
close(errs) | ||
|
||
if len(errs) > 0 { | ||
return nil, fmt.Errorf("failed to upload asset: %w", <-errs) | ||
} | ||
|
||
if err := c.commitAsset(ctx, asset); err != nil { | ||
return nil, fmt.Errorf("failed to commit asset: %w", err) | ||
} | ||
|
||
asset, err := doGet[Asset](c, ctx, asset.Links.Self) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get asset: %w", err) | ||
} | ||
|
||
return asset, nil | ||
} | ||
|
||
func (c *Client) uploadAssetChunk(ctx context.Context, op UploadOperation, data io.Reader) error { | ||
req, err := http.NewRequestWithContext(ctx, op.Method, op.URL, data) | ||
if err != nil { | ||
return fmt.Errorf("failed to create request: %w", err) | ||
} | ||
|
||
for _, h := range op.Headers { | ||
req.Header.Set(h.Name, h.Value) | ||
} | ||
|
||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to send request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type commitAssetRequest struct { | ||
Data struct { | ||
ID string `json:"id"` | ||
Type string `json:"type"` | ||
Attr struct { | ||
Uploaded bool `json:"uploaded"` | ||
Checksum string `json:"sourceFileChecksum,omitempty"` | ||
} `json:"attributes"` | ||
} `json:"data"` | ||
} | ||
|
||
func (c *Client) commitAsset(ctx context.Context, asset *Asset) error { | ||
commit := commitAssetRequest{} | ||
commit.Data.ID = asset.ID | ||
commit.Data.Type = asset.Type | ||
commit.Data.Attr.Uploaded = true | ||
|
||
body, err := json.Marshal(commit) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal request: %w", err) | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, asset.Links.Self, bytes.NewReader(body)) | ||
if err != nil { | ||
return fmt.Errorf("failed to create request: %w", err) | ||
} | ||
|
||
token, err := c.tokenSource.Token() | ||
if err != nil { | ||
return fmt.Errorf("failed to get token: %w", err) | ||
} | ||
|
||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to send request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
return nil | ||
} |