From eb4b74cbde4284e696e46f60b823590bee223009 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Mon, 17 Jan 2022 19:23:35 +0100 Subject: [PATCH] Add storage provider GetPath method --- storage/storage.go | 11 ++++++++++- storage/storage_test.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/storage/storage.go b/storage/storage.go index f7c7b8a..313ba26 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -7,14 +7,18 @@ import ( "github.com/spf13/afero" ) +const StorageFolderName = "files" + type IStorageProvider interface { StoreRaw(bucketName string, objectName string, data []byte) (string, error) StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error) + GetPath(bucketName string, objectName string) string } type FileSystemStorageProvider struct { fileSystem afero.Fs basePath string + wd string } func (sp FileSystemStorageProvider) StoreRaw(bucketName string, objectName string, data []byte) (string, error) { @@ -42,14 +46,19 @@ func (sp FileSystemStorageProvider) StoreExisting(bucketName string, objectName return sp.StoreRaw(bucketName, objectName, bytesRead) } +func (sp FileSystemStorageProvider) GetPath(bucketName string, objectName string) string { + return filepath.Join(sp.wd, StorageFolderName, sp.basePath, bucketName, objectName) +} + func GetFileSystemStorageProvider(basePath string, wd string) FileSystemStorageProvider { if wd == "" { wd, _ = os.Getwd() } return FileSystemStorageProvider{ - fileSystem: afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(wd, "files")), + fileSystem: afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(wd, StorageFolderName)), basePath: basePath, + wd: wd, } } diff --git a/storage/storage_test.go b/storage/storage_test.go index 9e177fb..6b6a025 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -51,4 +51,18 @@ func TestFileSystemStorageProvider(t *testing.T) { content, _ := afero.ReadFile(fileSystem, "/tmp/foo/bar/test/test.bin") assert.Equal(t, dummyData, content) }) + + t.Run("getPath method returns correct path", func(t *testing.T) { + fileSystem := afero.NewMemMapFs() + + provider := FileSystemStorageProvider{ + fileSystem: fileSystem, + basePath: "/tmp/foo/bar", + } + + _, err := provider.StoreRaw("test", "test.bin", dummyData) + assert.Nil(t, err) + + assert.Equal(t, "files/tmp/foo/bar/test/test.bin", provider.GetPath("test", "test.bin")) + }) }