Browse Source

Add storage provider GetPath method

feature/update-route-registration
Roman Zipp 3 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" "github.com/spf13/afero"
) )
const StorageFolderName = "files"
type IStorageProvider interface { type IStorageProvider interface {
StoreRaw(bucketName string, objectName string, data []byte) (string, error) StoreRaw(bucketName string, objectName string, data []byte) (string, error)
StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error) StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error)
GetPath(bucketName string, objectName string) string
} }
type FileSystemStorageProvider struct { type FileSystemStorageProvider struct {
fileSystem afero.Fs fileSystem afero.Fs
basePath string basePath string
wd string
} }
func (sp FileSystemStorageProvider) StoreRaw(bucketName string, objectName string, data []byte) (string, error) { 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) 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 { func GetFileSystemStorageProvider(basePath string, wd string) FileSystemStorageProvider {
if wd == "" { if wd == "" {
wd, _ = os.Getwd() wd, _ = os.Getwd()
} }
return FileSystemStorageProvider{ return FileSystemStorageProvider{
fileSystem: afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(wd, "files")),
fileSystem: afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(wd, StorageFolderName)),
basePath: basePath, 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") content, _ := afero.ReadFile(fileSystem, "/tmp/foo/bar/test/test.bin")
assert.Equal(t, dummyData, content) 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