summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2015-12-21 21:19:27 (GMT)
committerChristian Pointner <equinox@helsinki.at>2015-12-21 21:19:27 (GMT)
commitfa4e8087ca710222069e0353bd591056f03f98b2 (patch)
treeb6098a8d34b4237a7dc4154ee4b4815f6d6e1ab9
parenta51ab35bc71f678fcc0bc07bd2d2fd682587381e (diff)
canceling using works now
-rw-r--r--fetcher.go41
-rw-r--r--importer.go11
2 files changed, 48 insertions, 4 deletions
diff --git a/fetcher.go b/fetcher.go
index bff62bc..24635d1 100644
--- a/fetcher.go
+++ b/fetcher.go
@@ -34,7 +34,9 @@ import (
"os"
"path"
"path/filepath"
+ "strconv"
"strings"
+ "time"
)
type FetchResult struct {
@@ -114,10 +116,11 @@ func FetchFileCurl(ctx *ImportContext, res *FetchResult, uri *url.URL) (err erro
easy.Setopt(curl.OPT_NOPROGRESS, false)
easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool {
if ctx.Cancel != nil && len(ctx.Cancel) > 0 {
+ res.ResponseCode = http.StatusNoContent
+ res.ErrorString = "canceled"
return false
}
- ctx := userdata.(*ImportContext)
if ctx.ProgressCallBack != nil {
ctx.ProgressCallBack(1, "downloading", dlnow/dltotal, ctx.ProgressCallBackData)
}
@@ -126,7 +129,11 @@ func FetchFileCurl(ctx *ImportContext, res *FetchResult, uri *url.URL) (err erro
easy.Setopt(curl.OPT_PROGRESSDATA, ctx)
if err = easy.Perform(); err != nil {
- err = fmt.Errorf("curl-fetcher('%s'): %s", ctx.SourceUri, err)
+ if res.ResponseCode == http.StatusNoContent {
+ err = nil
+ } else {
+ err = fmt.Errorf("curl-fetcher('%s'): %s", ctx.SourceUri, err)
+ }
return
}
@@ -159,6 +166,35 @@ func FetchFileLocal(ctx *ImportContext, res *FetchResult, uri *url.URL) (err err
return
}
+func FetchFileFake(ctx *ImportContext, res *FetchResult, uri *url.URL) error {
+ rhdl.Printf("Fake fetcher for '%s'", ctx.SourceUri)
+
+ if duration, err := strconv.ParseUint(uri.Host, 10, 32); err != nil {
+ err = nil
+ res.ResponseCode = http.StatusBadRequest
+ res.ErrorString = "invalid duration (must be a positive integer)"
+ } else {
+ for i := uint(0); i < uint(duration); i++ {
+ if ctx.Cancel != nil && len(ctx.Cancel) > 0 {
+ res.ResponseCode = http.StatusNoContent
+ res.ErrorString = "canceled"
+ return nil
+ }
+ if ctx.ProgressCallBack != nil {
+ ctx.ProgressCallBack(1, "faking", float64(i)/float64(duration), ctx.ProgressCallBackData)
+ }
+ time.Sleep(100 * time.Millisecond)
+ }
+ if ctx.ProgressCallBack != nil {
+ ctx.ProgressCallBack(42, "faking", 1.0, ctx.ProgressCallBackData)
+ }
+ ctx.SourceFile = "/nonexistend/fake.mp3"
+ ctx.DeleteSourceFile = false
+ ctx.DeleteSourceDir = false
+ }
+ return nil
+}
+
type FetchFunc func(*ImportContext, *FetchResult, *url.URL) (err error)
// TODO: implement fetchers for:
@@ -168,6 +204,7 @@ type FetchFunc func(*ImportContext, *FetchResult, *url.URL) (err error)
var (
fetchers = map[string]FetchFunc{
"local": FetchFileLocal,
+ "fake": FetchFileFake,
}
curl_protos = map[string]bool{
"http": false, "https": false,
diff --git a/importer.go b/importer.go
index df491d7..512daf6 100644
--- a/importer.go
+++ b/importer.go
@@ -502,10 +502,11 @@ func import_audio(ctx *ImportContext, res *ImportResult) (err error) {
easy.Setopt(curl.OPT_NOPROGRESS, false)
easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool {
if ctx.Cancel != nil && len(ctx.Cancel) > 0 {
+ res.ResponseCode = http.StatusNoContent
+ res.ErrorString = "canceled"
return false
}
- ctx := userdata.(*ImportContext)
if ctx.ProgressCallBack != nil {
ctx.ProgressCallBack(2, "importing", ulnow/ultotal, ctx.ProgressCallBackData)
}
@@ -514,7 +515,13 @@ func import_audio(ctx *ImportContext, res *ImportResult) (err error) {
easy.Setopt(curl.OPT_PROGRESSDATA, ctx)
if err = easy.Perform(); err != nil {
- err = fmt.Errorf("importer: %s", err)
+ if res.ResponseCode == http.StatusNoContent {
+ res.Cart = ctx.Cart
+ res.Cut = ctx.Cut
+ err = nil
+ } else {
+ err = fmt.Errorf("importer: %s", err)
+ }
return
}