From 3dbd7913147f80944ad8c37f70a44d190f8c2733 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 1 Mar 2024 22:49:17 -0500 Subject: [PATCH] feat: add S5FileInfo struct and refactorings needed to be used as a fs.File and in a fs.Fs --- api/s5/file.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/api/s5/file.go b/api/s5/file.go index 7697153..c66489f 100644 --- a/api/s5/file.go +++ b/api/s5/file.go @@ -5,8 +5,11 @@ import ( "encoding/hex" "errors" "io" + "io/fs" "time" + s5libmetadata "git.lumeweb.com/LumeWeb/libs5-go/metadata" + "git.lumeweb.com/LumeWeb/portal/protocols/s5" "git.lumeweb.com/LumeWeb/portal/metadata" @@ -18,6 +21,8 @@ import ( ) var _ io.ReadSeekCloser = (*S5File)(nil) +var _ fs.File = (*S5File)(nil) +var _ fs.FileInfo = (*S5FileInfo)(nil) type S5File struct { reader io.ReadCloser @@ -31,6 +36,7 @@ type S5File struct { read bool tus *s5.TusHandler ctx context.Context + name string } type FileParams struct { @@ -40,6 +46,7 @@ type FileParams struct { Type types.CIDType Protocol *s5.S5Protocol Tus *s5.TusHandler + Name string } func NewFile(params FileParams) *S5File { @@ -51,6 +58,7 @@ func NewFile(params FileParams) *S5File { protocol: params.Protocol, tus: params.Tus, ctx: context.Background(), + name: params.Name, } } @@ -174,6 +182,10 @@ func (f *S5File) HashString() string { } func (f *S5File) Name() string { + if f.name != "" { + return f.name + } + cid, _ := f.CID().ToString() return cid @@ -242,3 +254,49 @@ func (f *S5File) Proof() ([]byte, error) { return proof, nil } +func (f *S5File) Manifest() (s5libmetadata.Metadata, error) { + meta, err := f.protocol.Node().Services().Storage().GetMetadataByCID(f.CID()) + if err != nil { + return nil, err + } + + return meta, nil +} + +func (f *S5File) Stat() (fs.FileInfo, error) { + return newS5FileInfo(f), nil +} + +type S5FileInfo struct { + file *S5File +} + +func (s S5FileInfo) Name() string { + return s.file.Name() +} + +func (s S5FileInfo) Size() int64 { + return int64(s.file.Size()) +} + +func (s S5FileInfo) Mode() fs.FileMode { + return 0 +} + +func (s S5FileInfo) ModTime() time.Time { + return s.file.Modtime() +} + +func (s S5FileInfo) IsDir() bool { + return s.file.typ == types.CIDTypeDirectory +} + +func (s S5FileInfo) Sys() any { + return nil +} + +func newS5FileInfo(file *S5File) *S5FileInfo { + return &S5FileInfo{ + file: file, + } +}