Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't return inputBuf if input is of type PDF #377

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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