From c95b6e585fde22d3e9b1c50ee2468560b4f674e1 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Thu, 11 Jan 2018 22:40:41 -0700
Subject: [PATCH] Limit thumbnails by content type in config

Adds #27
---
 config.sample.yaml                                  |  9 +++++++++
 .../turt2live/matrix-media-repo/config/config.go    |  5 +++--
 .../services/thumbnail_service/thumbnail_service.go | 13 +++++++++++++
 .../turt2live/matrix-media-repo/util/arrays.go      | 11 +++++++++++
 4 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 src/github.com/turt2live/matrix-media-repo/util/arrays.go

diff --git a/config.sample.yaml b/config.sample.yaml
index 08f8727b..de30b473 100644
--- a/config.sample.yaml
+++ b/config.sample.yaml
@@ -93,6 +93,15 @@ thumbnails:
   - width: 800
     height: 600
 
+  # The content types to thumbnail when requested. Types that are not supported by the media repo
+  # will not be thumbnailed (adding application/json here won't work). Clients may still not request
+  # thumbnails for these types - this won't make clients automatically thumbnail these file types.
+  types:
+   - "image/jpeg"
+   - "image/jpg"
+   - "image/png"
+   - "image/gif"
+
 # Controls for the rate limit functionality
 rateLimit:
   # Set this to false if rate limiting is handled at a higher level or you don't want it enabled.
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 cec65158..e9a4aabf 100644
--- a/src/github.com/turt2live/matrix-media-repo/config/config.go
+++ b/src/github.com/turt2live/matrix-media-repo/config/config.go
@@ -38,8 +38,9 @@ type MediaRepoConfig struct {
 	} `yaml:"downloads"`
 
 	Thumbnails struct {
-		MaxSourceBytes int64 `yaml:"maxSourceBytes"`
-		NumWorkers     int   `yaml:"numWorkers"`
+		MaxSourceBytes int64    `yaml:"maxSourceBytes"`
+		NumWorkers     int      `yaml:"numWorkers"`
+		Types          []string `yaml:"types,flow"`
 		Sizes []struct {
 			Width  int    `yaml:"width"`
 			Height int    `yaml:"height"`
diff --git a/src/github.com/turt2live/matrix-media-repo/services/thumbnail_service/thumbnail_service.go b/src/github.com/turt2live/matrix-media-repo/services/thumbnail_service/thumbnail_service.go
index 5a906ea0..11441ebd 100644
--- a/src/github.com/turt2live/matrix-media-repo/services/thumbnail_service/thumbnail_service.go
+++ b/src/github.com/turt2live/matrix-media-repo/services/thumbnail_service/thumbnail_service.go
@@ -14,6 +14,9 @@ import (
 	"github.com/turt2live/matrix-media-repo/util/errs"
 )
 
+// These are the content types that we can actually thumbnail
+var supportedThumbnailTypes = []string{"image/jpeg", "image/jpg", "image/png", "image/gif"}
+
 type thumbnailService struct {
 	store *stores.ThumbnailStore
 	ctx   context.Context
@@ -85,6 +88,16 @@ func (s *thumbnailService) GetThumbnail(media *types.Media, width int, height in
 		return thumb, nil
 	}
 
+	if !util.ArrayContains(supportedThumbnailTypes, media.ContentType) {
+		s.log.Warn("Cannot generate thumbnail for " + media.ContentType + " because it is not supported")
+		return nil, errors.New("cannot generate thumbnail for this media's content type")
+	}
+
+	if !util.ArrayContains(config.Get().Thumbnails.Types, media.ContentType) {
+		s.log.Warn("Cannot generate thumbnail for " + media.ContentType + " because it is not listed in the config")
+		return nil, errors.New("cannot generate thumbnail for this media's content type")
+	}
+
 	if media.SizeBytes > config.Get().Thumbnails.MaxSourceBytes {
 		s.log.Warn("Media too large to thumbnail")
 		return thumb, errs.ErrMediaTooLarge
diff --git a/src/github.com/turt2live/matrix-media-repo/util/arrays.go b/src/github.com/turt2live/matrix-media-repo/util/arrays.go
new file mode 100644
index 00000000..6c1d9669
--- /dev/null
+++ b/src/github.com/turt2live/matrix-media-repo/util/arrays.go
@@ -0,0 +1,11 @@
+package util
+
+func ArrayContains(a []string, v string) (bool) {
+	for _, e := range a {
+		if e == v {
+			return true
+		}
+	}
+
+	return false
+}
-- 
GitLab