Skip to content

Commit

Permalink
feat: fallback invalid image (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Sep 12, 2019
1 parent f162176 commit 3a0eca7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func encode(src image.Image) (*bytes.Buffer, error) {
}

// Convert returns a image buffer converted to webp
func Convert(src io.ReadCloser) (*bytes.Buffer, error) {
func Convert(src io.Reader) (*bytes.Buffer, error) {
img, err := decode(src)
if err != nil {
return nil, err
Expand Down
13 changes: 11 additions & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
package manael // import "manael.org/x/manael"

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net"
Expand Down Expand Up @@ -117,9 +119,16 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {

defer resp.Body.Close()

buf, err := Convert(resp.Body)
var body io.Reader = resp.Body

p := bytes.NewBuffer(nil)
r := io.TeeReader(body, p)

buf, err := Convert(r)
if err != nil {
resp = NewResponse(req2, http.StatusInternalServerError)
body = io.MultiReader(p, body)

resp.Body = ioutil.NopCloser(body)
log.Printf("error: %v\n", err)

return resp, nil
Expand Down
4 changes: 2 additions & 2 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ var transportTests = []struct {
{
"image/webp,image/*,*/*",
"/invalid.png",
http.StatusInternalServerError,
"text/plain; charset=utf-8",
http.StatusOK,
"image/png",
"not image",
},
}
Expand Down

0 comments on commit 3a0eca7

Please sign in to comment.