move embed logic to ocispkg

This commit is contained in:
jkoberg
2021-10-18 13:01:17 +02:00
parent d2026a5d94
commit 88664152e3
16 changed files with 125 additions and 323 deletions
-55
View File
@@ -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,
}
}
+10
View File
@@ -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)