forked from Elara6331/infinitime
Add file transfer progress
This commit is contained in:
@@ -12,6 +12,7 @@ type File struct {
|
||||
fs *FS
|
||||
path string
|
||||
offset uint32
|
||||
offsetCh chan uint32
|
||||
length uint32
|
||||
amtLeft uint32
|
||||
amtTferd uint32
|
||||
@@ -22,7 +23,7 @@ type File struct {
|
||||
|
||||
// Open opens a file and returns it as an fs.File to
|
||||
// satisfy the fs.FS interface
|
||||
func (blefs *FS) Open(path string) (fs.File, error) {
|
||||
func (blefs *FS) Open(path string) (*File, error) {
|
||||
// Make a read file request. This opens the file for reading.
|
||||
err := blefs.request(
|
||||
FSCmdReadFile,
|
||||
@@ -40,6 +41,7 @@ func (blefs *FS) Open(path string) (fs.File, error) {
|
||||
path: path,
|
||||
length: 0,
|
||||
offset: 0,
|
||||
offsetCh: make(chan uint32, 5),
|
||||
isReadOnly: true,
|
||||
isWriteOnly: false,
|
||||
}, nil
|
||||
@@ -66,11 +68,23 @@ func (blefs *FS) Create(path string, size uint32) (*File, error) {
|
||||
length: size,
|
||||
amtLeft: size,
|
||||
offset: 0,
|
||||
offsetCh: make(chan uint32, 5),
|
||||
isReadOnly: false,
|
||||
isWriteOnly: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Size returns the total size of the opened file
|
||||
func (file *File) Size() uint32 {
|
||||
return file.length
|
||||
}
|
||||
|
||||
// Progress returns a channel that receives the amount
|
||||
// of bytes sent as they are sent
|
||||
func (file *File) Progress() <-chan uint32 {
|
||||
return file.offsetCh
|
||||
}
|
||||
|
||||
// Read reads data from a file into b
|
||||
func (fl *File) Read(b []byte) (n int, err error) {
|
||||
// If file is write only (opened by FS.Create())
|
||||
@@ -149,6 +163,7 @@ func (fl *File) Read(b []byte) (n int, err error) {
|
||||
fl.length = resp.length
|
||||
// Add returned chunk length to offset and amount transferred
|
||||
fl.offset += resp.chunkLen
|
||||
fl.offsetCh <- fl.offset
|
||||
fl.amtTferd += resp.chunkLen
|
||||
|
||||
// Calculate amount of bytes to be sent in next request
|
||||
@@ -168,6 +183,7 @@ func (fl *File) Read(b []byte) (n int, err error) {
|
||||
chunkLen,
|
||||
)
|
||||
}
|
||||
close(fl.offsetCh)
|
||||
// Copy buffer contents to b
|
||||
copied := copy(b, buf)
|
||||
// Return amount of bytes copied with EOF error
|
||||
@@ -262,8 +278,11 @@ func (fl *File) Write(b []byte) (n int, err error) {
|
||||
)
|
||||
// Add chunk length to offset and amount transferred
|
||||
fl.offset += chunkLen
|
||||
fl.offsetCh <- fl.offset
|
||||
fl.amtTferd += chunkLen
|
||||
}
|
||||
|
||||
close(fl.offsetCh)
|
||||
return int(fl.offset), nil
|
||||
}
|
||||
|
||||
|
||||
15
blefs/iofs.go
Normal file
15
blefs/iofs.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package blefs
|
||||
|
||||
import "io/fs"
|
||||
|
||||
type goFS struct {
|
||||
*FS
|
||||
}
|
||||
|
||||
func (iofs goFS) Open(path string) (fs.File, error) {
|
||||
return iofs.FS.Open(path)
|
||||
}
|
||||
|
||||
func (blefs *FS) GoFS() fs.FS {
|
||||
return goFS{blefs}
|
||||
}
|
||||
Reference in New Issue
Block a user