From ede42d590a0faed02d6737c831443e04fc760c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82?= Date: Wed, 9 Jun 2021 09:30:12 +0200 Subject: [PATCH] chore: Moved thumbnail into its own package (#160) --- cmd/api/main.go | 1 - internal/resolvers/default/load.go | 3 +- internal/resolvers/default/resolver.go | 26 ++++++++++++-- .../default => pkg/thumbnail}/thumbnail.go | 36 +++---------------- .../thumbnail}/thumbnail_unix.go | 6 ++-- .../thumbnail}/thumbnail_windows.go | 2 +- 6 files changed, 34 insertions(+), 40 deletions(-) rename {internal/resolvers/default => pkg/thumbnail}/thumbnail.go (79%) rename {internal/resolvers/default => pkg/thumbnail}/thumbnail_unix.go (95%) rename {internal/resolvers/default => pkg/thumbnail}/thumbnail_windows.go (93%) diff --git a/cmd/api/main.go b/cmd/api/main.go index c85f2a31..934779d3 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -103,7 +103,6 @@ func main() { handleHealth(router) defaultresolver.Initialize(router, BaseURL()) - defaultresolver.InitializeThumbnail(router) listen(BindAddress(), mountRouter(router)) } diff --git a/internal/resolvers/default/load.go b/internal/resolvers/default/load.go index af6ae8fb..a1d5444f 100644 --- a/internal/resolvers/default/load.go +++ b/internal/resolvers/default/load.go @@ -13,6 +13,7 @@ import ( "github.com/Chatterino/api/pkg/cache" "github.com/Chatterino/api/pkg/resolver" + "github.com/Chatterino/api/pkg/thumbnail" "github.com/Chatterino/api/pkg/utils" "github.com/PuerkitoBio/goquery" ) @@ -115,7 +116,7 @@ func (dr *R) load(urlString string, r *http.Request) (interface{}, time.Duration Thumbnail: data.ImageSrc, } - if isSupportedThumbnail(resp.Header.Get("content-type")) { + if thumbnail.IsSupportedThumbnail(resp.Header.Get("content-type")) { response.Thumbnail = utils.FormatThumbnailURL(dr.baseURL, r, resp.Request.URL.String()) } diff --git a/internal/resolvers/default/resolver.go b/internal/resolvers/default/resolver.go index 4594989b..682b0975 100644 --- a/internal/resolvers/default/resolver.go +++ b/internal/resolvers/default/resolver.go @@ -19,6 +19,7 @@ import ( "github.com/Chatterino/api/internal/resolvers/youtube" "github.com/Chatterino/api/pkg/cache" "github.com/Chatterino/api/pkg/resolver" + "github.com/Chatterino/api/pkg/thumbnail" "github.com/Chatterino/api/pkg/utils" "github.com/go-chi/chi/v5" ) @@ -43,7 +44,8 @@ type R struct { customResolvers []resolver.CustomURLManager - defaultResolverCache *cache.Cache + defaultResolverCache *cache.Cache + defaultResolverThumbnailCache *cache.Cache } func (dr *R) HandleRequest(w http.ResponseWriter, r *http.Request) { @@ -65,12 +67,31 @@ func (dr *R) HandleRequest(w http.ResponseWriter, r *http.Request) { } } +func (dr *R) HandleThumbnailRequest(w http.ResponseWriter, r *http.Request) { + url, err := utils.UnescapeURLArgument(r, "url") + if err != nil { + _, err = w.Write(resolver.InvalidURL) + if err != nil { + log.Println("Error writing thumbnail response:", err) + } + return + } + + response := dr.defaultResolverThumbnailCache.Get(url, r) + + _, err = w.Write(response.([]byte)) + if err != nil { + log.Println("Error writing thumbnail response:", err) + } +} + func New(baseURL string) *R { r := &R{ baseURL: baseURL, } - r.defaultResolverCache = cache.New("linkResolver", r.load, time.Duration(10)*time.Minute) + r.defaultResolverCache = cache.New("linkResolver", r.load, 10*time.Minute) + r.defaultResolverThumbnailCache = cache.New("thumbnail", thumbnail.DoThumbnailRequest, 10*time.Minute) // Register Link Resolvers from internal/resolvers/ r.customResolvers = append(r.customResolvers, betterttv.New()...) @@ -92,4 +113,5 @@ func Initialize(router *chi.Mux, baseURL string) { defaultLinkResolver := New(baseURL) router.Get("/link_resolver/{url}", defaultLinkResolver.HandleRequest) + router.Get("/thumbnail/{url}", defaultLinkResolver.HandleThumbnailRequest) } diff --git a/internal/resolvers/default/thumbnail.go b/pkg/thumbnail/thumbnail.go similarity index 79% rename from internal/resolvers/default/thumbnail.go rename to pkg/thumbnail/thumbnail.go index db1d9969..723d380c 100644 --- a/internal/resolvers/default/thumbnail.go +++ b/pkg/thumbnail/thumbnail.go @@ -1,5 +1,4 @@ -// TODO: this should potentially be split out into its own file -package defaultresolver +package thumbnail import ( "bytes" @@ -19,7 +18,6 @@ import ( "github.com/Chatterino/api/pkg/cache" "github.com/Chatterino/api/pkg/resolver" "github.com/Chatterino/api/pkg/utils" - "github.com/go-chi/chi/v5" "github.com/nfnt/resize" ) @@ -55,7 +53,7 @@ func buildStaticThumbnailByteArray(inputBuf []byte, resp *http.Response) ([]byte return buffer.Bytes(), nil } -func doThumbnailRequest(urlString string, r *http.Request) (interface{}, time.Duration, error) { +func DoThumbnailRequest(urlString string, r *http.Request) (interface{}, time.Duration, error) { url, err := url.Parse(urlString) if err != nil { return resolver.InvalidURL, cache.NoSpecialDur, nil @@ -90,7 +88,7 @@ func doThumbnailRequest(urlString string, r *http.Request) (interface{}, time.Du return resolver.NoLinkInfoFound, cache.NoSpecialDur, nil } - if !isSupportedThumbnail(resp.Header.Get("content-type")) { + if !IsSupportedThumbnail(resp.Header.Get("content-type")) { return resolver.NoLinkInfoFound, cache.NoSpecialDur, nil } @@ -113,7 +111,7 @@ func doThumbnailRequest(urlString string, r *http.Request) (interface{}, time.Du return image, 10 * time.Minute, nil } -func isSupportedThumbnail(contentType string) bool { +func IsSupportedThumbnail(contentType string) bool { for _, supportedType := range supportedThumbnails { if contentType == supportedType { return true @@ -122,29 +120,3 @@ func isSupportedThumbnail(contentType string) bool { return false } - -func thumbnail(w http.ResponseWriter, r *http.Request) { - url, err := utils.UnescapeURLArgument(r, "url") - if err != nil { - _, err = w.Write(resolver.InvalidURL) - if err != nil { - log.Println("Error writing response:", err) - } - return - } - - response := thumbnailCache.Get(url, r) - - _, err = w.Write(response.([]byte)) - if err != nil { - log.Println("Error writing response:", err) - } -} - -var ( - thumbnailCache = cache.New("thumbnail", doThumbnailRequest, 10*time.Minute) -) - -func InitializeThumbnail(router *chi.Mux) { - router.Get("/thumbnail/{url}", thumbnail) -} diff --git a/internal/resolvers/default/thumbnail_unix.go b/pkg/thumbnail/thumbnail_unix.go similarity index 95% rename from internal/resolvers/default/thumbnail_unix.go rename to pkg/thumbnail/thumbnail_unix.go index 3c2bab73..1f8792ec 100644 --- a/internal/resolvers/default/thumbnail_unix.go +++ b/pkg/thumbnail/thumbnail_unix.go @@ -1,6 +1,6 @@ // +build !windows -package defaultresolver +package thumbnail import ( "fmt" @@ -11,7 +11,7 @@ import ( ) var ( - EncodeOptions = map[string]map[int]int{ + encodeOptions = map[string]map[int]int{ ".jpeg": {lilliput.JpegQuality: 85}, ".png": {lilliput.PngCompression: 7}, ".webp": {lilliput.WebpQuality: 85}, @@ -83,7 +83,7 @@ func buildThumbnailByteArray(inputBuf []byte, resp *http.Response) ([]byte, erro Width: newWidth, Height: newHeight, ResizeMethod: resizeMethod, - EncodeOptions: EncodeOptions[outputType], + EncodeOptions: encodeOptions[outputType], } // resize and transcode image diff --git a/internal/resolvers/default/thumbnail_windows.go b/pkg/thumbnail/thumbnail_windows.go similarity index 93% rename from internal/resolvers/default/thumbnail_windows.go rename to pkg/thumbnail/thumbnail_windows.go index bbff944e..9c95417f 100644 --- a/internal/resolvers/default/thumbnail_windows.go +++ b/pkg/thumbnail/thumbnail_windows.go @@ -1,6 +1,6 @@ // +build windows -package defaultresolver +package thumbnail import ( "errors"