From 10203de79498b5c7e767416ebe965a30aa7f07fb Mon Sep 17 00:00:00 2001 From: Travis Ralston <travpc@gmail.com> Date: Sun, 1 Mar 2020 19:16:51 -0700 Subject: [PATCH] Support a datastore kind of 'all' Fixes https://github.com/turt2live/matrix-media-repo/issues/225 --- CHANGELOG.md | 4 +++- common/media_kinds.go | 14 ++++++++++++++ storage/datastore/datastore.go | 8 +------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b85cbd9..4473662d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] -*Nothing yet*. +### Added + +* Added support for a `forKinds: ["all"]` option on datastores. ## [1.0.1] - February 27, 2020 diff --git a/common/media_kinds.go b/common/media_kinds.go index 4ea68567..5421b051 100644 --- a/common/media_kinds.go +++ b/common/media_kinds.go @@ -4,5 +4,19 @@ const KindLocalMedia = "local_media" const KindRemoteMedia = "remote_media" const KindThumbnails = "thumbnails" const KindArchives = "archives" +const KindAll = "all" var AllKinds = []string{KindLocalMedia, KindRemoteMedia, KindThumbnails} + +func IsKind(have string, want string) bool { + return have == want || have == KindAll +} + +func HasKind(have []string, want string) bool { + for _, k := range have { + if IsKind(k, want) { + return true + } + } + return false +} diff --git a/storage/datastore/datastore.go b/storage/datastore/datastore.go index 76ebc304..6274c3e7 100644 --- a/storage/datastore/datastore.go +++ b/storage/datastore/datastore.go @@ -105,13 +105,7 @@ func PickDatastore(forKind string, ctx rcontext.RequestContext) (*DatastoreRef, dsConf.MediaKinds = common.AllKinds } - allowed := false - for _, k := range dsConf.MediaKinds { - if k == forKind { - allowed = true - break - } - } + allowed := common.HasKind(dsConf.MediaKinds, forKind) if !allowed { continue } -- GitLab