diff options
author | Mike Crute <mike@crute.us> | 2023-12-31 12:12:11 -0800 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2023-12-31 12:12:11 -0800 |
commit | a2eff483b8967fa15a589f548abe0a53d3af47e3 (patch) | |
tree | 0b29fdbf7c8c0af34b6d94829ae9919f4711eaf1 | |
parent | 6241d18c5d4faee3c0806a3151edde15772c6eac (diff) | |
download | golib-a2eff483b8967fa15a589f548abe0a53d3af47e3.tar.bz2 golib-a2eff483b8967fa15a589f548abe0a53d3af47e3.tar.xz golib-a2eff483b8967fa15a589f548abe0a53d3af47e3.zip |
-rw-r--r-- | image/webp/client.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/image/webp/client.go b/image/webp/client.go index aabf240..5037f7c 100644 --- a/image/webp/client.go +++ b/image/webp/client.go | |||
@@ -181,3 +181,36 @@ func (c *WebPConvertClient) ConvertBytes(ctx context.Context, img []byte, qualit | |||
181 | 181 | ||
182 | return webp, nil | 182 | return webp, nil |
183 | } | 183 | } |
184 | |||
185 | // ConvertBytesStreaming takes an image in any format and sends it to | ||
186 | // the conversion server. It returns either an error or the webp encoded | ||
187 | // bytes as a ReadCloser from the input image. The caller must Close the | ||
188 | // returned ReadCloser or it will leak. | ||
189 | func (c *WebPConvertClient) ConvertBytesStreaming(ctx context.Context, img []byte, quality int, lossless, exact bool) (io.ReadCloser, error) { | ||
190 | if c.c == nil { | ||
191 | return nil, fmt.Errorf("WebPConvertClient has not been started") | ||
192 | } | ||
193 | |||
194 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://unix/", bytes.NewReader(img)) | ||
195 | if err != nil { | ||
196 | return nil, err | ||
197 | } | ||
198 | req.Header.Add("quality", strconv.Itoa(quality)) | ||
199 | req.Header.Add("lossless", fmt.Sprintf("%t", lossless)) | ||
200 | req.Header.Add("exact", fmt.Sprintf("%t", exact)) | ||
201 | |||
202 | res, err := c.c.Do(req) | ||
203 | if err != nil { | ||
204 | return nil, err | ||
205 | } | ||
206 | |||
207 | if res.StatusCode != http.StatusOK { | ||
208 | e, err := io.ReadAll(res.Body) | ||
209 | if err != nil { | ||
210 | return nil, err | ||
211 | } | ||
212 | return nil, fmt.Errorf("WebPConvertClient server error: %s", e) | ||
213 | } | ||
214 | |||
215 | return res.Body, nil | ||
216 | } | ||