Skip to content

Commit

Permalink
Merge pull request #6 from takutakahashi/webp
Browse files Browse the repository at this point in the history
Convert by only cwebp when .webp ext filepath requested
  • Loading branch information
takutakahashi authored Oct 13, 2023
2 parents 9861b7e + b961560 commit 26d5665
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -133,24 +134,24 @@ func proxy(w http.ResponseWriter, r *http.Request) {
}
return
}

buf, err := convert(orgRes.Body, quality)
if err != nil {
http.Error(w, "Image convert failed", http.StatusInternalServerError)
log.Printf("Image convert failed. %v\n", err)
return
}
defer buf.Reset()
var buf *bytes.Buffer
if pathExt == ".webp" {
buf, err = convWebp(buf, []string{})
buf, err = convWebp(orgRes.Body, []string{})
if err != nil {
http.Error(w, "image convert failed", http.StatusInternalServerError)
log.Printf("Read origin body failed. %v\n", err)
return

}
defer buf.Reset()
w.Header().Set("Content-Type", "image/webp")
} else {
buf, err = convert(orgRes.Body, quality)
if err != nil {
http.Error(w, "Image convert failed", http.StatusInternalServerError)
log.Printf("Image convert failed. %v\n", err)
return
}
defer buf.Reset()
w.Header().Set("Content-Type", "image/jpeg")
}
w.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
Expand Down

0 comments on commit 26d5665

Please sign in to comment.