Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert by only cwebp when .webp ext filepath requested #6

Merged
merged 10 commits into from
Oct 13, 2023
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