From dfe242b0cd7987117eab680b94d5b9d7f17cf172 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Fri, 30 Mar 2018 19:13:09 +0200 Subject: [PATCH] Add UploadedImagesDir config setting --- README.md | 2 ++ config/config.go | 17 +++++++++++++++-- config/config_test.go | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8a66187..5d17d20 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ defaultuser: # on database creation, gotify creates an admin user name: admin # the username of the default user pass: admin # the password of the default user passstrength: 10 # the bcrypt password strength (higher = better but also slower) +uploadedimagesdir: images # the directory for storing uploaded images ``` ### Environment @@ -86,6 +87,7 @@ GOTIFY_DATABASE_CONNECTION=gotify.db GOTIFY_DEFAULTUSER_NAME=admin GOTIFY_DEFAULTUSER_PASS=admin GOTIFY_PASSSTRENGTH=10 +GOTIFY_UPLOADEDIMAGESDIR=images ``` ### Database diff --git a/config/config.go b/config/config.go index d69edaa..6a02c72 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,11 @@ package config -import "github.com/jinzhu/configor" +import ( + "path/filepath" + "strings" + + "github.com/jinzhu/configor" +) // Configuration is stuff that can be configured externally per env variables or config file (config.yml). type Configuration struct { @@ -28,12 +33,20 @@ type Configuration struct { Name string `default:"admin"` Pass string `default:"admin"` } - PassStrength int `default:"10"` + PassStrength int `default:"10"` + UploadedImagesDir string `default:"images"` } // Get returns the configuration extracted from env variables or config file. func Get() *Configuration { conf := new(Configuration) configor.New(&configor.Config{ENVPrefix: "GOTIFY"}).Load(conf, "config.yml", "/etc/gotify/config.yml") + addTrailingSlashToPaths(conf) return conf } + +func addTrailingSlashToPaths(conf *Configuration) { + if !strings.HasSuffix(conf.UploadedImagesDir, "/") && !strings.HasSuffix(conf.UploadedImagesDir, "\\") { + conf.UploadedImagesDir = conf.UploadedImagesDir + string(filepath.Separator) + } +} diff --git a/config/config_test.go b/config/config_test.go index 73f55bc..099cbbf 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -4,6 +4,8 @@ import ( "os" "testing" + "path/filepath" + "github.com/stretchr/testify/assert" ) @@ -18,6 +20,20 @@ func TestConfigEnv(t *testing.T) { os.Unsetenv("GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTS") } +func TestAddSlash(t *testing.T) { + os.Setenv("GOTIFY_UPLOADEDIMAGESDIR", "../data/images") + conf := Get() + assert.Equal(t, "../data/images"+string(filepath.Separator), conf.UploadedImagesDir) + os.Unsetenv("GOTIFY_UPLOADEDIMAGESDIR") +} + +func TestNotAddSlash(t *testing.T) { + os.Setenv("GOTIFY_UPLOADEDIMAGESDIR", "../data/") + conf := Get() + assert.Equal(t, "../data/", conf.UploadedImagesDir) + os.Unsetenv("GOTIFY_UPLOADEDIMAGESDIR") +} + func TestConfigFile(t *testing.T) { file, err := os.Create("config.yml") defer func() {