From 4b0cb427306cb5bab60e6abdd16dbd7bd8df2a85 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 10 Dec 2015 17:30:40 +0100 Subject: added support for progress callbacks diff --git a/fetcher.go b/fetcher.go index b4e2f0c..03cf3da 100644 --- a/fetcher.go +++ b/fetcher.go @@ -33,7 +33,6 @@ import ( "os" "path" "strings" - "time" ) type CurlCBData struct { @@ -107,12 +106,11 @@ func FetchFileCurl(ctx *ImportContext, uri *url.URL) (err error) { easy.Setopt(curl.OPT_WRITEDATA, cbdata) easy.Setopt(curl.OPT_NOPROGRESS, false) - started := int64(0) easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool { - if started == 0 { - started = time.Now().Unix() + ctx := userdata.(*ImportContext) + if ctx.ProgressCallBack != nil { + ctx.ProgressCallBack(1, "downloading", dlnow/dltotal, ctx.ProgressCallBackData) } - fmt.Printf("Downloaded: %3.2f%%, Speed: %.1fKiB/s \r", dlnow/dltotal*100, dlnow/1000/float64((time.Now().Unix()-started))) return true }) easy.Setopt(curl.OPT_PROGRESSDATA, ctx) @@ -131,6 +129,9 @@ func FetchFileCurl(ctx *ImportContext, uri *url.URL) (err error) { func FetchFileLocal(ctx *ImportContext, uri *url.URL) (err error) { rhl.Printf("Local fetcher called for '%s'", ctx.SourceUri) + if ctx.ProgressCallBack != nil { + ctx.ProgressCallBack(1, "fetching", 1.0, ctx.ProgressCallBackData) + } ctx.SourceFile = uri.Path ctx.DeleteSourceFile = false ctx.DeleteSourceDir = false diff --git a/importer.go b/importer.go index 0d0a836..68b71a6 100644 --- a/importer.go +++ b/importer.go @@ -41,21 +41,23 @@ var ( type ImportContext struct { *Config *RdDb - UserName string - Password string - Trusted bool - ShowId int - GroupName string - Cart int - Cut int - Channels int - NormalizationLevel int - AutotrimLevel int - UseMetaData bool - SourceUri string - SourceFile string - DeleteSourceFile bool - DeleteSourceDir bool + UserName string + Password string + Trusted bool + ShowId int + GroupName string + Cart int + Cut int + Channels int + NormalizationLevel int + AutotrimLevel int + UseMetaData bool + SourceUri string + SourceFile string + DeleteSourceFile bool + DeleteSourceDir bool + ProgressCallBack func(step int, step_name string, progress float64, userdata interface{}) + ProgressCallBackData interface{} } func NewImportContext(conf *Config, rddb *RdDb, user string, group string) *ImportContext { @@ -76,6 +78,7 @@ func NewImportContext(conf *Config, rddb *RdDb, user string, group string) *Impo ctx.SourceFile = "" ctx.DeleteSourceFile = false ctx.DeleteSourceDir = false + ctx.ProgressCallBack = nil return ctx } @@ -168,6 +171,8 @@ func import_audio(ctx *ImportContext) (err error) { if res, err = client.Do(req); err != nil { return } + defer res.Body.Close() + if res.StatusCode != http.StatusOK { // TODO: better error output err = fmt.Errorf("bad status: %s", res.Status) @@ -176,13 +181,13 @@ func import_audio(ctx *ImportContext) (err error) { } func cleanup_files(ctx *ImportContext) { - if(ctx.DeleteSourceFile) { + if ctx.DeleteSourceFile { rhdl.Printf("importer: removing file: %s", ctx.SourceFile) if err := os.Remove(ctx.SourceFile); err != nil { rhl.Printf("importer: error removing source file: %s", err) return } - if(ctx.DeleteSourceDir) { + if ctx.DeleteSourceDir { dir := path.Dir(ctx.SourceFile) rhdl.Printf("importer: also removing directory: %s", dir) if err := os.Remove(dir); err != nil { @@ -197,6 +202,10 @@ func ImportFile(ctx *ImportContext) (err error) { rhl.Println("ImportFile called for", ctx.SourceFile) defer cleanup_files(ctx) + if ctx.ProgressCallBack != nil { + ctx.ProgressCallBack(2, "importing", 0.0, ctx.ProgressCallBackData) + } + if ctx.Trusted { if err = ctx.getPassword(true); err != nil { return @@ -207,5 +216,10 @@ func ImportFile(ctx *ImportContext) (err error) { return } + if ctx.ProgressCallBack != nil { + ctx.ProgressCallBack(2, "importing", 1.0, ctx.ProgressCallBackData) + } + + rhl.Println("ImportFile succesfully imported", ctx.SourceFile) return } -- cgit v0.10.2