try using lilliput instead of govips
This commit is contained in:
parent
d3d7ce98d6
commit
4fc4b1ee7e
4 changed files with 30 additions and 86 deletions
8
go.mod
8
go.mod
|
@ -2,10 +2,4 @@ module codeberg.org/pronounscc/image-proxy
|
|||
|
||||
go 1.20
|
||||
|
||||
require github.com/davidbyttow/govips/v2 v2.13.0
|
||||
|
||||
require (
|
||||
golang.org/x/image v0.5.0 // indirect
|
||||
golang.org/x/net v0.7.0 // indirect
|
||||
golang.org/x/text v0.7.0 // indirect
|
||||
)
|
||||
require github.com/discord/lilliput v0.0.0-20240201002527-adc97f85f75a
|
||||
|
|
50
go.sum
50
go.sum
|
@ -1,48 +1,2 @@
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davidbyttow/govips/v2 v2.13.0 h1:5MK9ZcXZC5GzUR9Ca8fJwOYqMgll/H096ec0PJP59QM=
|
||||
github.com/davidbyttow/govips/v2 v2.13.0/go.mod h1:LPTrwWtNa5n4yl9UC52YBOEGdZcY5hDTP4Ms2QWasTw=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI=
|
||||
golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
|
||||
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
github.com/discord/lilliput v0.0.0-20240201002527-adc97f85f75a h1:pNcLwEB3P0WceVWvRBeHM/BAO5nyQ1o2X9INIz6DD9Y=
|
||||
github.com/discord/lilliput v0.0.0-20240201002527-adc97f85f75a/go.mod h1:0euuUBAD72MAYRm2ElLaG1h0nBR+CgpfnKc/U6y/uE8=
|
||||
|
|
47
handler.go
47
handler.go
|
@ -14,7 +14,7 @@ import (
|
|||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/davidbyttow/govips/v2/vips"
|
||||
"github.com/discord/lilliput"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
|
@ -27,6 +27,7 @@ type Handler struct {
|
|||
// ProxyTarget is the host that requests for the default image size will be rewritten to.
|
||||
ProxyTarget string `json:"proxy_target"`
|
||||
proxy *httputil.ReverseProxy
|
||||
imageOps *lilliput.ImageOps
|
||||
}
|
||||
|
||||
type pattern struct {
|
||||
|
@ -159,23 +160,29 @@ func (hn *Handler) proxyImage(ctx context.Context, w http.ResponseWriter, cacheK
|
|||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
image, err := vips.NewImageFromReader(resp.Body)
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Printf("creating vips image: %v", err)
|
||||
log.Printf("error reading image body: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = image.ThumbnailWithSize(size, size, vips.InterestingCentre, vips.SizeBoth)
|
||||
decoder, err := lilliput.NewDecoder(b)
|
||||
if err != nil {
|
||||
log.Printf("resizing vips image: %v", err)
|
||||
log.Printf("error creating decoder: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
out, _, err := export(image, contentType)
|
||||
buf := make([]byte, 0, 1024*1024)
|
||||
buf, err = hn.imageOps.Transform(decoder, &lilliput.ImageOptions{
|
||||
FileType: fileExt(contentType),
|
||||
Width: size,
|
||||
Height: size,
|
||||
ResizeMethod: lilliput.ImageOpsFit,
|
||||
}, buf)
|
||||
if err != nil {
|
||||
log.Printf("exporting vips image: %v", err)
|
||||
log.Printf("error transforming image: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
@ -190,35 +197,27 @@ func (hn *Handler) proxyImage(ctx context.Context, w http.ResponseWriter, cacheK
|
|||
log.Printf("writing image to cache: %v", err)
|
||||
return
|
||||
}
|
||||
}(out)
|
||||
}(buf)
|
||||
|
||||
w.Header().Add("Content-Type", contentType)
|
||||
w.Header().Add("Content-Length", strconv.Itoa(len(out)))
|
||||
_, err = w.Write(out)
|
||||
w.Header().Add("Content-Length", strconv.Itoa(len(buf)))
|
||||
_, err = w.Write(buf)
|
||||
if err != nil {
|
||||
log.Printf("error writing resized file to response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func export(image *vips.ImageRef, contentType string) ([]byte, *vips.ImageMetadata, error) {
|
||||
func fileExt(contentType string) string {
|
||||
switch contentType {
|
||||
case "image/webp":
|
||||
params := vips.NewWebpExportParams()
|
||||
params.Quality = 90
|
||||
return image.ExportWebp(params)
|
||||
return ".webp"
|
||||
case "image/jpeg":
|
||||
params := vips.NewJpegExportParams()
|
||||
params.Quality = 90
|
||||
return image.ExportJpeg(params)
|
||||
return ".jpeg"
|
||||
case "image/png":
|
||||
params := vips.NewPngExportParams()
|
||||
params.Quality = 90
|
||||
return image.ExportPng(params)
|
||||
return ".png"
|
||||
case "image/gif":
|
||||
params := vips.NewGifExportParams()
|
||||
params.Quality = 90
|
||||
return image.ExportGIF(params)
|
||||
return ".gif"
|
||||
}
|
||||
|
||||
return nil, nil, errors.New("invalid image content type")
|
||||
return ""
|
||||
}
|
||||
|
|
11
main.go
11
main.go
|
@ -12,16 +12,10 @@ import (
|
|||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"github.com/davidbyttow/govips/v2/vips"
|
||||
"github.com/discord/lilliput"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// set vips log level to WARN, else it will spam logs on info level
|
||||
vips.LoggingSettings(nil, vips.LogLevelWarning)
|
||||
|
||||
vips.Startup(nil)
|
||||
defer vips.Shutdown()
|
||||
|
||||
// read config file and parse it
|
||||
confB, err := os.ReadFile("config.json")
|
||||
if err != nil {
|
||||
|
@ -34,6 +28,9 @@ func main() {
|
|||
log.Fatalf("unmarshaling config.json: %v", err)
|
||||
}
|
||||
|
||||
// create image ops object
|
||||
hn.imageOps = lilliput.NewImageOps(512)
|
||||
|
||||
// Construct reverse proxy for bare/default size images
|
||||
proxyURL, err := url.Parse(hn.ProxyTarget)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue