Skip to content

Commit

Permalink
feat(image-proxy): improve format support
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed Nov 1, 2023
1 parent 59abebd commit 3e01763
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
1 change: 1 addition & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
environment:
IMGPROXY_KEY: ${READFLOW_HASH_SECRET_KEY:-736563726574}
IMGPROXY_SALT: ${READFLOW_HASH_SECRET_SALT:-706570706572}
IMGPROXY_ENABLE_WEBP_DETECTION: true
ports:
- "${IMAGOR_PORT:-8081}:8080"
#######################################
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ services:
image: darthsim/imgproxy
environment:
IMGPROXY_KEY: ${READFLOW_HASH_SECRET_KEY:-736563726574}
IMGPROXY_SALT: ${READFLOW_HASH_SECRET_SALT:-706570706572}
IMGPROXY_SALT: ${READFLOW_HASH_SECRET_SALT:-706570706572}
IMGPROXY_ENABLE_WEBP_DETECTION: true
logging:
driver: "json-file"
options:
Expand Down
30 changes: 20 additions & 10 deletions pkg/api/image-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (

"github.com/ncarlier/readflow/pkg/config"
"github.com/ncarlier/readflow/pkg/constant"
"github.com/ncarlier/readflow/pkg/helper"
)

const defaultUserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0"

var hopHeaders = []string{
"Connection",
"Keep-Alive",
Expand All @@ -26,7 +25,11 @@ var hopHeaders = []string{
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
if dst.Get(k) != "" {
dst.Set(k, v)
} else {
dst.Add(k, v)
}
}
}
}
Expand Down Expand Up @@ -71,16 +74,12 @@ func imgProxyHandler(conf *config.Config) http.Handler {
return
}

// Manage request headers
userAgent := r.Header.Get("User-Agent")
if userAgent == "" {
userAgent = defaultUserAgent
}
req.Header.Set("User-Agent", userAgent)
// Manage request headers: copy, add x-forward, del hop
copyHeader(req.Header, r.Header)
if clientIP, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
appendHostToXForwardHeader(req.Header, clientIP)
}
delHopHeaders(r.Header)
delHopHeaders(req.Header)

// Do proxy request
resp, err := constant.DefaultClient.Do(req)
Expand All @@ -90,6 +89,17 @@ func imgProxyHandler(conf *config.Config) http.Handler {
}
defer resp.Body.Close()

// Redirect if image proxy failed
if resp.StatusCode >= 400 {
// decode image URL from proxy path
if img, err := helper.DecodeImageProxyPath(path); err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
} else {
http.Redirect(w, r, string(img), http.StatusTemporaryRedirect)
}
return
}

// Create proxy response
delHopHeaders(resp.Header)
copyHeader(w.Header(), resp.Header)
Expand Down
23 changes: 23 additions & 0 deletions pkg/helper/image-proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package helper

import (
"encoding/base64"
"strings"
)

// Encode image URL to Image Proxy path
func EncodeImageProxyPath(url, size string) string {
return "/resize:fit:" + size + "/" + base64.StdEncoding.EncodeToString([]byte(url))
}

// Decode image URL from Image Proxy Path
func DecodeImageProxyPath(path string) (url string, err error) {
url = path[strings.LastIndex(path, "/")+1:]
var decoded []byte
decoded, err = base64.StdEncoding.DecodeString(url)
if err == nil {
url = string(decoded)
}

return
}
18 changes: 0 additions & 18 deletions pkg/helper/sign.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/service/articles_thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/base64"

"github.com/ncarlier/readflow/pkg/helper"
"github.com/ncarlier/readflow/pkg/model"
)

Expand All @@ -13,7 +14,7 @@ func (reg *Registry) GetArticleThumbnailHash(article *model.Article, size string
if article.Image == nil || *article.Image == "" {
return ""
}
path := "/resize:fit:" + size + "/" + base64.StdEncoding.EncodeToString([]byte(*article.Image))
path := helper.EncodeImageProxyPath(*article.Image, size)

mac := hmac.New(sha256.New, reg.conf.Hash.SecretKey.Value)
mac.Write(reg.conf.Hash.SecretSalt.Value)
Expand Down

0 comments on commit 3e01763

Please sign in to comment.