summaryrefslogtreecommitdiff
path: root/importer.go
diff options
context:
space:
mode:
Diffstat (limited to 'importer.go')
-rw-r--r--importer.go191
1 files changed, 121 insertions, 70 deletions
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
}