Browse Source

Add method to store locally existing files to storage wrapper

feature/add-storage-layer
Fabian Vowie 2 years ago
parent
commit
1bfc426158
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 16
      storage/storage.go
  2. 21
      storage/storage_test.go

16
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)
}

21
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)

Loading…
Cancel
Save