From 467557e78f95ec4a0f41488f53005b9bcae5e536 Mon Sep 17 00:00:00 2001
From: Pavel Kardash <pavel@kardash.su>
Date: Fri, 14 Sep 2018 12:23:57 +0300
Subject: [PATCH] Upload exclusions to specific users

---
 config.sample.yaml                                  |  9 +++++++++
 .../matrix-media-repo/common/config/config.go       |  9 +++++----
 .../upload_controller/upload_controller.go          | 13 +++++++++++++
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/config.sample.yaml b/config.sample.yaml
index 6e7f6292..e68596b1 100644
--- a/config.sample.yaml
+++ b/config.sample.yaml
@@ -50,6 +50,15 @@ uploads:
   allowedTypes:
    - "*/*"
 
+  # If not all file types are allowed, then you can make an exlusion for some users. So 
+  # that users can upload file types that not allowed to others.
+  #exclusions:
+  #  "@admin1:matrix.org":
+  #     - "application/pdf"
+  #     - "application/vnd.ms-excel"
+  #  "@admin2:matrix.org": [*/*]
+
+
 # Settings related to downloading files from the media repository
 downloads:
   # The maximum number of bytes to download from other servers
diff --git a/src/github.com/turt2live/matrix-media-repo/common/config/config.go b/src/github.com/turt2live/matrix-media-repo/common/config/config.go
index 1c2a7d62..c9dca0b2 100644
--- a/src/github.com/turt2live/matrix-media-repo/common/config/config.go
+++ b/src/github.com/turt2live/matrix-media-repo/common/config/config.go
@@ -32,10 +32,11 @@ type DatabaseConfig struct {
 }
 
 type UploadsConfig struct {
-	StoragePaths         []string `yaml:"storagePaths,flow"`
-	MaxSizeBytes         int64    `yaml:"maxBytes"`
-	AllowedTypes         []string `yaml:"allowedTypes,flow"`
-	ReportedMaxSizeBytes int64    `yaml:"reportedMaxBytes"`
+	StoragePaths         []string            `yaml:"storagePaths,flow"`
+	MaxSizeBytes         int64               `yaml:"maxBytes"`
+	AllowedTypes         []string            `yaml:"allowedTypes,flow"`
+	AllowedExcl          map[string][]string `yaml:"exclusions,flow"`
+	ReportedMaxSizeBytes int64               `yaml:"reportedMaxBytes"`
 }
 
 type DownloadsConfig struct {
diff --git a/src/github.com/turt2live/matrix-media-repo/controllers/upload_controller/upload_controller.go b/src/github.com/turt2live/matrix-media-repo/controllers/upload_controller/upload_controller.go
index 7e8d5d74..1b91412f 100644
--- a/src/github.com/turt2live/matrix-media-repo/controllers/upload_controller/upload_controller.go
+++ b/src/github.com/turt2live/matrix-media-repo/controllers/upload_controller/upload_controller.go
@@ -75,10 +75,23 @@ func StoreDirect(contents io.Reader, contentType string, filename string, userId
 		}
 	}
 	if !allowed {
+		exclusion := false
+		for user, userExcl := range config.Get().Uploads.AllowedExcl {
+			if user == userId {
+				for _, exclType := range userExcl {
+					if glob.Glob(exclType, fileMime){
+						exclusion = true
+						log.Info("Content type " + fileMime +" (reported as " + contentType+") is allowed to be uploaded as exclusion for user "+ userId)
+					}
+				}
+			}
+		}
+		if !exclusion {
 			log.Warn("Content type " + fileMime +" (reported as " + contentType+") is not allowed to be uploaded")
 
 			os.Remove(fileLocation) // delete temp file
 			return nil, common.ErrMediaNotAllowed
+		}
 	}
 
 	hash, err := storage.GetFileHash(fileLocation)
-- 
GitLab