From 1bfc42615897dd0c053bd7fddaca042f2a5ddad6 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sat, 15 Jan 2022 14:37:11 +0100 Subject: [PATCH] Add method to store locally existing files to storage wrapper --- storage/storage.go | 16 ++++++++++++++-- storage/storage_test.go | 21 +++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/storage/storage.go b/storage/storage.go index e8f52b0..79079fe 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -8,7 +8,8 @@ import ( ) type IStorageProvider interface { - store(bucketName string, objectName string, data []byte) + storeRaw(bucketName string, objectName string, data []byte) + storeExisting(bucketName string, objectName string, existingFilePath string) } type FileSystemStorageProvider struct { @@ -16,7 +17,7 @@ type FileSystemStorageProvider struct { basePath string } -func (sp FileSystemStorageProvider) store(bucketName string, objectName string, data []byte) { +func (sp FileSystemStorageProvider) storeRaw(bucketName string, objectName string, data []byte) { directoryPath := filepath.Join(sp.basePath, bucketName) sp.fileSystem.MkdirAll(directoryPath, os.ModePerm) @@ -25,3 +26,14 @@ func (sp FileSystemStorageProvider) store(bucketName string, objectName string, afero.WriteFile(sp.fileSystem, filePath, data, os.ModePerm) } + +func (sp FileSystemStorageProvider) storeExisting(bucketName string, objectName string, existingFilePath string) { + directoryPath := filepath.Join(sp.basePath, bucketName) + + sp.fileSystem.MkdirAll(directoryPath, os.ModePerm) + + filePath := filepath.Join(directoryPath, objectName) + + bytesRead, _ := afero.ReadFile(sp.fileSystem, existingFilePath) + afero.WriteFile(sp.fileSystem, filePath, bytesRead, os.ModePerm) +} diff --git a/storage/storage_test.go b/storage/storage_test.go index b7e8a9e..535946f 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -1,6 +1,7 @@ package storage import ( + "os" "testing" "github.com/spf13/afero" @@ -8,7 +9,7 @@ import ( ) func TestFileSystemStorageProvider(t *testing.T) { - t.Run("Store method stores files in filesystem", func(t *testing.T) { + t.Run("storeRaw method stores files in filesystem", func(t *testing.T) { fileSystem := afero.NewMemMapFs() dummyData := []byte{0x13, 0x37} @@ -18,7 +19,23 @@ func TestFileSystemStorageProvider(t *testing.T) { basePath: "/tmp/foo/bar", } - provider.store("test", "test.bin", dummyData) + provider.storeRaw("test", "test.bin", dummyData) + + exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin") + assert.True(t, exists) + }) + + t.Run("storeExisting method stores files in filesystem", func(t *testing.T) { + fileSystem := afero.NewMemMapFs() + + afero.WriteFile(fileSystem, "/tmp/existing.bin", []byte{0x13, 0x37}, os.ModePerm) + + provider := FileSystemStorageProvider{ + fileSystem: fileSystem, + basePath: "/tmp/foo/bar", + } + + provider.storeExisting("test", "test.bin", "/tmp/existing.bin") exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin") assert.True(t, exists)