diff options
Diffstat (limited to 'src/helsinki.at')
-rw-r--r-- | src/helsinki.at/rhimport/fetcher.go | 11 | ||||
-rw-r--r-- | src/helsinki.at/rhimport/importer.go | 48 | ||||
-rw-r--r-- | src/helsinki.at/rhimportd/ctrlWebSimple.go | 5 |
3 files changed, 42 insertions, 22 deletions
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 } |