From 3426c29e2e4835875d0ae935dd8bd774b60d669d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 16 Sep 2021 15:52:11 +0200 Subject: [PATCH] Fix concurrent registration of mimetypes (#2077) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- changelog/unreleased/fix-concurrent-mimetype-map.md | 5 +++++ pkg/mime/mime.go | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 changelog/unreleased/fix-concurrent-mimetype-map.md diff --git a/changelog/unreleased/fix-concurrent-mimetype-map.md b/changelog/unreleased/fix-concurrent-mimetype-map.md new file mode 100644 index 00000000000..82ea865422d --- /dev/null +++ b/changelog/unreleased/fix-concurrent-mimetype-map.md @@ -0,0 +1,5 @@ +Bugfix: Fix concurrent registration of mimetypes + +We fixed registering mimetypes in the mime package when starting multiple storage providers in the same process. + +https://github.com/cs3org/reva/pull/2077 diff --git a/pkg/mime/mime.go b/pkg/mime/mime.go index 3e8a0844430..69084503082 100644 --- a/pkg/mime/mime.go +++ b/pkg/mime/mime.go @@ -20,23 +20,24 @@ package mime import ( "path" + "sync" gomime "github.com/cubewise-code/go-mime" ) const defaultMimeDir = "httpd/unix-directory" -var mimes map[string]string +var mimes sync.Map func init() { - mimes = map[string]string{} + mimes = sync.Map{} } // RegisterMime is a package level function that registers // a mime type with the given extension. // TODO(labkode): check that we do not override mime type mappings? func RegisterMime(ext, mime string) { - mimes[ext] = mime + mimes.Store(ext, mime) } // Detect returns the mimetype associated with the given filename. @@ -61,5 +62,8 @@ func Detect(isDir bool, fn string) string { } func getCustomMime(ext string) string { - return mimes[ext] + if m, ok := mimes.Load(ext); ok { + return m.(string) + } + return "" }