From a6b29b342a73582a7f02877dda648d8d7a44ec57 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Tue, 8 Dec 2015 11:50:37 +0100
Subject: import_file should be done


diff --git a/src/helsinki.at/rhimport/fetcher.go b/src/helsinki.at/rhimport/fetcher.go
index 1a5801b..97fe7ff 100644
--- a/src/helsinki.at/rhimport/fetcher.go
+++ b/src/helsinki.at/rhimport/fetcher.go
@@ -24,11 +24,15 @@
 
 package rhimport
 
+import (
+	"path"
+)
+
 func FetchFile(ctx *ImportContext) (err error) {
 
 	// TODO: fetch file from ctx.SourceUri and put it into ctx.Config.TempDir
 
-	ctx.SourceFile = ctx.Config.TempDir + "/source-file.ogg"
+	ctx.SourceFile = ctx.Config.TempDir + "/" + path.Base(ctx.SourceUri)
 	ctx.DeleteSourceFile = true
 	return
 }
diff --git a/src/helsinki.at/rhimport/importer.go b/src/helsinki.at/rhimport/importer.go
index 2b512ed..0f8eea8 100644
--- a/src/helsinki.at/rhimport/importer.go
+++ b/src/helsinki.at/rhimport/importer.go
@@ -25,12 +25,17 @@
 package rhimport
 
 import (
-// "bytes"
-// "fmt"
-// "io"
-// "mime/multipart"
-// "net/http"
-// "os"
+	"bytes"
+	"fmt"
+	"io"
+	"mime/multipart"
+	"net/http"
+	"os"
+	"path"
+)
+
+var (
+	bool2str = map[bool]string{false: "0", true: "1"}
 )
 
 type ImportContext struct {
@@ -104,52 +109,69 @@ func (ctx *ImportContext) getShowInfo() (err error) {
 	return
 }
 
-// func import_audio(url, file string) (err error) {
-
-// 	var b bytes.Buffer
-// 	w := multipart.NewWriter(&b)
-
-// 	if err = w.WriteField("COMMAND", "2"); err != nil {
-// 		return
-// 	}
-// 	if err = w.WriteField("LOGIN_NAME", "<username>"); err != nil {
-// 		return
-// 	}
-// 	if err = w.WriteField("PASSWORD", "<password>"); err != nil {
-// 		return
-// 	}
-
-// 	f, err := os.Open(file)
-// 	if err != nil {
-// 		return
-// 	}
-// 	fw, err := w.CreateFormFile("FILENAME", file)
-// 	if err != nil {
-// 		return
-// 	}
-// 	if _, err = io.Copy(fw, f); err != nil {
-// 		return
-// 	}
-// 	f.Close()
-
-// 	w.Close()
-
-// 	req, err := http.NewRequest("POST", url, &b)
-// 	if err != nil {
-// 		return
-// 	}
-// 	req.Header.Set("Content-Type", w.FormDataContentType())
-
-// 	client := &http.Client{}
-// 	res, err := client.Do(req)
-// 	if err != nil {
-// 		return
-// 	}
-// 	if res.StatusCode != http.StatusOK {
-// 		err = fmt.Errorf("bad status: %s", res.Status)
-// 	}
-// 	return
-// }
+func import_audio(ctx *ImportContext) (err error) {
+	var b bytes.Buffer
+	w := multipart.NewWriter(&b)
+
+	if err = w.WriteField("COMMAND", "2"); err != nil {
+		return
+	}
+	if err = w.WriteField("LOGIN_NAME", ctx.UserName); err != nil {
+		return
+	}
+	if err = w.WriteField("PASSWORD", ctx.Password); err != nil {
+		return
+	}
+	if err = w.WriteField("CART_NUMBER", fmt.Sprintf("%d", ctx.Cart)); err != nil {
+		return
+	}
+	if err = w.WriteField("CUT_NUMBER", fmt.Sprintf("%d", ctx.Cut)); err != nil {
+		return
+	}
+	if err = w.WriteField("CHANNELS", fmt.Sprintf("%d", ctx.Channels)); err != nil {
+		return
+	}
+	if err = w.WriteField("NORMALIZATION_LEVEL", fmt.Sprintf("%d", ctx.NormalizationLevel)); err != nil {
+		return
+	}
+	if err = w.WriteField("AUTOTRIM_LEVEL", fmt.Sprintf("%d", ctx.AutotrimLevel)); err != nil {
+		return
+	}
+	if err = w.WriteField("USE_METADATA", bool2str[ctx.UseMetaData]); err != nil {
+		return
+	}
+
+	var f *os.File
+	var fw io.Writer
+	if f, err = os.Open(ctx.SourceFile); err != nil {
+		return
+	}
+	if fw, err = w.CreateFormFile("FILENAME", path.Base(ctx.SourceFile)); err != nil {
+		return
+	}
+	if _, err = io.Copy(fw, f); err != nil {
+		return
+	}
+	f.Close()
+	w.Close()
+
+	var req *http.Request
+	if req, err = http.NewRequest("POST", ctx.Config.RDXportEndpoint, &b); err != nil {
+		return
+	}
+	req.Header.Set("Content-Type", w.FormDataContentType())
+
+	client := &http.Client{}
+	var res *http.Response
+	if res, err = client.Do(req); err != nil {
+		return
+	}
+	if res.StatusCode != http.StatusOK {
+		// TODO: better error output
+		err = fmt.Errorf("bad status: %s", res.Status)
+	}
+	return
+}
 
 func ImportFile(ctx *ImportContext) (err error) {
 	rhl.Println("ImportFile called for", ctx.SourceFile)
@@ -160,11 +182,5 @@ func ImportFile(ctx *ImportContext) (err error) {
 		}
 	}
 
-	if err = ctx.getShowInfo(); err != nil {
-		return
-	}
-
-	rhdl.Printf("%+v", ctx)
-
-	return
+	return import_audio(ctx)
 }
diff --git a/test/simple1.json b/test/simple1.json
index 0efecec..0aeec7d 100644
--- a/test/simple1.json
+++ b/test/simple1.json
@@ -7,6 +7,6 @@
     "CHANNELS": 2,
     "NORMALIZATION_LEVEL": -1200,
     "AUTOTRIM_LEVEL": 0,
-    "USE_METADATA": false,
+    "USE_METADATA": true,
     "SOURCE_URI": "http://example.com/files/sendung.mp3"
 }
-- 
cgit v0.10.2