Browse Source

Add storage provider GetPath method

feature/update-route-registration
Roman Zipp 2 years ago
committed by Fabian Vowie
parent
commit
eb4b74cbde
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 11
      storage/storage.go
  2. 14
      storage/storage_test.go

11
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,
}
}

14
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"))
})
}
Loading…
Cancel
Save