diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-08 10:50:37 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-08 10:50:37 (GMT) |
commit | a6b29b342a73582a7f02877dda648d8d7a44ec57 (patch) | |
tree | 6d782f9e2e23040f79dadf9af7e5a54154573045 | |
parent | abff4760fe4aede34b60fbefc8f398ad2370304e (diff) |
import_file should be done
-rw-r--r-- | src/helsinki.at/rhimport/fetcher.go | 6 | ||||
-rw-r--r-- | src/helsinki.at/rhimport/importer.go | 134 | ||||
-rw-r--r-- | test/simple1.json | 2 |
3 files changed, 81 insertions, 61 deletions
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" } |