-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets_upload.go
127 lines (102 loc) · 2.99 KB
/
assets_upload.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
package appstore
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"sync"
)
func (c *Client) uploadAsset(ctx context.Context, asset *Resource[Asset], data []byte) (*Resource[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.NewReader(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, data); err != nil {
return nil, fmt.Errorf("failed to commit asset: %w", err)
}
resp, err := doGet[Asset](c, ctx, asset.Links.Self)
if err != nil {
return nil, fmt.Errorf("failed to get asset: %w", err)
}
return resp, 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 *Resource[Asset], data []byte) error {
commit := commitAssetRequest{}
commit.Data.ID = asset.ID
commit.Data.Type = asset.Type
commit.Data.Attr.Uploaded = true
// achievements images do not require checksum
if asset.Type != resourceTypeAchievementImages {
checksum := md5.Sum(data)
commit.Data.Attr.Checksum = hex.EncodeToString(checksum[:])
}
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
}