From 7b45b0694747100a82b5eb918a855508e9adff9a Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Sun, 12 Nov 2017 21:51:38 -0700
Subject: [PATCH] Don't thumbnail large files

---
 config.sample.yaml                                            | 3 +++
 src/github.com/turt2live/matrix-media-repo/config/config.go   | 1 +
 .../turt2live/matrix-media-repo/media_handler/thumbnailer.go  | 4 ++++
 3 files changed, 8 insertions(+)

diff --git a/config.sample.yaml b/config.sample.yaml
index c5d4643a..7cfdd332 100644
--- a/config.sample.yaml
+++ b/config.sample.yaml
@@ -25,6 +25,9 @@ uploads:
 
 # The thumbnail configuration for the media repository.
 thumbnails:
+  # The maximum number of bytes an image can be before the thumbnailer refuses.
+  maxSourceBytes: 10485760 # 10MB default, 0 to disable
+
   # All thumbnails are generated into one of the sizes listed here. The first size is used as
   # the default for when no width or height is requested. The media repository will return
   # either an exact match or the next largest size of thumbnail.
diff --git a/src/github.com/turt2live/matrix-media-repo/config/config.go b/src/github.com/turt2live/matrix-media-repo/config/config.go
index ff2416c4..1a63932f 100644
--- a/src/github.com/turt2live/matrix-media-repo/config/config.go
+++ b/src/github.com/turt2live/matrix-media-repo/config/config.go
@@ -24,6 +24,7 @@ type MediaRepoConfig struct {
 	} `yaml:"uploads"`
 
 	Thumbnails struct {
+		MaxSourceBytes int64 `yaml:"maxSourceBytes"`
 		Sizes []struct {
 			Width int `yaml:"width"`
 			Height int `yaml:"height"`
diff --git a/src/github.com/turt2live/matrix-media-repo/media_handler/thumbnailer.go b/src/github.com/turt2live/matrix-media-repo/media_handler/thumbnailer.go
index 7dc62fa4..f000e8c7 100644
--- a/src/github.com/turt2live/matrix-media-repo/media_handler/thumbnailer.go
+++ b/src/github.com/turt2live/matrix-media-repo/media_handler/thumbnailer.go
@@ -66,6 +66,10 @@ func GetThumbnail(ctx context.Context, media types.Media, width int, height int,
 		return thumb, err
 	}
 
+	if media.SizeBytes > c.Thumbnails.MaxSourceBytes {
+		return thumb, errors.New("cannot thumbnail, image too large")
+	}
+
 	return generateThumbnail(ctx, media, targetWidth, targetHeight, method, c, db)
 }
 
-- 
GitLab