summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2015-12-10 16:30:40 (GMT)
committerChristian Pointner <equinox@helsinki.at>2015-12-10 16:30:40 (GMT)
commitfa921c13ae7d2fb82ab5801244b6b1e9ef7db371 (patch)
tree9e0abeebd534f179fab16042cd8f5e867bbcb66d /src
parentce68e63d07aa6efa926601298a472818ae6a37a6 (diff)
added support for progress callbacks
Diffstat (limited to 'src')
-rw-r--r--src/helsinki.at/rhimport/fetcher.go11
-rw-r--r--src/helsinki.at/rhimport/importer.go48
-rw-r--r--src/helsinki.at/rhimportd/ctrlWebSimple.go5
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
}