From 3012f89fff0ee185c11dca56d8172c04c4a1987f Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Tue, 8 Dec 2015 12:25:52 +0100
Subject: fetcher parses SourceUri and selects right fetcher function


diff --git a/src/helsinki.at/rhimport/fetcher.go b/src/helsinki.at/rhimport/fetcher.go
index 97fe7ff..26077b8 100644
--- a/src/helsinki.at/rhimport/fetcher.go
+++ b/src/helsinki.at/rhimport/fetcher.go
@@ -25,14 +25,46 @@
 package rhimport
 
 import (
+	"fmt"
+	"net/url"
 	"path"
 )
 
+func FetchFileHttp(ctx *ImportContext, uri *url.URL) (err error) {
+	rhl.Printf("HTTP/HTTPS fetcher called for '%s'", ctx.SourceUri)
+	ctx.SourceFile = ctx.Config.TempDir + "/" + path.Base(uri.Path)
+	ctx.DeleteSourceFile = true
+	return
+}
+
+func FetchFileLocal(ctx *ImportContext, uri *url.URL) (err error) {
+	rhl.Printf("Local fetcher called for '%s'", ctx.SourceUri)
+	ctx.SourceFile = uri.Path
+	ctx.DeleteSourceFile = false
+	return
+}
+
+type FetchFunc func(*ImportContext, *url.URL) (err error)
+
+var (
+	fetchers = map[string]FetchFunc{
+		"http":  FetchFileHttp,
+		"https": FetchFileHttp,
+		"local": FetchFileLocal,
+	}
+)
+
 func FetchFile(ctx *ImportContext) (err error) {
 
-	// TODO: fetch file from ctx.SourceUri and put it into ctx.Config.TempDir
+	var uri *url.URL
+	if uri, err = url.Parse(ctx.SourceUri); err != nil {
+		return
+	}
 
-	ctx.SourceFile = ctx.Config.TempDir + "/" + path.Base(ctx.SourceUri)
-	ctx.DeleteSourceFile = true
+	if fetcher, ok := fetchers[uri.Scheme]; ok {
+		err = fetcher(ctx, uri)
+	} else {
+		err = fmt.Errorf("No fetcher for uri scheme '%s' found.", uri.Scheme)
+	}
 	return
 }
diff --git a/test/simple1.json b/test/simple1.json
index 0aeec7d..3083de7 100644
--- a/test/simple1.json
+++ b/test/simple1.json
@@ -8,5 +8,5 @@
     "NORMALIZATION_LEVEL": -1200,
     "AUTOTRIM_LEVEL": 0,
     "USE_METADATA": true,
-    "SOURCE_URI": "http://example.com/files/sendung.mp3"
+    "SOURCE_URI": "https://example.com/files/sendung.mp3"
 }
diff --git a/test/simple2.json b/test/simple2.json
new file mode 100644
index 0000000..6f8735d
--- /dev/null
+++ b/test/simple2.json
@@ -0,0 +1,12 @@
+{
+    "LOGIN_NAME": "heslinki",
+    "PASSWORD": "123456",
+    "GROUP_NAME": "test",
+    "CART_NUMBER": 10000,
+    "CUT_NUMBER": 1,
+    "CHANNELS": 2,
+    "NORMALIZATION_LEVEL": -1200,
+    "AUTOTRIM_LEVEL": 0,
+    "USE_METADATA": true,
+    "SOURCE_URI": "local:///path/to/sendung.mp3"
+}
-- 
cgit v0.10.2