From b7456c5beafeeba5357db25eac195b93313647fe Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 15 Jul 2016 15:10:29 +0200 Subject: simplified progress reports diff --git a/rhimport/core.go b/rhimport/core.go index 92e798b..d5d7549 100644 --- a/rhimport/core.go +++ b/rhimport/core.go @@ -256,3 +256,11 @@ func (ctx *Context) updateCutCartTitle() (err error) { } return ctx.db.UpdateCutCartTitle(ctx.Cart, ctx.Cut, ctx.GroupName, filename) } + +func (ctx *Context) reportProgress(step int, stepName string, current, total float64) { + if ctx.ProgressCallBack != nil { + if keep := ctx.ProgressCallBack(step, stepName, current, total, ctx.Cart, ctx.Cut, ctx.ProgressCallBackData); !keep { + ctx.ProgressCallBack = nil + } + } +} diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index c9bfc2f..b4380fa 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -103,12 +103,7 @@ func curlWriteCallback(ptr []byte, userdata interface{}) bool { data.writeError = fmt.Errorf("file exceeds maximum file size") return false } - - if data.ctx.ProgressCallBack != nil { - if keep := data.ctx.ProgressCallBack(1, "downloading", float64(data.written), data.totalSize, 0, 0, data.ctx.ProgressCallBackData); !keep { - data.ctx.ProgressCallBack = nil - } - } + data.ctx.reportProgress(1, "downloading", float64(data.written), data.totalSize) return true } @@ -447,12 +442,7 @@ func fetchFileDirConvert(ctx *Context, res *Result, origSrc *os.File, sizeTotal } written += uint64(w) - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "fetching", float64(written), float64(sizeTotal), 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } - + ctx.reportProgress(1, "fetching", float64(written), float64(sizeTotal)) if ctx.Cancel != nil && len(ctx.Cancel) > 0 { res.ResponseCode = http.StatusNoContent res.ErrorString = "canceled" @@ -533,12 +523,7 @@ func fetchFileDir(ctx *Context, res *Result, uri *url.URL, dir string, convert b return nil } - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "fetching", 0.0, float64(size), 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } - + ctx.reportProgress(1, "fetching", 0.0, float64(size)) if convert { if err = fetchFileDirConvert(ctx, res, src, size); err != nil { return @@ -549,12 +534,7 @@ func fetchFileDir(ctx *Context, res *Result, uri *url.URL, dir string, convert b ctx.DeleteSourceDir = false } } - - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "fetching", float64(size), float64(size), 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } + ctx.reportProgress(1, "fetching", float64(size), float64(size)) return } @@ -575,18 +555,12 @@ func fetchFileFake(ctx *Context, res *Result, uri *url.URL) error { res.ErrorString = "canceled" return nil } - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "faking", float64(i), float64(duration), 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } + + ctx.reportProgress(1, "faking", float64(i), float64(duration)) time.Sleep(100 * time.Millisecond) } - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "faking", float64(duration), float64(duration), 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } + ctx.reportProgress(1, "faking", float64(duration), float64(duration)) + ctx.SourceFile = "/nonexistend/fake.mp3" ctx.OrigFilename = ctx.SourceFile if ctx.SourceFilePolicy == Auto { @@ -626,12 +600,7 @@ func writeAttachmentFile(ctx *Context, res *Result, sizeTotal uint64, conv Fetch } written += uint64(w) - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "receiving", float64(written), float64(sizeTotal), 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } - + ctx.reportProgress(1, "receiving", float64(written), float64(sizeTotal)) if uint64(written) >= sizeTotal { return nil } @@ -672,12 +641,7 @@ func fetchFileAttachment(ctx *Context, res *Result, uri *url.URL) error { return err } - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "receiving", 0.0, float64(sizeTotal), 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } - + ctx.reportProgress(1, "receiving", 0.0, float64(sizeTotal)) err = writeAttachmentFile(ctx, res, sizeTotal, conv) conv.Close() if res.ResponseCode == http.StatusNoContent { diff --git a/rhimport/importer.go b/rhimport/importer.go index bcaf84f..37bbc01 100644 --- a/rhimport/importer.go +++ b/rhimport/importer.go @@ -311,11 +311,7 @@ func importAudio(ctx *Context, res *Result) (err error) { return false } - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(3, "importing", ulnow, ultotal, ctx.Cart, ctx.Cut, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } + ctx.reportProgress(3, "importing", ulnow, ultotal) return true }) easy.Setopt(curl.OPT_PROGRESSDATA, ctx) diff --git a/rhimport/normalizer.go b/rhimport/normalizer.go index 3f56151..422acaa 100644 --- a/rhimport/normalizer.go +++ b/rhimport/normalizer.go @@ -71,11 +71,7 @@ func runNormalizer(ctx *Context, src *os.File, size int64) (err error) { break } written += w - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(2, "normalizing", float64(written), float64(size), 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } + ctx.reportProgress(2, "normalizing", float64(written), float64(size)) } ffstdin.Close() res := <-ffresult @@ -91,11 +87,7 @@ func runNormalizer(ctx *Context, src *os.File, size int64) (err error) { func NormalizeFile(ctx *Context) (err error) { if ctx.LoudnessCorr == 0.0 { rhl.Println("NormalizeFile: skipping normalization since the gain = 0.0dB") - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(2, "normalizing", 1.0, 1.0, 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } + ctx.reportProgress(2, "normalizing", 1.0, 1.0) return } @@ -114,12 +106,7 @@ func NormalizeFile(ctx *Context) (err error) { size = info.Size() } - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(2, "normalizing", 0.0, float64(size), 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } - + ctx.reportProgress(2, "normalizing", 0.0, float64(size)) if err = runNormalizer(ctx, src, size); err != nil { rhl.Println("NormalizeFile error:", err) if ctx.DeleteSourceFile { @@ -130,12 +117,7 @@ func NormalizeFile(ctx *Context) (err error) { } return } - - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(2, "normalizing", float64(size), float64(size), 0, 0, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } + ctx.reportProgress(2, "normalizing", float64(size), float64(size)) return } -- cgit v0.10.2