From 71df8efc3823a9767eb82a4383c5dfec8a0e5eba Mon Sep 17 00:00:00 2001 From: Mark Karpeles Date: Tue, 14 Jan 2020 08:36:53 +0900 Subject: [PATCH] adding some doc, and comments --- idle.go | 2 +- part.go | 3 +++ read.go | 20 +++++++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/idle.go b/idle.go index eb2d3db..a4ac6f5 100644 --- a/idle.go +++ b/idle.go @@ -44,7 +44,7 @@ func (f *File) isComplete() bool { log.Printf("idle: file is now complete, marking as such") f.complete = true - f.savePart() + f.savePart() return true } diff --git a/part.go b/part.go index a7a5462..d8de5fc 100644 --- a/part.go +++ b/part.go @@ -6,6 +6,9 @@ import ( "os" ) +// SavePart triggers an immediate save of the download status to a .part file +// on disk, allowing resume to happen if the program terminates and is opened +// again. func (f *File) SavePart() error { f.lk.Lock() defer f.lk.Unlock() diff --git a/read.go b/read.go index 437be3d..a930cb2 100644 --- a/read.go +++ b/read.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "os" ) func (f *File) getSize() error { @@ -67,7 +68,20 @@ func (f *File) GetSize() (int64, error) { return f.size, nil } -// Seek in file for next Read() operation. +// Stat() will obtain the information of the underlying file after checking +// its size matches that of the file to download. +func (f *File) Stat() (os.FileInfo, error) { + _, err := f.GetSize() + if err != nil { + return nil, err + } + + return f.local.Stat() +} + +// Seek in file for next Read() operation. If you use io.SeekEnd but the file +// download hasn't started, a HEAD request will be made to obtain the file's +// size. func (f *File) Seek(offset int64, whence int) (int64, error) { f.lk.Lock() defer f.lk.Unlock() @@ -101,6 +115,8 @@ func (f *File) Seek(offset int64, whence int) (int64, error) { } } +// Read will read data from the file at the current position after checking +// it was successfully downloaded. func (f *File) Read(p []byte) (n int, err error) { // read data f.lk.Lock() @@ -113,6 +129,8 @@ func (f *File) Read(p []byte) (n int, err error) { return } +// ReadAt will read data from the disk at a specified offset after checking +// it was successfully downloaded. func (f *File) ReadAt(p []byte, off int64) (int, error) { f.lk.Lock() defer f.lk.Unlock()