From cccba3ce2e7a5797c84c78bce7e8d24fdcb8fce7 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Sun, 13 Dec 2015 01:36:41 +0100
Subject: GroupName should now be only supplied for music pools added context
 SanityCheck before fetching files


diff --git a/src/helsinki.at/rhimport/fetcher.go b/src/helsinki.at/rhimport/fetcher.go
index a86f7ca..7d406b2 100644
--- a/src/helsinki.at/rhimport/fetcher.go
+++ b/src/helsinki.at/rhimport/fetcher.go
@@ -146,7 +146,6 @@ func FetchFileLocal(ctx *ImportContext, uri *url.URL) (err error) {
 
 type FetchFunc func(*ImportContext, *url.URL) (err error)
 
-
 // TODO: implement fetchers for:
 //         archiv://
 //         public://
diff --git a/src/helsinki.at/rhimport/importer.go b/src/helsinki.at/rhimport/importer.go
index f365ea5..0fc5ecf 100644
--- a/src/helsinki.at/rhimport/importer.go
+++ b/src/helsinki.at/rhimport/importer.go
@@ -62,7 +62,7 @@ type ImportContext struct {
 	ProgressCallBackData interface{}
 }
 
-func NewImportContext(conf *Config, rddb *RdDb, user string, group string) *ImportContext {
+func NewImportContext(conf *Config, rddb *RdDb, user string) *ImportContext {
 	ctx := new(ImportContext)
 	ctx.Config = conf
 	ctx.RdDb = rddb
@@ -71,11 +71,11 @@ func NewImportContext(conf *Config, rddb *RdDb, user string, group string) *Impo
 	ctx.Trusted = false
 	ctx.ShowId = 0
 	ctx.ClearShowCarts = false
-	ctx.GroupName = group
+	ctx.GroupName = ""
 	ctx.Cart = 0
 	ctx.Cut = 0
 	ctx.Channels = 2
-	ctx.NormalizationLevel = -1200
+	ctx.NormalizationLevel = -12
 	ctx.AutotrimLevel = 0
 	ctx.UseMetaData = false
 	ctx.SourceFile = ""
@@ -86,6 +86,29 @@ func NewImportContext(conf *Config, rddb *RdDb, user string, group string) *Impo
 	return ctx
 }
 
+func (ctx *ImportContext) SanityCheck() error {
+	if ctx.UserName == "" {
+		return fmt.Errorf("empty Username is not allowed")
+	}
+	if ctx.Password == "" && !ctx.Trusted {
+		return fmt.Errorf("empty Password on untrusted control interface is not allowed")
+	}
+	if ctx.ShowId != 0 {
+		return nil
+	}
+	if ctx.GroupName != "" {
+		// TODO: check if GroupName is a music pool -> Error if not
+		return nil
+	}
+	if ctx.Cart == 0 {
+		return fmt.Errorf("either ShowId, PoolName or CartNumber must be supplied")
+	}
+	if ctx.Channels != 1 && ctx.Channels != 2 {
+		return fmt.Errorf("channles must be 1 or 2")
+	}
+	return nil
+}
+
 func (ctx *ImportContext) getPassword(cached bool) (err error) {
 	req := getPasswordRequest{}
 	req.username = ctx.UserName
@@ -409,7 +432,7 @@ func ImportFile(ctx *ImportContext) (res *ImportResult, err error) {
 		//     - add_cut(ctx, res) [200 -> OK]
 		//     - import_audio(ctx, res) [200 -> OK]
 		return
-	} else if ctx.GroupName != "" && ctx.Cart == 0 {
+	} else if ctx.GroupName != "" {
 		res.ResponseCode = 500
 		res.ErrorString = "Importing to music pools using the group name is not yet implemented"
 		// TODO: fetch info from dropboxes (import-params)
@@ -419,7 +442,8 @@ func ImportFile(ctx *ImportContext) (res *ImportResult, err error) {
 	} 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 and ShowId must be in context)
+		// 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]
diff --git a/src/helsinki.at/rhimportd/ctrlWebSimple.go b/src/helsinki.at/rhimportd/ctrlWebSimple.go
index a57e8c5..5e12df5 100644
--- a/src/helsinki.at/rhimportd/ctrlWebSimple.go
+++ b/src/helsinki.at/rhimportd/ctrlWebSimple.go
@@ -36,7 +36,6 @@ import (
 type webSimpleRequestData struct {
 	UserName           string `json:"LOGIN_NAME"`
 	Password           string `json:"PASSWORD"`
-	GroupName          string `json:"GROUP_NAME"`
 	Cart               uint   `json:"CART_NUMBER"`
 	Cut                uint   `json:"CUT_NUMBER"`
 	Channels           uint   `json:"CHANNELS"`
@@ -84,7 +83,7 @@ func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RdDb, trusted b
 	if trusted {
 		username = r.Header.Get("X-Forwarded-User")
 	}
-	ctx = rhimport.NewImportContext(conf, rddb, username, reqdata.GroupName)
+	ctx = rhimport.NewImportContext(conf, rddb, username)
 	ctx.Password = reqdata.Password
 	ctx.Trusted = trusted
 	ctx.Cart = reqdata.Cart
@@ -108,9 +107,10 @@ func webSimpleHandler(conf *rhimport.Config, rddb *rhimport.RdDb, trusted bool,
 		return
 	}
 
-	// TODO: add a sanity check for the context:
-	//          it would be nice to know whether a request is bogus
-	//          *before* we fetch a big file!
+	if err = ctx.SanityCheck(); err != nil {
+		webSimpleErrorResponse(w, http.StatusBadRequest, err.Error())
+		return
+	}
 
 	if err = rhimport.FetchFile(ctx); err != nil {
 		webSimpleErrorResponse(w, http.StatusInternalServerError, err.Error())
diff --git a/test/simple1.json b/test/simple1.json
index 6474061..db3f2aa 100644
--- a/test/simple1.json
+++ b/test/simple1.json
@@ -1,7 +1,6 @@
 {
     "LOGIN_NAME": "heslinki",
     "PASSWORD": "123456",
-    "GROUP_NAME": "test",
     "CART_NUMBER": 100000,
     "CUT_NUMBER": 1,
     "CHANNELS": 2,
diff --git a/test/simple2.json b/test/simple2.json
index 6a16ab6..e97379e 100644
--- a/test/simple2.json
+++ b/test/simple2.json
@@ -1,7 +1,6 @@
 {
     "LOGIN_NAME": "heslinki",
     "PASSWORD": "123456",
-    "GROUP_NAME": "test",
     "CART_NUMBER": 100000,
     "CUT_NUMBER": 1,
     "CHANNELS": 2,
diff --git a/test/simple3.json b/test/simple3.json
index 4cb4164..eaebab9 100644
--- a/test/simple3.json
+++ b/test/simple3.json
@@ -1,7 +1,6 @@
 {
     "LOGIN_NAME": "heslinki",
     "PASSWORD": "123456",
-    "GROUP_NAME": "test",
     "CART_NUMBER": 100000,
     "CUT_NUMBER": 1,
     "CHANNELS": 2,
diff --git a/test/simple4.json b/test/simple4.json
index 472c8db..abd8b30 100644
--- a/test/simple4.json
+++ b/test/simple4.json
@@ -1,7 +1,6 @@
 {
     "LOGIN_NAME": "heslinki",
     "PASSWORD": "123456",
-    "GROUP_NAME": "test",
     "CART_NUMBER": 100000,
     "CUT_NUMBER": 1,
     "CHANNELS": 2,
diff --git a/test/simple5.json b/test/simple5.json
index ad0ff98..d74e2c4 100644
--- a/test/simple5.json
+++ b/test/simple5.json
@@ -1,7 +1,6 @@
 {
     "LOGIN_NAME": "heslinki",
     "PASSWORD": "123456",
-    "GROUP_NAME": "test",
     "CART_NUMBER": 100000,
     "CUT_NUMBER": 1,
     "CHANNELS": 2,
-- 
cgit v0.10.2