|
1 | 1 | package types |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
4 | 7 | "encoding/json" |
| 8 | + "errors" |
5 | 9 | "io" |
| 10 | + "mime/multipart" |
| 11 | + "strconv" |
6 | 12 | "strings" |
7 | 13 | ) |
8 | 14 |
|
@@ -85,3 +91,72 @@ func (p *InitializeInput) Body() (io.Reader, error) { |
85 | 91 | func (p *InitializeInput) ParameterMap() map[string]string { |
86 | 92 | return map[string]string{} |
87 | 93 | } |
| 94 | + |
| 95 | +type AppendInput struct { |
| 96 | + accessToken string |
| 97 | + boundary string |
| 98 | + |
| 99 | + // Path parameter: The media identifier for the media to perform the append operation. |
| 100 | + MediaID string |
| 101 | + |
| 102 | + // The file to upload. |
| 103 | + Media io.Reader |
| 104 | + |
| 105 | + // An integer value representing the media upload segment. |
| 106 | + SegmentIndex int |
| 107 | +} |
| 108 | + |
| 109 | +func (p *AppendInput) SetAccessToken(token string) { |
| 110 | + p.accessToken = token |
| 111 | +} |
| 112 | + |
| 113 | +func (p *AppendInput) AccessToken() string { |
| 114 | + return p.accessToken |
| 115 | +} |
| 116 | + |
| 117 | +func (p *AppendInput) Boundary() string { |
| 118 | + return p.boundary |
| 119 | +} |
| 120 | + |
| 121 | +func (p *AppendInput) GenerateBoundary() string { |
| 122 | + h := sha256.New() |
| 123 | + h.Write([]byte(p.MediaID + strconv.Itoa(p.SegmentIndex))) |
| 124 | + boundary := hex.EncodeToString(h.Sum(nil)) |
| 125 | + p.boundary = boundary |
| 126 | + return boundary |
| 127 | +} |
| 128 | + |
| 129 | +func (p *AppendInput) ResolveEndpoint(endpointBase string) string { |
| 130 | + endpoint := strings.Replace(endpointBase, ":mediaID", p.MediaID, 1) |
| 131 | + return endpoint |
| 132 | +} |
| 133 | + |
| 134 | +func (p *AppendInput) Body() (io.Reader, error) { |
| 135 | + body := &bytes.Buffer{} |
| 136 | + writer := multipart.NewWriter(body) |
| 137 | + defer writer.Close() |
| 138 | + |
| 139 | + if p.boundary == "" { |
| 140 | + return nil, errors.New("boundary is not set") |
| 141 | + } |
| 142 | + writer.SetBoundary(p.boundary) |
| 143 | + |
| 144 | + part, err := writer.CreateFormFile("media", "media") |
| 145 | + if err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + |
| 149 | + if _, err := io.Copy(part, p.Media); err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + |
| 153 | + if err := writer.WriteField("segment_index", strconv.Itoa(p.SegmentIndex)); err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + |
| 157 | + return body, nil |
| 158 | +} |
| 159 | + |
| 160 | +func (p *AppendInput) ParameterMap() map[string]string { |
| 161 | + return map[string]string{} |
| 162 | +} |
0 commit comments