fix: add a read state so we can noop a seek when we have not done anything yet

This commit is contained in:
Derrick Hammer 2024-01-24 19:47:25 -05:00
parent 14d8760c1f
commit 84bb08144b
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 7 additions and 1 deletions

View File

@ -21,10 +21,11 @@ type File struct {
storage interfaces.StorageService storage interfaces.StorageService
record *models.Upload record *models.Upload
cid *encoding.CID cid *encoding.CID
read bool
} }
func NewFile(hash []byte, storage interfaces.StorageService) *File { func NewFile(hash []byte, storage interfaces.StorageService) *File {
return &File{hash: hash, storage: storage} return &File{hash: hash, storage: storage, read: false}
} }
func (f *File) Exists() bool { func (f *File) Exists() bool {
@ -38,6 +39,7 @@ func (f *File) Read(p []byte) (n int, err error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
f.read = true
return f.reader.Read(p) return f.reader.Read(p)
} }
@ -45,6 +47,10 @@ func (f *File) Read(p []byte) (n int, err error) {
func (f *File) Seek(offset int64, whence int) (int64, error) { func (f *File) Seek(offset int64, whence int) (int64, error) {
switch whence { switch whence {
case io.SeekStart: case io.SeekStart:
if !f.read {
return 0, nil
}
if f.reader != nil { if f.reader != nil {
err := f.reader.Close() err := f.reader.Close()
if err != nil { if err != nil {