From a3a28f0d2edfa71e159d4b5e7b554782c1258a13 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sat, 15 Jan 2022 14:48:38 +0100 Subject: [PATCH] Return created file path in store methods --- storage/storage.go | 13 ++++++++----- storage/storage_test.go | 6 ++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/storage/storage.go b/storage/storage.go index efb1db9..bc77f19 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -8,8 +8,8 @@ import ( ) type IStorageProvider interface { - storeRaw(bucketName string, objectName string, data []byte) - storeExisting(bucketName string, objectName string, existingFilePath string) + storeRaw(bucketName string, objectName string, data []byte) string + storeExisting(bucketName string, objectName string, existingFilePath string) string } type FileSystemStorageProvider struct { @@ -17,7 +17,7 @@ type FileSystemStorageProvider struct { basePath string } -func (sp FileSystemStorageProvider) storeRaw(bucketName string, objectName string, data []byte) { +func (sp FileSystemStorageProvider) storeRaw(bucketName string, objectName string, data []byte) string { directoryPath := filepath.Join(sp.basePath, bucketName) sp.fileSystem.MkdirAll(directoryPath, os.ModePerm) @@ -25,9 +25,12 @@ func (sp FileSystemStorageProvider) storeRaw(bucketName string, objectName strin filePath := filepath.Join(directoryPath, objectName) afero.WriteFile(sp.fileSystem, filePath, data, os.ModePerm) + + return filePath } -func (sp FileSystemStorageProvider) storeExisting(bucketName string, objectName string, existingFilePath string) { +func (sp FileSystemStorageProvider) storeExisting(bucketName string, objectName string, existingFilePath string) string { bytesRead, _ := afero.ReadFile(sp.fileSystem, existingFilePath) - sp.storeRaw(bucketName, objectName, bytesRead) + + return sp.storeRaw(bucketName, objectName, bytesRead) } diff --git a/storage/storage_test.go b/storage/storage_test.go index 403df5e..bf2c515 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -19,7 +19,8 @@ func TestFileSystemStorageProvider(t *testing.T) { basePath: "/tmp/foo/bar", } - provider.storeRaw("test", "test.bin", dummyData) + finalPath := provider.storeRaw("test", "test.bin", dummyData) + assert.Equal(t, "/tmp/foo/bar/test/test.bin", finalPath) exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin") assert.True(t, exists) @@ -38,7 +39,8 @@ func TestFileSystemStorageProvider(t *testing.T) { basePath: "/tmp/foo/bar", } - provider.storeExisting("test", "test.bin", "/tmp/existing.bin") + finalPath := provider.storeExisting("test", "test.bin", "/tmp/existing.bin") + assert.Equal(t, "/tmp/foo/bar/test/test.bin", finalPath) exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin") assert.True(t, exists)