diff --git a/accounts/accounts.go b/accounts/accounts.go index b1ecc525f..bab5b32d4 100644 --- a/accounts/accounts.go +++ b/accounts/accounts.go @@ -2,21 +2,7 @@ package accounts import ( "embed" - "io/fs" - "net/http" ) //go:embed assets/* -var assets embed.FS - -// Assets FS -var Assets http.FileSystem - -func init() { - embedFS, err := fs.Sub(assets, "assets") - if err != nil { - panic(err) - } - - Assets = http.FS(embedFS) -} \ No newline at end of file +var Assets embed.FS diff --git a/accounts/pkg/assets/assets.go b/accounts/pkg/assets/assets.go deleted file mode 100644 index 601769a97..000000000 --- a/accounts/pkg/assets/assets.go +++ /dev/null @@ -1,54 +0,0 @@ -package assets - -import ( - "github.com/owncloud/ocis/accounts" - "net/http" - "os" - "path" - - "github.com/owncloud/ocis/accounts/pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" -) - -// assets gets initialized by New and provides the handler. -type assets struct { - logger log.Logger - config *config.Config -} - -// Open just implements the HTTP filesystem interface. -func (a assets) Open(original string) (http.File, error) { - if a.config.Asset.Path != "" { - if stat, err := os.Stat(a.config.Asset.Path); err == nil && stat.IsDir() { - custom := path.Join( - a.config.Asset.Path, - original, - ) - - if _, err := os.Stat(custom); !os.IsNotExist(err) { - f, err := os.Open(custom) - - if err != nil { - return nil, err - } - - return f, nil - } - } else { - a.logger.Warn(). - Str("path", a.config.Asset.Path). - Msg("Assets directory doesn't exist") - } - } - - return accounts.Assets.Open(original) -} - -// New returns a new http filesystem to serve assets. -func New(opts ...Option) http.FileSystem { - options := newOptions(opts...) - - return assets{ - config: options.Config, - } -} diff --git a/accounts/pkg/assets/option.go b/accounts/pkg/assets/option.go index 0ebcdba1d..de885fba8 100644 --- a/accounts/pkg/assets/option.go +++ b/accounts/pkg/assets/option.go @@ -1,10 +1,20 @@ package assets import ( + "net/http" + + "github.com/owncloud/ocis/accounts" "github.com/owncloud/ocis/accounts/pkg/config" + "github.com/owncloud/ocis/ocis-pkg/assetsfs" "github.com/owncloud/ocis/ocis-pkg/log" ) +// New returns a new http filesystem to serve assets. +func New(opts ...Option) http.FileSystem { + options := newOptions(opts...) + return assetsfs.New(accounts.Assets, options.Config.Asset.Path, options.Logger) +} + // Option defines a single option function. type Option func(o *Options) diff --git a/graph-explorer/graph_explorer.go b/graph-explorer/graph_explorer.go index 5367fdc8f..afd013842 100644 --- a/graph-explorer/graph_explorer.go +++ b/graph-explorer/graph_explorer.go @@ -2,21 +2,7 @@ package graphexplorer import ( "embed" - "io/fs" - "net/http" ) //go:embed assets/* -var assets embed.FS - -// Assets FS -var Assets http.FileSystem - -func init() { - embedFS, err := fs.Sub(assets, "assets") - if err != nil { - panic(err) - } - - Assets = http.FS(embedFS) -} +var Assets embed.FS diff --git a/graph-explorer/pkg/assets/assets.go b/graph-explorer/pkg/assets/assets.go deleted file mode 100644 index 072565a29..000000000 --- a/graph-explorer/pkg/assets/assets.go +++ /dev/null @@ -1,29 +0,0 @@ -package assets - -import ( - graph_explorer "github.com/owncloud/ocis/graph-explorer" - "github.com/owncloud/ocis/graph-explorer/pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" - "net/http" -) - -// assets gets initialized by New and provides the handler. -type assets struct { - logger log.Logger - config *config.Config -} - -// Open just implements the HTTP filesystem interface. -func (a assets) Open(original string) (http.File, error) { - return graph_explorer.Assets.Open(original) -} - -// New returns a new http filesystem to serve assets. -func New(opts ...Option) http.FileSystem { - options := newOptions(opts...) - - return assets{ - logger: options.Logger, - config: options.Config, - } -} diff --git a/graph-explorer/pkg/assets/option.go b/graph-explorer/pkg/assets/option.go index b9ff238a1..228dff7fc 100644 --- a/graph-explorer/pkg/assets/option.go +++ b/graph-explorer/pkg/assets/option.go @@ -1,10 +1,20 @@ package assets import ( + "net/http" + + graphexplorer "github.com/owncloud/ocis/graph-explorer" "github.com/owncloud/ocis/graph-explorer/pkg/config" + "github.com/owncloud/ocis/ocis-pkg/assetsfs" "github.com/owncloud/ocis/ocis-pkg/log" ) +// New returns a new http filesystem to serve assets. +func New(opts ...Option) http.FileSystem { + options := newOptions(opts...) + return assetsfs.New(graphexplorer.Assets, "", options.Logger) +} + // Option defines a single option function. type Option func(o *Options) diff --git a/idp/idp.go b/idp/idp.go index f33530aca..380e20d84 100644 --- a/idp/idp.go +++ b/idp/idp.go @@ -2,21 +2,7 @@ package idp import ( "embed" - "io/fs" - "net/http" ) //go:embed assets/* -var assets embed.FS - -// Assets FS -var Assets http.FileSystem - -func init() { - embedFS, err := fs.Sub(assets, "assets") - if err != nil { - panic(err) - } - - Assets = http.FS(embedFS) -} \ No newline at end of file +var Assets embed.FS diff --git a/idp/pkg/assets/assets.go b/idp/pkg/assets/assets.go deleted file mode 100644 index c1995e27a..000000000 --- a/idp/pkg/assets/assets.go +++ /dev/null @@ -1,55 +0,0 @@ -package assets - -import ( - "github.com/owncloud/ocis/idp" - "net/http" - "os" - "path" - - "github.com/owncloud/ocis/idp/pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" -) - -// assets gets initialized by New and provides the handler. -type assets struct { - logger log.Logger - config *config.Config -} - -// Open just implements the HTTP filesystem interface. -func (a assets) Open(original string) (http.File, error) { - if a.config.Asset.Path != "" { - if stat, err := os.Stat(a.config.Asset.Path); err == nil && stat.IsDir() { - custom := path.Join( - a.config.Asset.Path, - original, - ) - - if _, err := os.Stat(custom); !os.IsNotExist(err) { - f, err := os.Open(custom) - - if err != nil { - return nil, err - } - - return f, nil - } - } else { - a.logger.Warn(). - Str("path", a.config.Asset.Path). - Msg("Assets directory doesn't exist") - } - } - - return idp.Assets.Open(original) -} - -// New returns a new http filesystem to serve assets. -func New(opts ...Option) http.FileSystem { - options := newOptions(opts...) - - return assets{ - logger: options.Logger, - config: options.Config, - } -} diff --git a/idp/pkg/assets/option.go b/idp/pkg/assets/option.go index 54f1eebf5..e2b4e3a23 100644 --- a/idp/pkg/assets/option.go +++ b/idp/pkg/assets/option.go @@ -1,10 +1,20 @@ package assets import ( + "net/http" + + "github.com/owncloud/ocis/idp" "github.com/owncloud/ocis/idp/pkg/config" + "github.com/owncloud/ocis/ocis-pkg/assetsfs" "github.com/owncloud/ocis/ocis-pkg/log" ) +// New returns a new http filesystem to serve assets. +func New(opts ...Option) http.FileSystem { + options := newOptions(opts...) + return assetsfs.New(idp.Assets, options.Config.Asset.Path, options.Logger) +} + // Option defines a single option function. type Option func(o *Options) diff --git a/ocis-pkg/assetsfs/assetsfs.go b/ocis-pkg/assetsfs/assetsfs.go new file mode 100644 index 000000000..3d2653b8a --- /dev/null +++ b/ocis-pkg/assetsfs/assetsfs.go @@ -0,0 +1,70 @@ +package assetsfs + +import ( + "embed" + "fmt" + "io/fs" + "net/http" + "os" + "path" + + "github.com/owncloud/ocis/ocis-pkg/log" +) + +// FileSystem customized to load assets +type FileSystem struct { + fs http.FileSystem + assetPath string + log log.Logger +} + +// Open checks if assetPath is set and tries to load from there. Falls back to fs if that is not possible +func (f *FileSystem) Open(original string) (http.File, error) { + if f.assetPath != "" { + file, err := read(f.assetPath, original) + if err == nil { + return file, nil + } + f.log.Warn(). + Str("path", f.assetPath). + Str("filename", original). + Str("error", err.Error()). + Msg("error reading from assetPath") + } + + return f.fs.Open(original) +} + +// New initializes a new FileSystem. Quits on error +func New(embedFS embed.FS, assetPath string, logger log.Logger) *FileSystem { + f, err := fs.Sub(embedFS, "assets") + if err != nil { + fmt.Println("Cannot load subtree fs:", err.Error()) + os.Exit(1) + } + + return &FileSystem{ + fs: http.FS(f), + assetPath: assetPath, + log: logger, + } +} + +// tries to read file from disk or errors +func read(assetPath string, fileName string) (http.File, error) { + if stat, err := os.Stat(assetPath); err != nil || !stat.IsDir() { + return nil, fmt.Errorf("can't load asset path: %s", err) + } + + p := path.Join(assetPath, fileName) + if _, err := os.Stat(p); err != nil { + return nil, err + } + + f, err := os.Open(p) + if err != nil { + return nil, err + } + + return f, nil +} diff --git a/settings/pkg/assets/assets.go b/settings/pkg/assets/assets.go deleted file mode 100644 index 18b565b20..000000000 --- a/settings/pkg/assets/assets.go +++ /dev/null @@ -1,55 +0,0 @@ -package assets - -import ( - "net/http" - "os" - "path/filepath" - - "github.com/owncloud/ocis/settings" - - "github.com/owncloud/ocis/ocis-pkg/log" - "github.com/owncloud/ocis/settings/pkg/config" -) - -// assets gets initialized by New and provides the handler. -type assets struct { - logger log.Logger - config *config.Config -} - -// Open just implements the HTTP filesystem interface. -func (a assets) Open(original string) (http.File, error) { - if a.config.Asset.Path != "" { - if stat, err := os.Stat(a.config.Asset.Path); err == nil && stat.IsDir() { - custom := filepath.Join( - a.config.Asset.Path, - original, - ) - - if _, err := os.Stat(custom); !os.IsNotExist(err) { - f, err := os.Open(custom) - - if err != nil { - return nil, err - } - - return f, nil - } - } else { - a.logger.Warn(). - Str("path", a.config.Asset.Path). - Msg("Assets directory doesn't exist") - } - } - - return settings.Assets.Open(original) -} - -// New returns a new http filesystem to serve assets. -func New(opts ...Option) http.FileSystem { - options := newOptions(opts...) - - return assets{ - config: options.Config, - } -} diff --git a/settings/pkg/assets/option.go b/settings/pkg/assets/option.go index 592aa796f..3b23576b2 100644 --- a/settings/pkg/assets/option.go +++ b/settings/pkg/assets/option.go @@ -1,10 +1,20 @@ package assets import ( + "net/http" + + "github.com/owncloud/ocis/ocis-pkg/assetsfs" "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/owncloud/ocis/settings" "github.com/owncloud/ocis/settings/pkg/config" ) +// New returns a new http filesystem to serve assets. +func New(opts ...Option) http.FileSystem { + options := newOptions(opts...) + return assetsfs.New(settings.Assets, options.Config.Asset.Path, options.Logger) +} + // Option defines a single option function. type Option func(o *Options) diff --git a/settings/settings.go b/settings/settings.go index 07841a118..b31c92527 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -2,21 +2,7 @@ package settings import ( "embed" - "io/fs" - "net/http" ) //go:embed assets/* -var assets embed.FS - -// Assets FS -var Assets http.FileSystem - -func init() { - embedFS, err := fs.Sub(assets, "assets") - if err != nil { - panic(err) - } - - Assets = http.FS(embedFS) -} +var Assets embed.FS diff --git a/web/pkg/assets/assets.go b/web/pkg/assets/assets.go deleted file mode 100644 index 1555a5401..000000000 --- a/web/pkg/assets/assets.go +++ /dev/null @@ -1,55 +0,0 @@ -package assets - -import ( - "github.com/owncloud/ocis/web" - "net/http" - "os" - "path" - - "github.com/owncloud/ocis/ocis-pkg/log" - "github.com/owncloud/ocis/web/pkg/config" -) - -// assets gets initialized by New and provides the handler. -type assets struct { - logger log.Logger - config *config.Config -} - -// Open just implements the HTTP filesystem interface. -func (a assets) Open(original string) (http.File, error) { - if a.config.Asset.Path != "" { - if stat, err := os.Stat(a.config.Asset.Path); err == nil && stat.IsDir() { - custom := path.Join( - a.config.Asset.Path, - original, - ) - - if _, err := os.Stat(custom); !os.IsNotExist(err) { - f, err := os.Open(custom) - - if err != nil { - return nil, err - } - - return f, nil - } - } else { - a.logger.Fatal(). - Str("path", a.config.Asset.Path). - Msg("assets directory doesn't exist") - } - } - - return web.Assets.Open(original) -} - -// New returns a new http filesystem to serve assets. -func New(opts ...Option) http.FileSystem { - options := newOptions(opts...) - - return assets{ - logger: options.Logger, - config: options.Config, - } -} diff --git a/web/pkg/assets/option.go b/web/pkg/assets/option.go index b87aae6e5..233ca8435 100644 --- a/web/pkg/assets/option.go +++ b/web/pkg/assets/option.go @@ -1,10 +1,20 @@ package assets import ( + "net/http" + + "github.com/owncloud/ocis/ocis-pkg/assetsfs" "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/owncloud/ocis/web" "github.com/owncloud/ocis/web/pkg/config" ) +// New returns a new http filesystem to serve assets. +func New(opts ...Option) http.FileSystem { + options := newOptions(opts...) + return assetsfs.New(web.Assets, options.Config.Asset.Path, options.Logger) +} + // Option defines a single option function. type Option func(o *Options) diff --git a/web/web.go b/web/web.go index 7b1b9e509..743bf5721 100644 --- a/web/web.go +++ b/web/web.go @@ -2,22 +2,8 @@ package web import ( "embed" - "io/fs" - "net/http" ) //go:embed assets/* //go:embed assets/js/* -var assets embed.FS - -// Assets FS -var Assets http.FileSystem - -func init() { - embedFS, err := fs.Sub(assets, "assets") - if err != nil { - panic(err) - } - - Assets = http.FS(embedFS) -} \ No newline at end of file +var Assets embed.FS