diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-06-27 18:52:38 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-06-27 18:52:38 (GMT) |
commit | 92b427a54c5782fea71af3709ac564ba4dc67e57 (patch) | |
tree | d57896e8be36a52ee52c8b29e0ce51f84e19f02d | |
parent | 70a371f16e7a92821161e771a40b63c84fd194ea (diff) |
changed signature of progress callback
-rw-r--r-- | rhimport/core.go | 2 | ||||
-rw-r--r-- | rhimport/fetcher.go | 31 | ||||
-rw-r--r-- | rhimport/importer.go | 8 | ||||
-rw-r--r-- | rhimport/session.go | 9 |
4 files changed, 22 insertions, 28 deletions
diff --git a/rhimport/core.go b/rhimport/core.go index 6fb0bcb..7880d95 100644 --- a/rhimport/core.go +++ b/rhimport/core.go @@ -53,7 +53,7 @@ func init() { fetcherInit() } -type ProgressCB func(step int, stepName string, progress float64, userdata interface{}) bool +type ProgressCB func(step int, stepName string, current, total float64, userdata interface{}) bool type DoneCB func(result Result, userdata interface{}) bool type Result struct { diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index 0b1c3b2..d923f24 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -112,7 +112,7 @@ func curlProgressCallback(dltotal, dlnow, ultotal, ulnow float64, userdata inter } if data.ctx.ProgressCallBack != nil { - if keep := data.ctx.ProgressCallBack(1, "downloading", dlnow/dltotal, data.ctx.ProgressCallBackData); !keep { + if keep := data.ctx.ProgressCallBack(1, "downloading", dlnow, dltotal, data.ctx.ProgressCallBackData); !keep { data.ctx.ProgressCallBack = nil } } @@ -293,11 +293,6 @@ func fetchFileTmp(ctx *Context, res *Result, uri *url.URL) (err error) { func fetchFileDir(ctx *Context, res *Result, uri *url.URL, dir string) (err error) { rhl.Printf("Dir fetcher called for '%s'", ctx.SourceUri) - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "fetching", 0.0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } ctx.SourceFile = filepath.Join(dir, path.Clean("/"+uri.Path)) var src *os.File @@ -306,20 +301,29 @@ func fetchFileDir(ctx *Context, res *Result, uri *url.URL, dir string) (err erro res.ErrorString = fmt.Sprintf("local-file open(): %s", err) return nil } + size := float64(0) if info, err := src.Stat(); err != nil { res.ResponseCode = http.StatusBadRequest res.ErrorString = fmt.Sprintf("local-file stat(): %s", err) return nil } else { + size = float64(info.Size()) if info.IsDir() { res.ResponseCode = http.StatusBadRequest res.ErrorString = fmt.Sprintf("'%s' is a directory", ctx.SourceFile) return nil } } + if ctx.ProgressCallBack != nil { + if keep := ctx.ProgressCallBack(1, "fetching", 0.0, size, ctx.ProgressCallBackData); !keep { + ctx.ProgressCallBack = nil + } + } + src.Close() + if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "fetching", 1.0, ctx.ProgressCallBackData); !keep { + if keep := ctx.ProgressCallBack(1, "fetching", size, size, ctx.ProgressCallBackData); !keep { ctx.ProgressCallBack = nil } } @@ -348,14 +352,14 @@ func fetchFileFake(ctx *Context, res *Result, uri *url.URL) error { return nil } if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "faking", float64(i)/float64(duration), ctx.ProgressCallBackData); !keep { + if keep := ctx.ProgressCallBack(1, "faking", float64(i), float64(duration), ctx.ProgressCallBackData); !keep { ctx.ProgressCallBack = nil } } time.Sleep(100 * time.Millisecond) } if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "faking", 1.0, ctx.ProgressCallBackData); !keep { + if keep := ctx.ProgressCallBack(1, "faking", float64(duration), float64(duration), ctx.ProgressCallBackData); !keep { ctx.ProgressCallBack = nil } } @@ -396,13 +400,8 @@ func writeAttachmentFile(ctx *Context, res *Result, sizeTotal uint64, src *os.Fi } written += w - p := float64(written) / float64(sizeTotal) - if p > 1.0 { - p = 1.0 - } - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "receiving", p, ctx.ProgressCallBackData); !keep { + if keep := ctx.ProgressCallBack(1, "receiving", float64(written), float64(sizeTotal), ctx.ProgressCallBackData); !keep { ctx.ProgressCallBack = nil } } @@ -443,7 +442,7 @@ func fetchFileAttachment(ctx *Context, res *Result, uri *url.URL) error { defer src.Close() if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "receiving", 0.0, ctx.ProgressCallBackData); !keep { + if keep := ctx.ProgressCallBack(1, "receiving", 0.0, float64(sizeTotal), ctx.ProgressCallBackData); !keep { ctx.ProgressCallBack = nil } } diff --git a/rhimport/importer.go b/rhimport/importer.go index 73d446e..5ffeef5 100644 --- a/rhimport/importer.go +++ b/rhimport/importer.go @@ -312,7 +312,7 @@ func importAudio(ctx *Context, res *Result) (err error) { } if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(2, "importing", ulnow/ultotal, ctx.ProgressCallBackData); !keep { + if keep := ctx.ProgressCallBack(2, "importing", ulnow, ultotal, ctx.ProgressCallBackData); !keep { ctx.ProgressCallBack = nil } } @@ -451,12 +451,6 @@ func ImportFile(ctx *Context) (res *Result, err error) { rhl.Printf("importer: ImportFile called with: show-id: %d, pool-name: '%s', cart/cut: %d/%d", ctx.ShowId, ctx.GroupName, ctx.Cart, ctx.Cut) - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(2, "importing", 0.0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } - // TODO: on trusted interfaces we should call getPassword again with cached=false after 401's if ctx.Trusted { if err = ctx.getPassword(true); err != nil { diff --git a/rhimport/session.go b/rhimport/session.go index 84d283e..ece5303 100644 --- a/rhimport/session.go +++ b/rhimport/session.go @@ -70,7 +70,8 @@ type SessionDoneCB struct { type ProgressData struct { Step int StepName string - Progress float64 + Current float64 + Total float64 } type sessionAddProgressHandlerResponse struct { @@ -93,9 +94,9 @@ type sessionAddDoneHandlerRequest struct { response chan<- sessionAddDoneHandlerResponse } -func sessionProgressCallback(step int, stepName string, progress float64, userdata interface{}) bool { +func sessionProgressCallback(step int, stepName string, current, total float64, userdata interface{}) bool { out := userdata.(chan<- ProgressData) - out <- ProgressData{step, stepName, progress} + out <- ProgressData{step, stepName, current, total} return true } @@ -163,7 +164,7 @@ func (self *Session) addDoneHandler(userdata interface{}, cb DoneCB) (resp sessi func (self *Session) callProgressHandler(p *ProgressData) { for _, cb := range self.progressCBs { if cb.cb != nil { - if keep := cb.cb(p.Step, p.StepName, p.Progress, cb.userdata); !keep { + if keep := cb.cb(p.Step, p.StepName, p.Current, p.Total, cb.userdata); !keep { cb.cb = nil } } |