From 84e2186fa5aa584547d67381504053094fe6ee17 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Tue, 1 Aug 2023 20:09:39 -0600
Subject: [PATCH] Add TIFF thumbnail support

---
 CHANGELOG.md           |  1 +
 config.sample.yaml     |  1 +
 thumbnailing/i/tiff.go | 46 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+)
 create mode 100644 thumbnailing/i/tiff.go

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 69972765..b39eddb0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -91,6 +91,7 @@ path/server, for example, then you can simply update the path in the config for
 * Support for [MSC4034](https://github.com/matrix-org/matrix-spec-proposals/pull/4034) (self-serve usage information) is added, alongside a new "maximum file count" quota limit.
 * The `GET /_synapse/admin/v1/statistics/users/media` [endpoint](https://matrix-org.github.io/synapse/v1.88/admin_api/statistics.html#users-media-usage-statistics) from Synapse is now supported at the same path for local server admins.
 * Thumbnailing support for BMP images.
+* Thumbnailing support for TIFF images.
 
 ### Removed
 
diff --git a/config.sample.yaml b/config.sample.yaml
index 2da60529..62d51557 100644
--- a/config.sample.yaml
+++ b/config.sample.yaml
@@ -412,6 +412,7 @@ thumbnails:
     - "image/heif"
     - "image/webp"
     - "image/bmp"
+    - "image/tiff"
     #- "image/svg+xml" # Be sure to have ImageMagick installed to thumbnail SVG files
     - "audio/mpeg"
     - "audio/ogg"
diff --git a/thumbnailing/i/tiff.go b/thumbnailing/i/tiff.go
new file mode 100644
index 00000000..57334e59
--- /dev/null
+++ b/thumbnailing/i/tiff.go
@@ -0,0 +1,46 @@
+package i
+
+import (
+	"errors"
+	"io"
+
+	"github.com/turt2live/matrix-media-repo/common/rcontext"
+	"github.com/turt2live/matrix-media-repo/thumbnailing/m"
+	"golang.org/x/image/tiff"
+)
+
+type tiffGenerator struct {
+}
+
+func (d tiffGenerator) supportedContentTypes() []string {
+	return []string{"image/tiff"}
+}
+
+func (d tiffGenerator) supportsAnimation() bool {
+	return false
+}
+
+func (d tiffGenerator) matches(img io.Reader, contentType string) bool {
+	return contentType == "image/tiff"
+}
+
+func (d tiffGenerator) GetOriginDimensions(b io.Reader, contentType string, ctx rcontext.RequestContext) (bool, int, int, error) {
+	i, err := tiff.DecodeConfig(b)
+	if err != nil {
+		return false, 0, 0, err
+	}
+	return true, i.Width, i.Height, nil
+}
+
+func (d tiffGenerator) GenerateThumbnail(b io.Reader, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
+	src, err := tiff.Decode(b)
+	if err != nil {
+		return nil, errors.New("tiff: error decoding thumbnail: " + err.Error())
+	}
+
+	return pngGenerator{}.GenerateThumbnailOf(src, width, height, method, ctx)
+}
+
+func init() {
+	generators = append(generators, tiffGenerator{})
+}
-- 
GitLab