From 15ba6e9695eb26214d69961a363d7bbf57ea24c9 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 25 Jan 2024 16:30:35 -0500 Subject: [PATCH] feat: create interface for File --- interfaces/file.go | 19 +++++++++++++++++++ interfaces/storage.go | 1 + storage/file.go | 32 ++++++++++++++++---------------- 3 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 interfaces/file.go diff --git a/interfaces/file.go b/interfaces/file.go new file mode 100644 index 0000000..f7bfa44 --- /dev/null +++ b/interfaces/file.go @@ -0,0 +1,19 @@ +package interfaces + +import ( + "git.lumeweb.com/LumeWeb/libs5-go/encoding" + "git.lumeweb.com/LumeWeb/portal/db/models" + "io" + "time" +) + +type File interface { + Record() (*models.Upload, error) + Hash() []byte + HashString() string + Name() string + Modtime() time.Time + Size() uint64 + CID() *encoding.CID + io.ReadSeekCloser +} diff --git a/interfaces/storage.go b/interfaces/storage.go index a5afc7b..fb36313 100644 --- a/interfaces/storage.go +++ b/interfaces/storage.go @@ -27,5 +27,6 @@ type StorageService interface { DeleteTusUpload(uploadID string) error ScheduleTusUpload(uploadID string, attempt int) error Tus() *tusd.Handler + NewFile(hash []byte) File Service } diff --git a/storage/file.go b/storage/file.go index e7ccbff..1e1aea6 100644 --- a/storage/file.go +++ b/storage/file.go @@ -12,10 +12,10 @@ import ( ) var ( - _ io.ReadSeekCloser = (*File)(nil) + _ interfaces.File = (*FileImpl)(nil) ) -type File struct { +type FileImpl struct { reader io.ReadCloser hash []byte storage interfaces.StorageService @@ -24,17 +24,17 @@ type File struct { read bool } -func NewFile(hash []byte, storage interfaces.StorageService) *File { - return &File{hash: hash, storage: storage, read: false} +func NewFile(hash []byte, storage interfaces.StorageService) *FileImpl { + return &FileImpl{hash: hash, storage: storage, read: false} } -func (f *File) Exists() bool { +func (f *FileImpl) Exists() bool { exists, _ := f.storage.FileExists(f.hash) return exists } -func (f *File) Read(p []byte) (n int, err error) { +func (f *FileImpl) Read(p []byte) (n int, err error) { err = f.init(0) if err != nil { return 0, err @@ -44,7 +44,7 @@ func (f *File) Read(p []byte) (n int, err error) { return f.reader.Read(p) } -func (f *File) Seek(offset int64, whence int) (int64, error) { +func (f *FileImpl) Seek(offset int64, whence int) (int64, error) { switch whence { case io.SeekStart: if !f.read { @@ -73,7 +73,7 @@ func (f *File) Seek(offset int64, whence int) (int64, error) { return 0, nil } -func (f *File) Close() error { +func (f *FileImpl) Close() error { if f.reader != nil { r := f.reader f.reader = nil @@ -83,7 +83,7 @@ func (f *File) Close() error { return nil } -func (f *File) init(offset int64) error { +func (f *FileImpl) init(offset int64) error { if f.reader == nil { reader, _, err := f.storage.GetFile(f.hash, offset) if err != nil { @@ -97,7 +97,7 @@ func (f *File) init(offset int64) error { return nil } -func (f *File) Record() (*models.Upload, error) { +func (f *FileImpl) Record() (*models.Upload, error) { if f.record == nil { exists, record := f.storage.FileExists(f.hash) @@ -111,7 +111,7 @@ func (f *File) Record() (*models.Upload, error) { return f.record, nil } -func (f *File) Hash() []byte { +func (f *FileImpl) Hash() []byte { hashStr := f.HashString() if hashStr == "" { @@ -126,7 +126,7 @@ func (f *File) Hash() []byte { return str } -func (f *File) HashString() string { +func (f *FileImpl) HashString() string { record, err := f.Record() if err != nil { return "" @@ -135,13 +135,13 @@ func (f *File) HashString() string { return record.Hash } -func (f *File) Name() string { +func (f *FileImpl) Name() string { cid, _ := f.CID().ToString() return cid } -func (f *File) Modtime() time.Time { +func (f *FileImpl) Modtime() time.Time { record, err := f.Record() if err != nil { return time.Unix(0, 0) @@ -149,7 +149,7 @@ func (f *File) Modtime() time.Time { return record.CreatedAt } -func (f *File) Size() uint64 { +func (f *FileImpl) Size() uint64 { record, err := f.Record() if err != nil { return 0 @@ -157,7 +157,7 @@ func (f *File) Size() uint64 { return record.Size } -func (f *File) CID() *encoding.CID { +func (f *FileImpl) CID() *encoding.CID { if f.cid == nil { multihash := encoding.MultihashFromBytes(f.Hash(), types.HashTypeBlake3) cid := encoding.NewCID(types.CIDTypeRaw, *multihash, f.Size())