Add file transfer progress

This commit is contained in:
Elara 2021-12-12 13:25:40 -08:00
parent db8f03dee7
commit 60a0daa82e
4 changed files with 38 additions and 4 deletions

View File

@ -12,6 +12,7 @@ type File struct {
fs *FS fs *FS
path string path string
offset uint32 offset uint32
offsetCh chan uint32
length uint32 length uint32
amtLeft uint32 amtLeft uint32
amtTferd uint32 amtTferd uint32
@ -22,7 +23,7 @@ type File struct {
// Open opens a file and returns it as an fs.File to // Open opens a file and returns it as an fs.File to
// satisfy the fs.FS interface // 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. // Make a read file request. This opens the file for reading.
err := blefs.request( err := blefs.request(
FSCmdReadFile, FSCmdReadFile,
@ -40,6 +41,7 @@ func (blefs *FS) Open(path string) (fs.File, error) {
path: path, path: path,
length: 0, length: 0,
offset: 0, offset: 0,
offsetCh: make(chan uint32, 5),
isReadOnly: true, isReadOnly: true,
isWriteOnly: false, isWriteOnly: false,
}, nil }, nil
@ -66,11 +68,23 @@ func (blefs *FS) Create(path string, size uint32) (*File, error) {
length: size, length: size,
amtLeft: size, amtLeft: size,
offset: 0, offset: 0,
offsetCh: make(chan uint32, 5),
isReadOnly: false, isReadOnly: false,
isWriteOnly: true, isWriteOnly: true,
}, nil }, 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 // Read reads data from a file into b
func (fl *File) Read(b []byte) (n int, err error) { func (fl *File) Read(b []byte) (n int, err error) {
// If file is write only (opened by FS.Create()) // 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 fl.length = resp.length
// Add returned chunk length to offset and amount transferred // Add returned chunk length to offset and amount transferred
fl.offset += resp.chunkLen fl.offset += resp.chunkLen
fl.offsetCh <- fl.offset
fl.amtTferd += resp.chunkLen fl.amtTferd += resp.chunkLen
// Calculate amount of bytes to be sent in next request // 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, chunkLen,
) )
} }
close(fl.offsetCh)
// Copy buffer contents to b // Copy buffer contents to b
copied := copy(b, buf) copied := copy(b, buf)
// Return amount of bytes copied with EOF error // 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 // Add chunk length to offset and amount transferred
fl.offset += chunkLen fl.offset += chunkLen
fl.offsetCh <- fl.offset
fl.amtTferd += chunkLen fl.amtTferd += chunkLen
} }
close(fl.offsetCh)
return int(fl.offset), nil return int(fl.offset), nil
} }

15
blefs/iofs.go Normal file
View 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}
}

1
go.mod
View File

@ -6,4 +6,5 @@ require (
github.com/fxamacker/cbor/v2 v2.3.0 github.com/fxamacker/cbor/v2 v2.3.0
github.com/godbus/dbus/v5 v5.0.3 github.com/godbus/dbus/v5 v5.0.3
github.com/muka/go-bluetooth v0.0.0-20211122080231-b99792bbe62a github.com/muka/go-bluetooth v0.0.0-20211122080231-b99792bbe62a
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
) )

5
go.sum
View File

@ -3,8 +3,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fxamacker/cbor v1.5.1 h1:XjQWBgdmQyqimslUh5r4tUGmoqzHmBFQOImkWGi2awg=
github.com/fxamacker/cbor v1.5.1/go.mod h1:3aPGItF174ni7dDzd6JZ206H8cmr4GDNBGpPa971zsU=
github.com/fxamacker/cbor/v2 v2.3.0 h1:aM45YGMctNakddNNAezPxDUpv38j44Abh+hifNuqXik= github.com/fxamacker/cbor/v2 v2.3.0 h1:aM45YGMctNakddNNAezPxDUpv38j44Abh+hifNuqXik=
github.com/fxamacker/cbor/v2 v2.3.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= github.com/fxamacker/cbor/v2 v2.3.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME= github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME=
@ -44,8 +42,9 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1 h1:sIky/MyNRSHTrdxfsiUSS4WIAMvInbeXljJz+jDjeYE=
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57iKi3297KFs0QDbGN60A=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200925191224-5d1fdd8fa346/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= golang.org/x/tools v0.0.0-20200925191224-5d1fdd8fa346/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=