Browse Source

Return created file path in store methods

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

13
storage/storage.go

@ -8,8 +8,8 @@ import (
) )
type IStorageProvider interface { 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 { type FileSystemStorageProvider struct {
@ -17,7 +17,7 @@ type FileSystemStorageProvider struct {
basePath string 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) directoryPath := filepath.Join(sp.basePath, bucketName)
sp.fileSystem.MkdirAll(directoryPath, os.ModePerm) sp.fileSystem.MkdirAll(directoryPath, os.ModePerm)
@ -25,9 +25,12 @@ func (sp FileSystemStorageProvider) storeRaw(bucketName string, objectName strin
filePath := filepath.Join(directoryPath, objectName) filePath := filepath.Join(directoryPath, objectName)
afero.WriteFile(sp.fileSystem, filePath, data, os.ModePerm) 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) bytesRead, _ := afero.ReadFile(sp.fileSystem, existingFilePath)
sp.storeRaw(bucketName, objectName, bytesRead)
return sp.storeRaw(bucketName, objectName, bytesRead)
} }

6
storage/storage_test.go

@ -19,7 +19,8 @@ func TestFileSystemStorageProvider(t *testing.T) {
basePath: "/tmp/foo/bar", 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") exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin")
assert.True(t, exists) assert.True(t, exists)
@ -38,7 +39,8 @@ func TestFileSystemStorageProvider(t *testing.T) {
basePath: "/tmp/foo/bar", 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") exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin")
assert.True(t, exists) assert.True(t, exists)

Loading…
Cancel
Save