From 5c0cbb0cc16ca9a664d23bedc32f762ca65f9a75 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Mon, 15 Jan 2018 23:07:34 -0700
Subject: [PATCH] Add a way to disable animated thumbnails

They can be very CPU intensive, and people may not want to suffer that. Having the option to turn them off isn't a bad idea.
---
 config.sample.yaml                                            | 4 ++++
 src/github.com/turt2live/matrix-media-repo/config/config.go   | 2 ++
 .../services/thumbnail_service/thumbnailer.go                 | 3 ++-
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/config.sample.yaml b/config.sample.yaml
index d51132e0..3d94e096 100644
--- a/config.sample.yaml
+++ b/config.sample.yaml
@@ -102,6 +102,10 @@ thumbnails:
     - "image/png"
     - "image/gif"
 
+  # Animated thumbnails can be CPU intensive to generate. To disable the generation of animated
+  # thumbnails, set this to false. If disabled, regular thumbnails will be returned.
+  allowAnimated: true
+
   # The maximum file size to thumbnail when a capable animated thumbnail is requested. If the image
   # is larger than this, the thumbnail will be generated as a static image.
   maxAnimateSizeBytes: 10485760 # 10MB default, 0 to disable
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 08104637..3851d52b 100644
--- a/src/github.com/turt2live/matrix-media-repo/config/config.go
+++ b/src/github.com/turt2live/matrix-media-repo/config/config.go
@@ -46,6 +46,7 @@ type ThumbnailsConfig struct {
 	Types               []string         `yaml:"types,flow"`
 	MaxAnimateSizeBytes int64            `yaml:"maxAnimateSizeBytes"`
 	Sizes               []*ThumbnailSize `yaml:"sizes,flow"`
+	AllowAnimated       bool             `yaml:"allowAnimated"`
 }
 
 type ThumbnailSize struct {
@@ -159,6 +160,7 @@ func NewDefaultConfig() *MediaRepoConfig {
 			MaxSourceBytes:      10485760, // 10mb
 			MaxAnimateSizeBytes: 10485760, // 10mb
 			NumWorkers:          10,
+			AllowAnimated:       true,
 			Sizes: []*ThumbnailSize{
 				{32, 32},
 				{96, 96},
diff --git a/src/github.com/turt2live/matrix-media-repo/services/thumbnail_service/thumbnailer.go b/src/github.com/turt2live/matrix-media-repo/services/thumbnail_service/thumbnailer.go
index d95881e1..dfaa2808 100644
--- a/src/github.com/turt2live/matrix-media-repo/services/thumbnail_service/thumbnailer.go
+++ b/src/github.com/turt2live/matrix-media-repo/services/thumbnail_service/thumbnailer.go
@@ -12,6 +12,7 @@ import (
 
 	"github.com/disintegration/imaging"
 	"github.com/sirupsen/logrus"
+	"github.com/turt2live/matrix-media-repo/config"
 	"github.com/turt2live/matrix-media-repo/storage"
 	"github.com/turt2live/matrix-media-repo/types"
 	"github.com/turt2live/matrix-media-repo/util"
@@ -85,7 +86,7 @@ func (t *thumbnailer) GenerateThumbnail(media *types.Media, width int, height in
 
 	contentType := "image/png"
 	imgData := &bytes.Buffer{}
-	if animated && util.ArrayContains(animatedTypes, media.ContentType) {
+	if config.Get().Thumbnails.AllowAnimated && animated && util.ArrayContains(animatedTypes, media.ContentType) {
 		t.log.Info("Generating animated thumbnail")
 		contentType = "image/gif"
 
-- 
GitLab