diff options
-rw-r--r-- | fetcher.go | 6 | ||||
-rw-r--r-- | importer.go | 134 |
2 files changed, 80 insertions, 60 deletions
@@ -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/importer.go b/importer.go index 2b512ed..0f8eea8 100644 --- a/importer.go +++ b/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) } |