From 0d62b5e3072af417e6013036cae2e3661f1ae6e8 Mon Sep 17 00:00:00 2001 From: Karar Al-Remahy Date: Wed, 19 Oct 2022 01:04:15 +0200 Subject: [PATCH] fix: Don't return inputBuf if input is of type PDF (#377) --- CHANGELOG.md | 2 +- pkg/thumbnail/thumbnail.go | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cb56eb5..54c3e6bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - Breaking: Go version 1.17 is now the minimum required version to build this. (#292) - Breaking: Thumbnail generation now requires libvips. See [docs/build.md](./docs/build.md) for prerequisite instructions. (#366, #369) - Breaking: Resolver caches are now stored in PostgreSQL. See [docs/build.md](./docs/build.md) for prerequisite instructions. (#271) -- PDF: Generate customized tooltips for PDF files. (#374) +- PDF: Generate customized tooltips for PDF files. (#374, #377) - Twitter: Generate thumbnails with all images of a tweet. (#373) - YouTube: Added support for 'YouTube shorts' URLs. (#299) - Fix: SevenTV emotes now resolve correctly. (#281, #288, #307) diff --git a/pkg/thumbnail/thumbnail.go b/pkg/thumbnail/thumbnail.go index 9a3a748f..ecf15a77 100644 --- a/pkg/thumbnail/thumbnail.go +++ b/pkg/thumbnail/thumbnail.go @@ -47,21 +47,22 @@ func Shutdown() { func BuildStaticThumbnail(inputBuf []byte, resp *http.Response) ([]byte, error) { image, err := vips.NewImageFromBuffer(inputBuf) + if err != nil { + return []byte{}, fmt.Errorf("could not load image from url: %s", resp.Request.URL) + } + // govips has the height & width values in int, which means we're converting uint to int. maxThumbnailSize := int(cfg.MaxThumbnailSize) + format := image.Format() // Only resize if the original image has bigger dimensions than maxThumbnailSize - if image.Width() <= maxThumbnailSize && image.Height() <= maxThumbnailSize { + if image.Width() <= maxThumbnailSize && image.Height() <= maxThumbnailSize && format != vips.ImageTypePDF { // We don't need to resize image nor does it need to be passed through govips. return inputBuf, nil } importParams := vips.NewImportParams() - if err != nil { - return []byte{}, fmt.Errorf("could not load image from url: %s", resp.Request.URL) - } - image, err = vips.LoadThumbnailFromBuffer(inputBuf, maxThumbnailSize, maxThumbnailSize, vips.InterestingNone, vips.SizeDown, importParams) if err != nil { @@ -70,7 +71,7 @@ func BuildStaticThumbnail(inputBuf []byte, resp *http.Response) ([]byte, error) } var outputBuf []byte - if image.Format() == vips.ImageTypePDF { + if format == vips.ImageTypePDF { // Export thumbnails for PDF as PNG outputBuf, _, err = image.ExportPng(vips.NewPngExportParams()) } else {