From fa921c13ae7d2fb82ab5801244b6b1e9ef7db371 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/src/helsinki.at/rhimport/fetcher.go b/src/helsinki.at/rhimport/fetcher.go index b4e2f0c..03cf3da 100644 --- a/src/helsinki.at/rhimport/fetcher.go +++ b/src/helsinki.at/rhimport/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/src/helsinki.at/rhimport/importer.go b/src/helsinki.at/rhimport/importer.go index 0d0a836..68b71a6 100644 --- a/src/helsinki.at/rhimport/importer.go +++ b/src/helsinki.at/rhimport/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 } diff --git a/src/helsinki.at/rhimportd/ctrlWebSimple.go b/src/helsinki.at/rhimportd/ctrlWebSimple.go index 7b7c70d..1d28818 100644 --- a/src/helsinki.at/rhimportd/ctrlWebSimple.go +++ b/src/helsinki.at/rhimportd/ctrlWebSimple.go @@ -67,6 +67,10 @@ func webSimpleResponse(w http.ResponseWriter) { encoder.Encode(respdata) } +func webSimpleProgressCallback(step int, step_name string, progress float64, userdata interface{}) { + fmt.Printf("Step %d / %s: %3.2f%%\r", step, step_name, progress * 100) +} + func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RdDb, trusted bool, r *http.Request) (ctx *rhimport.ImportContext, err error) { decoder := json.NewDecoder(r.Body) @@ -90,6 +94,7 @@ func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RdDb, trusted b ctx.AutotrimLevel = reqdata.AutotrimLevel ctx.UseMetaData = reqdata.UseMetaData ctx.SourceUri = reqdata.SourceUri + ctx.ProgressCallBack = webSimpleProgressCallback return } diff --git a/test/simple1.json b/test/simple1.json index f952a70..6474061 100644 --- a/test/simple1.json +++ b/test/simple1.json @@ -2,7 +2,7 @@ "LOGIN_NAME": "heslinki", "PASSWORD": "123456", "GROUP_NAME": "test", - "CART_NUMBER": 10000, + "CART_NUMBER": 100000, "CUT_NUMBER": 1, "CHANNELS": 2, "NORMALIZATION_LEVEL": -1200, diff --git a/test/simple2.json b/test/simple2.json index 6f8735d..6a16ab6 100644 --- a/test/simple2.json +++ b/test/simple2.json @@ -2,7 +2,7 @@ "LOGIN_NAME": "heslinki", "PASSWORD": "123456", "GROUP_NAME": "test", - "CART_NUMBER": 10000, + "CART_NUMBER": 100000, "CUT_NUMBER": 1, "CHANNELS": 2, "NORMALIZATION_LEVEL": -1200, diff --git a/test/simple3.json b/test/simple3.json index 969b2b6..4cb4164 100644 --- a/test/simple3.json +++ b/test/simple3.json @@ -2,7 +2,7 @@ "LOGIN_NAME": "heslinki", "PASSWORD": "123456", "GROUP_NAME": "test", - "CART_NUMBER": 10000, + "CART_NUMBER": 100000, "CUT_NUMBER": 1, "CHANNELS": 2, "NORMALIZATION_LEVEL": -1200, diff --git a/test/test-simple.sh b/test/test-simple.sh index e797dd1..bc1ab60 100755 --- a/test/test-simple.sh +++ b/test/test-simple.sh @@ -5,6 +5,7 @@ if [ -z "$1" ]; then exit 1 fi -curl -XPOST 'http://localhost:4000/public/simple' -d @$1 +#curl -XPOST 'http://localhost:4000/public/simple' -d @$1 +curl -H "X-Forwarded-User: heslinki" -XPOST 'http://localhost:4000/trusted/simple' -d @$1 exit 0 -- cgit v0.10.2