2024-03-02 03:49:47 +00:00
|
|
|
package s5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/fs"
|
2024-03-03 13:29:07 +00:00
|
|
|
"path"
|
2024-03-02 03:49:47 +00:00
|
|
|
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ fs.FS = (*webAppFs)(nil)
|
|
|
|
|
|
|
|
type webAppFs struct {
|
|
|
|
root *encoding.CID
|
|
|
|
s5 *S5API
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w webAppFs) Open(name string) (fs.File, error) {
|
|
|
|
file := w.s5.newFile(FileParams{
|
|
|
|
Hash: w.root.Hash.HashBytes(),
|
|
|
|
Type: w.root.Type,
|
|
|
|
})
|
|
|
|
|
|
|
|
manifest, err := file.Manifest()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
webApp, ok := manifest.(*metadata.WebAppMetadata)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("manifest is not a web app")
|
|
|
|
}
|
|
|
|
|
2024-03-03 12:38:34 +00:00
|
|
|
if name == "." {
|
|
|
|
return w.s5.newFile(FileParams{
|
|
|
|
Hash: w.root.Hash.HashBytes(),
|
|
|
|
Type: w.root.Type,
|
|
|
|
Name: name,
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2024-03-03 08:41:30 +00:00
|
|
|
item, ok := webApp.Paths.Get(name)
|
2024-03-02 03:49:47 +00:00
|
|
|
|
2024-03-03 12:38:34 +00:00
|
|
|
if !ok {
|
2024-03-03 13:29:07 +00:00
|
|
|
name = path.Join(name, "index.html")
|
|
|
|
item, ok = webApp.Paths.Get(name)
|
|
|
|
if !ok {
|
|
|
|
return nil, fs.ErrNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.s5.newFile(FileParams{
|
|
|
|
Hash: item.Cid.Hash.HashBytes(),
|
|
|
|
Type: item.Cid.Type,
|
|
|
|
Name: name,
|
|
|
|
Root: w.root.Hash.HashBytes(),
|
|
|
|
RootType: w.root.Type,
|
|
|
|
}), nil
|
2024-03-02 03:49:47 +00:00
|
|
|
}
|
|
|
|
return w.s5.newFile(FileParams{
|
|
|
|
Hash: item.Cid.Hash.HashBytes(),
|
|
|
|
Type: item.Cid.Type,
|
|
|
|
Name: name,
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWebAppFs(root *encoding.CID, s5 *S5API) *webAppFs {
|
|
|
|
return &webAppFs{
|
|
|
|
root: root,
|
|
|
|
s5: s5,
|
|
|
|
}
|
|
|
|
}
|