From 5478acfc09dfa4b59546e883917ccb70cce68b06 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 29 Sep 2024 19:04:25 +0200 Subject: [PATCH] fix(files): configure the files path in files init instead of globally This fixes a regression introduced in daa7ad053c35a97933ca79aee007c388538bab5d where the root path would be included twice in the file path, leading to retrieval issues. --- pkg/config/config.go | 7 ------- pkg/files/filehandling.go | 14 ++++++++++++++ pkg/files/files.go | 6 ------ pkg/models/task_attachment_test.go | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 7e9b77097..f361e8fd5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -487,13 +487,6 @@ func InitConfig() { log.Warning("service.enablemetrics is deprecated and will be removed in a future release. Please use metrics.enable.") MetricsEnabled.Set(true) } - - if !strings.HasPrefix(FilesBasePath.GetString(), "/") { - FilesBasePath.Set(filepath.Join( - ServiceRootpath.GetString(), - FilesBasePath.GetString(), - )) - } } func random(length int) (string, error) { diff --git a/pkg/files/filehandling.go b/pkg/files/filehandling.go index 1d6f59c07..a422b1f42 100644 --- a/pkg/files/filehandling.go +++ b/pkg/files/filehandling.go @@ -18,8 +18,11 @@ package files import ( "os" + "path/filepath" + "strings" "testing" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/keyvalue" @@ -32,16 +35,27 @@ import ( var fs afero.Fs var afs *afero.Afero +func setDefaultConfig() { + if !strings.HasPrefix(config.FilesBasePath.GetString(), "/") { + config.FilesBasePath.Set(filepath.Join( + config.ServiceRootpath.GetString(), + config.FilesBasePath.GetString(), + )) + } +} + // InitFileHandler creates a new file handler for the file backend we want to use func InitFileHandler() { fs = afero.NewOsFs() afs = &afero.Afero{Fs: fs} + setDefaultConfig() } // InitTestFileHandler initializes a new memory file system for testing func InitTestFileHandler() { fs = afero.NewMemMapFs() afs = &afero.Afero{Fs: fs} + setDefaultConfig() } func initFixtures(t *testing.T) { diff --git a/pkg/files/files.go b/pkg/files/files.go index d34565a88..695ec052d 100644 --- a/pkg/files/files.go +++ b/pkg/files/files.go @@ -22,7 +22,6 @@ import ( "os" "path/filepath" "strconv" - "strings" "time" "code.vikunja.io/api/pkg/config" @@ -59,12 +58,7 @@ func (*File) TableName() string { } func (f *File) getAbsoluteFilePath() string { - base := "" - if !strings.HasPrefix(config.FilesBasePath.GetString(), "/") { - base = config.ServiceRootpath.GetString() - } return filepath.Join( - base, config.FilesBasePath.GetString(), strconv.FormatInt(f.ID, 10), ) diff --git a/pkg/models/task_attachment_test.go b/pkg/models/task_attachment_test.go index 2ede7544c..c7e5f1044 100644 --- a/pkg/models/task_attachment_test.go +++ b/pkg/models/task_attachment_test.go @@ -51,7 +51,7 @@ func TestTaskAttachment_ReadOne(t *testing.T) { // Load the actual attachment file and check its content err = ta.File.LoadFileByID() require.NoError(t, err) - assert.Equal(t, filepath.Join(config.ServiceRootpath.GetString(), config.FilesBasePath.GetString(), "/1"), ta.File.File.Name()) + assert.Equal(t, filepath.Join(config.ServiceRootpath.GetString(), "files", "1"), ta.File.File.Name()) content := make([]byte, 9) read, err := ta.File.File.Read(content) require.NoError(t, err)