Skip to content

Commit

Permalink
fix: Don't return inputBuf if input is of type PDF (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
KararTY committed Oct 18, 2022
1 parent a67a14d commit 0d62b5e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions pkg/thumbnail/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 0d62b5e

Please sign in to comment.