From 6e9e940372b1e76ecde2f6918c53592d5addfaf0 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 14 Dec 2015 05:02:37 +0100 Subject: added more sophisticated import options - still needs testing diff --git a/importer.go b/importer.go index 61c2b64..925722e 100644 --- a/importer.go +++ b/importer.go @@ -45,7 +45,7 @@ type ImportContext struct { UserName string Password string Trusted bool - ShowId int + ShowId uint ClearShowCarts bool GroupName string Cart uint @@ -144,7 +144,7 @@ func (ctx *ImportContext) getGroupOfCart() error { return nil } -func (ctx *ImportContext) getShowInfo() (err error) { +func (ctx *ImportContext) getShowInfo() (carts []uint, err error) { req := getShowInfoRequest{} req.showid = ctx.ShowId req.response = make(chan getShowInfoResult) @@ -155,12 +155,11 @@ func (ctx *ImportContext) getShowInfo() (err error) { err = res.err return } - - rhdl.Printf("Show %d:\n", ctx.ShowId) - rhdl.Printf(" Title: '%s'\n", res.title) - rhdl.Printf(" Normalization Level: %d\n", res.norm_lvl) - rhdl.Printf(" Autotrim Level: %d\n", res.trim_lvl) - rhdl.Printf(" Carts: %v\n", res.carts) + ctx.GroupName = res.group + ctx.NormalizationLevel = res.norm_lvl + ctx.AutotrimLevel = res.trim_lvl + ctx.UseMetaData = true + carts = res.carts return } @@ -189,6 +188,7 @@ func (ctx *ImportContext) getMusicInfo() (err error) { } ctx.NormalizationLevel = res.norm_lvl ctx.AutotrimLevel = res.trim_lvl + ctx.UseMetaData = true return } @@ -203,11 +203,11 @@ func (self *ImportResult) fromRDWebResult(rdres *RDWebResult) { self.ResponseCode = rdres.ResponseCode self.ErrorString = rdres.ErrorString if rdres.AudioConvertError != 0 { - self.ErrorString += fmt.Sprint(", Audio Convert Error: %d", rdres.AudioConvertError) + self.ErrorString += fmt.Sprintf(", Audio Convert Error: %d", rdres.AudioConvertError) } } -func add_cart(ctx *ImportContext, result *ImportResult) (err error) { +func add_cart(ctx *ImportContext, res *ImportResult) (err error) { var b bytes.Buffer w := multipart.NewWriter(&b) @@ -220,7 +220,7 @@ func add_cart(ctx *ImportContext, result *ImportResult) (err error) { if err = w.WriteField("PASSWORD", ctx.Password); err != nil { return } - if err = w.WriteField("GRPOUP_NAME", ctx.GroupName); err != nil { + if err = w.WriteField("GROUP_NAME", ctx.GroupName); err != nil { return } if err = w.WriteField("TYPE", "audio"); err != nil { @@ -233,32 +233,32 @@ func add_cart(ctx *ImportContext, result *ImportResult) (err error) { } w.Close() - var res *http.Response - if res, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + var resp *http.Response + if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } - defer res.Body.Close() + defer resp.Body.Close() - if res.StatusCode != http.StatusOK { + if resp.StatusCode != http.StatusOK { var rdres *RDWebResult - if rdres, err = NewRDWebResultFromXML(res.Body); err != nil { + if rdres, err = NewRDWebResultFromXML(resp.Body); err != nil { return } - result.fromRDWebResult(rdres) + res.fromRDWebResult(rdres) return } var cartadd *RDCartAdd - if cartadd, err = NewRDCartAddFromXML(res.Body); err != nil { + if cartadd, err = NewRDCartAddFromXML(resp.Body); err != nil { return } - result.ResponseCode = res.StatusCode - result.ErrorString = "OK" - result.Cart = cartadd.Carts[0].Number - ctx.Cart = result.Cart + res.ResponseCode = resp.StatusCode + res.ErrorString = "OK" + res.Cart = cartadd.Carts[0].Number + ctx.Cart = res.Cart return } -func add_cut(ctx *ImportContext, result *ImportResult) (err error) { +func add_cut(ctx *ImportContext, res *ImportResult) (err error) { var b bytes.Buffer w := multipart.NewWriter(&b) @@ -276,32 +276,32 @@ func add_cut(ctx *ImportContext, result *ImportResult) (err error) { } w.Close() - var res *http.Response - if res, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + var resp *http.Response + if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } - defer res.Body.Close() + defer resp.Body.Close() - if res.StatusCode != http.StatusOK { + if resp.StatusCode != http.StatusOK { var rdres *RDWebResult - if rdres, err = NewRDWebResultFromXML(res.Body); err != nil { + if rdres, err = NewRDWebResultFromXML(resp.Body); err != nil { return } - result.fromRDWebResult(rdres) + res.fromRDWebResult(rdres) return } var cutadd *RDCutAdd - if cutadd, err = NewRDCutAddFromXML(res.Body); err != nil { + if cutadd, err = NewRDCutAddFromXML(resp.Body); err != nil { return } - result.ResponseCode = res.StatusCode - result.ErrorString = "OK" - result.Cut = cutadd.Cuts[0].Number + res.ResponseCode = resp.StatusCode + res.ErrorString = "OK" + res.Cut = cutadd.Cuts[0].Number ctx.Cut = cutadd.Cuts[0].Number return } -func remove_cart(ctx *ImportContext, result *ImportResult) (err error) { +func remove_cart(ctx *ImportContext, res *ImportResult) (err error) { var b bytes.Buffer w := multipart.NewWriter(&b) @@ -319,17 +319,17 @@ func remove_cart(ctx *ImportContext, result *ImportResult) (err error) { } w.Close() - var res *http.Response - if res, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + var resp *http.Response + if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } - defer res.Body.Close() + defer resp.Body.Close() var rdres *RDWebResult - if rdres, err = NewRDWebResultFromXML(res.Body); err != nil { + if rdres, err = NewRDWebResultFromXML(resp.Body); err != nil { return } - result.fromRDWebResult(rdres) + res.fromRDWebResult(rdres) return } @@ -438,6 +438,61 @@ func import_audio(ctx *ImportContext, result *ImportResult) (err error) { return } +func remove_add_cart_cut(ctx *ImportContext, res *ImportResult) (err error) { + if err = remove_cart(ctx, res); err != nil || (res.ResponseCode != http.StatusOK && res.ResponseCode != http.StatusNotFound) { + return + } + if err = add_cart(ctx, res); err != nil || res.ResponseCode != http.StatusOK { + return + } + return add_cut(ctx, res) +} + +func is_cart_member_of_show(ctx *ImportContext, res *ImportResult, carts []uint) (found bool) { + if ctx.Cart == 0 { + return true + } + for _, cart := range carts { + if cart == ctx.Cart { + return true + } + } + res.ResponseCode = http.StatusBadRequest + res.ErrorString = fmt.Sprintf("Requested cart %d is not a member of show: %d", ctx.Cart, ctx.ShowId) + return false +} + +func clear_show_carts(ctx *ImportContext, res *ImportResult, carts []uint) (err error) { + if ctx.ClearShowCarts { + orig_cart := ctx.Cart + for _, cart := range carts { + ctx.Cart = cart + if err = remove_cart(ctx, res); err != nil || (res.ResponseCode != http.StatusOK && res.ResponseCode != http.StatusNotFound) { + return + } + } + ctx.Cart = orig_cart + } + return +} + +func add_show_cart_cut(ctx *ImportContext, res *ImportResult, carts []uint) (err error) { + if err = add_cart(ctx, res); err != nil || res.ResponseCode != http.StatusOK { + return + } + for _, cart := range carts { + if cart == ctx.Cart { + return add_cut(ctx, res) + } + } + if err = remove_cart(ctx, res); err != nil || res.ResponseCode != http.StatusOK { + return + } + res.ResponseCode = http.StatusForbidden + res.ErrorString = fmt.Sprintf("Show %d has no free carts left", ctx.ShowId) + return +} + func cleanup_files(ctx *ImportContext) { if ctx.DeleteSourceFile { rhdl.Printf("importer: removing file: %s", ctx.SourceFile) @@ -457,7 +512,6 @@ func cleanup_files(ctx *ImportContext) { } func ImportFile(ctx *ImportContext) (res *ImportResult, err error) { - rhl.Println("ImportFile called for", ctx.SourceFile) defer cleanup_files(ctx) if ctx.ProgressCallBack != nil { @@ -470,32 +524,35 @@ func ImportFile(ctx *ImportContext) (res *ImportResult, err error) { } } - res = &ImportResult{} + res = &ImportResult{ResponseCode: http.StatusOK} if ctx.ShowId != 0 { - res.ResponseCode = 500 - res.ErrorString = "Importing to shows using the show-id is not yet implemented" - // TODO: fetch info from dropboxes (cartlist, groupname, import-params) - // - if (ctx.Cart not in cartlist) -> Error - // - if (ClearShowCarts == true): foreach(cart in cartlist): remove_cart(cart) [200 || 404 -> OK] - // - add_cart(ctx, res) [200 -> OK] - // - add_cut(ctx, res) [200 -> OK] - return + var show_carts []uint + if show_carts, err = ctx.getShowInfo(); err != nil { + return + } + if !is_cart_member_of_show(ctx, res, show_carts) { + return + } + if err = clear_show_carts(ctx, res, show_carts); err != nil || (res.ResponseCode != http.StatusOK && res.ResponseCode != http.StatusNotFound) { + return + } + if err = add_show_cart_cut(ctx, res, show_carts); err != nil || res.ResponseCode != http.StatusOK { + return + } } else if ctx.GroupName != "" { - res.ResponseCode = 500 - res.ErrorString = "Importing to music pools is not yet implemented" - // TODO: fetch info from dropboxes (import-params) - // - add_cart(ctx, res) [200 -> OK] - // - add_cut(ctx, res) [200 -> OK] - return - } else if ctx.Cart != 0 && ctx.Cut == 0 { - res.ResponseCode = 500 - res.ErrorString = "Importing to a Cart which might not exist is not yet implemented" - // TODO: (everything except Cut, GroupName and ShowId must be in context) - // - ctx.getGroupOfCart(ctx.Cart) - // - remove_cart(ctx, res) [200 || 404 -> OK] - // - add_cart(ctx, res) [200 -> OK] - // - add_cut(ctx, res) [200 -> OK] - return + if err = ctx.getMusicInfo(); err != nil { + return + } + if err = remove_add_cart_cut(ctx, res); err != nil || res.ResponseCode != http.StatusOK { + return + } + } else if ctx.Cart != 0 && ctx.Cut == 0 { // TODO: we should add a DeleteCart option... + if err = ctx.getGroupOfCart(); err != nil { + return + } + if err = remove_add_cart_cut(ctx, res); err != nil || res.ResponseCode != http.StatusOK { + return + } } if ctx.Cart != 0 && ctx.Cut != 0 { @@ -510,11 +567,5 @@ func ImportFile(ctx *ImportContext) (res *ImportResult, err error) { } rhdl.Printf("ImportResult: %+v\n", res) - - if res.ResponseCode == http.StatusOK { - rhl.Println("ImportFile succesfully imported", ctx.SourceFile) - } else { - rhl.Println("ImportFile import of", ctx.SourceFile, "was unsuccesful") - } return } diff --git a/rddb.go b/rddb.go index 0a9e21e..b693c47 100644 --- a/rddb.go +++ b/rddb.go @@ -63,14 +63,15 @@ type getGroupOfCartRequest struct { type getShowInfoResult struct { title string - carts []int + group string + carts []uint norm_lvl int trim_lvl int err error } type getShowInfoRequest struct { - showid int + showid uint response chan getShowInfoResult } @@ -135,7 +136,7 @@ func (self *RdDb) init(conf *Config) (err error) { if self.getGroupOfCartStmt, err = self.dbh.Prepare("select NAME,DEFAULT_LOW_CART,DEFAULT_HIGH_CART from GROUPS where DEFAULT_LOW_CART <= ? and DEFAULT_HIGH_CART >= ?;"); err != nil { return } - if self.getShowInfoStmt, err = self.dbh.Prepare("select CART.TITLE,CART.MACROS,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART from CART, DROPBOXES, GROUPS where CART.NUMBER = DROPBOXES.TO_CART and GROUPS.NAME = DROPBOXES.GROUP_NAME and CART.NUMBER = ?;"); err != nil { + if self.getShowInfoStmt, err = self.dbh.Prepare("select CART.TITLE,CART.MACROS,DROPBOXES.GROUP_NAME,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART from CART, DROPBOXES, GROUPS where CART.NUMBER = DROPBOXES.TO_CART and GROUPS.NAME = DROPBOXES.GROUP_NAME and CART.NUMBER = ?;"); err != nil { return } if self.checkMusicGroupStmt, err = self.dbh.Prepare("select count(*) from DROPBOXES where GROUP_NAME = ? and SET_USER_DEFINED like \"M;%\";"); err != nil { @@ -199,7 +200,7 @@ func (self *RdDb) getLogTableName(log string) string { return strings.Replace(log, " ", "_", -1) + "_LOG" // TODO: this should get escaped for mySQL but golang doesn't support it!!! } -func (self *RdDb) getShowCarts(log string, low_cart, high_cart int) (carts []int, err error) { +func (self *RdDb) getShowCarts(log string, low_cart, high_cart int) (carts []uint, err error) { q := fmt.Sprintf("select CART_NUMBER from %s where CART_NUMBER >= %d and CART_NUMBER <= %d order by COUNT;", self.getLogTableName(log), low_cart, high_cart) var rows *sql.Rows if rows, err = self.dbh.Query(q); err != nil { @@ -207,7 +208,7 @@ func (self *RdDb) getShowCarts(log string, low_cart, high_cart int) (carts []int } defer rows.Close() for rows.Next() { - var cart int + var cart uint if err = rows.Scan(&cart); err != nil { return } @@ -217,10 +218,10 @@ func (self *RdDb) getShowCarts(log string, low_cart, high_cart int) (carts []int return } -func (self *RdDb) getShowInfo(showid int) (result getShowInfoResult) { +func (self *RdDb) getShowInfo(showid uint) (result getShowInfoResult) { var macros string var low_cart, high_cart int - result.err = self.getShowInfoStmt.QueryRow(showid).Scan(&result.title, ¯os, &result.norm_lvl, &result.trim_lvl, &low_cart, &high_cart) + result.err = self.getShowInfoStmt.QueryRow(showid).Scan(&result.title, ¯os, &result.group, &result.norm_lvl, &result.trim_lvl, &low_cart, &high_cart) if result.err != nil { if result.err == sql.ErrNoRows { result.err = fmt.Errorf("show '%d' not found", showid) -- cgit v0.10.2