2024-01-25 00:07:35 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2024-02-09 20:42:53 +00:00
|
|
|
"context"
|
2024-02-01 01:29:27 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2024-02-16 13:53:53 +00:00
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2024-02-01 01:29:27 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/db/models"
|
2024-02-09 20:42:53 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/renter"
|
|
|
|
"go.sia.tech/renterd/api"
|
2024-01-25 00:07:35 +00:00
|
|
|
)
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
type FileImpl struct {
|
2024-01-25 00:07:35 +00:00
|
|
|
reader io.ReadCloser
|
|
|
|
hash []byte
|
2024-02-01 01:29:27 +00:00
|
|
|
storage *StorageServiceDefault
|
2024-02-09 20:42:53 +00:00
|
|
|
renter *renter.RenterDefault
|
2024-01-25 00:07:35 +00:00
|
|
|
record *models.Upload
|
2024-01-25 00:22:31 +00:00
|
|
|
cid *encoding.CID
|
2024-01-25 00:47:25 +00:00
|
|
|
read bool
|
2024-01-25 00:07:35 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 20:42:53 +00:00
|
|
|
type FileParams struct {
|
|
|
|
Storage *StorageServiceDefault
|
|
|
|
Renter *renter.RenterDefault
|
|
|
|
Hash []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFile(params FileParams) *FileImpl {
|
|
|
|
return &FileImpl{hash: params.Hash, storage: params.Storage, renter: params.Renter, read: false}
|
2024-01-25 00:07:35 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) Exists() bool {
|
2024-01-25 00:07:35 +00:00
|
|
|
exists, _ := f.storage.FileExists(f.hash)
|
|
|
|
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) Read(p []byte) (n int, err error) {
|
2024-01-25 00:07:35 +00:00
|
|
|
err = f.init(0)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2024-01-25 00:47:25 +00:00
|
|
|
f.read = true
|
2024-01-25 00:07:35 +00:00
|
|
|
|
|
|
|
return f.reader.Read(p)
|
|
|
|
}
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) Seek(offset int64, whence int) (int64, error) {
|
2024-01-25 00:07:35 +00:00
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
2024-01-25 00:47:25 +00:00
|
|
|
if !f.read {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2024-01-25 00:07:35 +00:00
|
|
|
if f.reader != nil {
|
|
|
|
err := f.reader.Close()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
f.reader = nil
|
|
|
|
}
|
|
|
|
err := f.init(offset)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2024-01-25 00:14:06 +00:00
|
|
|
case io.SeekCurrent:
|
2024-01-25 00:07:35 +00:00
|
|
|
return 0, errors.New("not supported")
|
2024-01-25 00:14:06 +00:00
|
|
|
case io.SeekEnd:
|
|
|
|
return int64(f.Size()), nil
|
2024-01-25 00:07:35 +00:00
|
|
|
default:
|
|
|
|
return 0, errors.New("invalid whence")
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) Close() error {
|
2024-01-25 00:07:35 +00:00
|
|
|
if f.reader != nil {
|
|
|
|
r := f.reader
|
|
|
|
f.reader = nil
|
|
|
|
return r.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) init(offset int64) error {
|
2024-01-25 00:07:35 +00:00
|
|
|
if f.reader == nil {
|
|
|
|
reader, _, err := f.storage.GetFile(f.hash, offset)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.reader = reader
|
2024-01-25 00:58:10 +00:00
|
|
|
f.read = false
|
2024-01-25 00:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) Record() (*models.Upload, error) {
|
2024-01-25 00:07:35 +00:00
|
|
|
if f.record == nil {
|
|
|
|
exists, record := f.storage.FileExists(f.hash)
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.New("file does not exist")
|
|
|
|
}
|
|
|
|
|
2024-02-16 13:53:53 +00:00
|
|
|
f.record = record
|
2024-01-25 00:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return f.record, nil
|
|
|
|
}
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) Hash() []byte {
|
2024-01-25 00:07:35 +00:00
|
|
|
hashStr := f.HashString()
|
|
|
|
|
|
|
|
if hashStr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
str, err := hex.DecodeString(hashStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) HashString() string {
|
2024-01-25 00:07:35 +00:00
|
|
|
record, err := f.Record()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return record.Hash
|
|
|
|
}
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) Name() string {
|
2024-01-25 00:23:17 +00:00
|
|
|
cid, _ := f.CID().ToString()
|
|
|
|
|
|
|
|
return cid
|
2024-01-25 00:07:35 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) Modtime() time.Time {
|
2024-01-25 00:07:35 +00:00
|
|
|
record, err := f.Record()
|
|
|
|
if err != nil {
|
|
|
|
return time.Unix(0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return record.CreatedAt
|
|
|
|
}
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) Size() uint64 {
|
2024-01-25 00:14:06 +00:00
|
|
|
record, err := f.Record()
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return record.Size
|
|
|
|
}
|
2024-01-25 21:30:35 +00:00
|
|
|
func (f *FileImpl) CID() *encoding.CID {
|
2024-01-25 00:22:31 +00:00
|
|
|
if f.cid == nil {
|
|
|
|
multihash := encoding.MultihashFromBytes(f.Hash(), types.HashTypeBlake3)
|
|
|
|
cid := encoding.NewCID(types.CIDTypeRaw, *multihash, f.Size())
|
|
|
|
f.cid = cid
|
|
|
|
}
|
|
|
|
return f.cid
|
|
|
|
}
|
2024-01-26 00:17:48 +00:00
|
|
|
|
|
|
|
func (f *FileImpl) Mime() string {
|
|
|
|
record, err := f.Record()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return record.MimeType
|
|
|
|
}
|
2024-02-09 20:43:38 +00:00
|
|
|
|
|
|
|
func (f *FileImpl) Protocol() string {
|
|
|
|
record, err := f.Record()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return record.Protocol
|
|
|
|
}
|
|
|
|
func (f *FileImpl) Proof() ([]byte, error) {
|
|
|
|
object, err := f.renter.GetObject(context.Background(), f.Protocol(), f.HashString(), api.DownloadObjectOptions{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
proof, err := io.ReadAll(object.Content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = object.Content.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return proof, nil
|
|
|
|
}
|