From 0919f2f9c534d294ae795ccb138c3fd0dc7344d2 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Tue, 16 Apr 2019 10:48:47 -0600
Subject: [PATCH] Support storagePaths configuration option at a higher level

---
 .../matrix-media-repo/cmd/media_repo/main.go  | 18 ++++++
 .../storage/datastore/datastore.go            | 56 -------------------
 2 files changed, 18 insertions(+), 56 deletions(-)

diff --git a/src/github.com/turt2live/matrix-media-repo/cmd/media_repo/main.go b/src/github.com/turt2live/matrix-media-repo/cmd/media_repo/main.go
index 2f4910e2..5790a58e 100644
--- a/src/github.com/turt2live/matrix-media-repo/cmd/media_repo/main.go
+++ b/src/github.com/turt2live/matrix-media-repo/cmd/media_repo/main.go
@@ -28,6 +28,24 @@ func main() {
 		panic(err)
 	}
 
+	if len(config.Get().Uploads.StoragePaths) > 0 {
+		logrus.Warn("storagePaths usage is deprecated - please use datastores instead")
+		for _, p := range config.Get().Uploads.StoragePaths {
+			ds, err := storage.GetOrCreateDatastoreOfType(context.Background(), logrus.WithFields(logrus.Fields{"path": p}), "file", p)
+			if err != nil {
+				logrus.Fatal(err)
+			}
+
+			fakeConfig := config.DatastoreConfig{
+				Type:       "file",
+				Enabled:    true,
+				ForUploads: true,
+				Options:    map[string]string{"path": ds.Uri},
+			}
+			config.Get().DataStores = append(config.Get().DataStores, fakeConfig)
+		}
+	}
+
 	mediaStore := storage.GetDatabase().GetMediaStore(context.TODO(), &logrus.Entry{})
 
 	logrus.Info("Initializing datastores...")
diff --git a/src/github.com/turt2live/matrix-media-repo/storage/datastore/datastore.go b/src/github.com/turt2live/matrix-media-repo/storage/datastore/datastore.go
index 0fe1cc61..4d029576 100644
--- a/src/github.com/turt2live/matrix-media-repo/storage/datastore/datastore.go
+++ b/src/github.com/turt2live/matrix-media-repo/storage/datastore/datastore.go
@@ -66,62 +66,6 @@ func GetUriForDatastore(dsConf config.DatastoreConfig) string {
 }
 
 func PickDatastore(ctx context.Context, log *logrus.Entry) (*DatastoreRef, error) {
-	// Legacy options first
-	storagePaths := config.Get().Uploads.StoragePaths
-	if len(storagePaths) > 0 {
-		log.Warn("Using legacy options to find a datastore")
-
-		if len(storagePaths) == 1 {
-			ds, err := storage.GetOrCreateDatastoreOfType(ctx, log, "file", storagePaths[0])
-			if err != nil {
-				return nil, err
-			}
-
-			fakeConfig := config.DatastoreConfig{
-				Type:       "file",
-				Enabled:    true,
-				ForUploads: true,
-				Options:    map[string]string{"path": ds.Uri},
-			}
-			return newDatastoreRef(ds, fakeConfig), nil
-		}
-
-		var basePath string
-		var pathSize int64
-		for i := 0; i < len(storagePaths); i++ {
-			currPath := storagePaths[i]
-			ds, err := storage.GetOrCreateDatastoreOfType(ctx, log, "file", currPath)
-			if err != nil {
-				continue
-			}
-
-			size, err := estimatedDatastoreSize(ds, ctx, log)
-			if err != nil {
-				continue
-			}
-
-			if basePath == "" || size < pathSize {
-				basePath = currPath
-				pathSize = size
-			}
-		}
-
-		if basePath != "" {
-			ds, err := storage.GetOrCreateDatastoreOfType(ctx, log, "file", basePath)
-			if err != nil {
-				return nil, err
-			}
-
-			fakeConfig := config.DatastoreConfig{
-				Type:       "file",
-				Enabled:    true,
-				ForUploads: true,
-				Options:    map[string]string{"path": ds.Uri},
-			}
-			return newDatastoreRef(ds, fakeConfig), nil
-		}
-	}
-
 	// If we haven't found a legacy option, pick a datastore
 	log.Info("Finding a suitable datastore to pick for uploads")
 	confDatastores := config.Get().DataStores
-- 
GitLab