diff --git a/config.sample.yaml b/config.sample.yaml index 08f8727bbc1da7d5bc0d259a07366c75a84a27a8..de30b4731fa38409e3fac28ebbf217391f2a70a5 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 cec651582eb4fc0340070f3c88bcfe0775549c35..e9a4aabfa1f54dfd99bf20312e415146ffac0fca 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 5a906ea0a2c1e132649abcddfe6ba5a595e9de2f..11441ebdabf14069e9ff69877faee58be9bdda80 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 0000000000000000000000000000000000000000..6c1d9669dfaf22a58d7ef9bc19a8acf440105742 --- /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 +}