github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/assets/dynamic/fs.go (about) 1 package dynamic 2 3 import ( 4 "context" 5 "fmt" 6 "net/url" 7 "path/filepath" 8 "time" 9 10 "github.com/cozy/cozy-stack/pkg/assets/model" 11 "github.com/cozy/cozy-stack/pkg/config/config" 12 ) 13 14 // DynamicAssetsFolderName is the folder name for dynamic assets 15 const DynamicAssetsFolderName = "dyn-assets" 16 17 var assetFS AssetsFS 18 19 // AssetsFS is the interface implemented by all the implementations handling assets. 20 // 21 // At the moment there two separate implementations: 22 // - [SwiftFS] allowing to manage assets via an OpenStack Swift API. 23 // - [AferoFS] with [NewOsFS] allowing to manage assets directly on the host filesystem. 24 // - [AferoFS] with [NewInMemory] allowing to manage assets directly in a in-memory session. 25 type AssetsFS interface { 26 Add(string, string, *model.Asset) error 27 Get(string, string) ([]byte, error) 28 Remove(string, string) error 29 List() (map[string][]*model.Asset, error) 30 CheckStatus(ctx context.Context) (time.Duration, error) 31 } 32 33 // InitDynamicAssetFS initializes the dynamic asset FS. 34 func InitDynamicAssetFS(fsURL string) error { 35 u, err := url.Parse(fsURL) 36 if err != nil { 37 return err 38 } 39 40 switch u.Scheme { 41 case config.SchemeMem: 42 assetFS = NewInMemoryFS() 43 44 case config.SchemeFile: 45 assetFS, err = NewOsFS(filepath.Join(u.Path, DynamicAssetsFolderName)) 46 if err != nil { 47 return err 48 } 49 50 case config.SchemeSwift, config.SchemeSwiftSecure: 51 assetFS, err = NewSwiftFS() 52 if err != nil { 53 return err 54 } 55 56 default: 57 return fmt.Errorf("Invalid scheme %s for dynamic assets FS", u.Scheme) 58 } 59 60 return nil 61 }