Skip to content

Commit

Permalink
chore: Moved thumbnail into its own package (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
zneix committed Jun 9, 2021
1 parent 4358948 commit ede42d5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 40 deletions.
1 change: 0 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func main() {
handleHealth(router)

defaultresolver.Initialize(router, BaseURL())
defaultresolver.InitializeThumbnail(router)

listen(BindAddress(), mountRouter(router))
}
3 changes: 2 additions & 1 deletion internal/resolvers/default/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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())
}

Expand Down
26 changes: 24 additions & 2 deletions internal/resolvers/default/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand All @@ -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()...)
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO: this should potentially be split out into its own file
package defaultresolver
package thumbnail

import (
"bytes"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build !windows

package defaultresolver
package thumbnail

import (
"fmt"
Expand All @@ -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},
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build windows

package defaultresolver
package thumbnail

import (
"errors"
Expand Down

0 comments on commit ede42d5

Please sign in to comment.