package storage import ( "os" "path/filepath" "github.com/spf13/afero" ) type IStorageProvider interface { StoreRaw(bucketName string, objectName string, data []byte) (string, error) StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error) } type FileSystemStorageProvider struct { fileSystem afero.Fs basePath string } func (sp FileSystemStorageProvider) StoreRaw(bucketName string, objectName string, data []byte) (string, error) { directoryPath := filepath.Join(sp.basePath, bucketName) if err := sp.fileSystem.MkdirAll(directoryPath, os.ModePerm); err != nil { return "", err } filePath := filepath.Join(directoryPath, objectName) if err := afero.WriteFile(sp.fileSystem, filePath, data, os.ModePerm); err != nil { return "", err } return filePath, nil } func (sp FileSystemStorageProvider) StoreExisting(bucketName string, objectName string, existingFilePath string) (string, error) { bytesRead, _ := afero.ReadFile(sp.fileSystem, existingFilePath) return sp.StoreRaw(bucketName, objectName, bytesRead) } func GetFileSystemStorageProvider(basePath string) FileSystemStorageProvider { wd, _ := os.Getwd() return FileSystemStorageProvider{ fileSystem: afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(wd, "files")), basePath: basePath, } } // TODO: Move this out of this file func GetMemoryStorageProvider() FileSystemStorageProvider { return FileSystemStorageProvider{ fileSystem: afero.NewBasePathFs(afero.NewMemMapFs(), "/"), basePath: "/tmp/foo/bar", } }