summaryrefslogtreecommitdiff
path: root/rhimport/core.go
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2016-01-08 01:06:30 (GMT)
committerChristian Pointner <equinox@helsinki.at>2016-01-08 01:06:30 (GMT)
commite88ef9d360843541bd348b35099dda1b15c6c896 (patch)
treed53b90301fbf535d076a56b10f44851c0572ec82 /rhimport/core.go
parent9cd0b1783c0c90c68c4840b5d317e9135e07774e (diff)
prepare export rhimport package to rhrd-go repo
Diffstat (limited to 'rhimport/core.go')
-rw-r--r--rhimport/core.go192
1 files changed, 192 insertions, 0 deletions
diff --git a/rhimport/core.go b/rhimport/core.go
new file mode 100644
index 0000000..98acd35
--- /dev/null
+++ b/rhimport/core.go
@@ -0,0 +1,192 @@
+//
+// rhimportd
+//
+// The Radio Helsinki Rivendell Import Daemon
+//
+//
+// Copyright (C) 2015-2016 Christian Pointner <equinox@helsinki.at>
+//
+// This file is part of rhimportd.
+//
+// rhimportd is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// any later version.
+//
+// rhimportd is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with rhimportd. If not, see <http://www.gnu.org/licenses/>.
+//
+
+package rhimport
+
+import (
+ "fmt"
+ "github.com/andelf/go-curl"
+ "helsinki.at/rhrd-go/rddb"
+ "io/ioutil"
+ "log"
+ "os"
+)
+
+const (
+ CART_MAX = 999999
+ CUT_MAX = 999
+)
+
+var (
+ bool2str = map[bool]string{false: "0", true: "1"}
+ rhl = log.New(os.Stderr, "[rhimport]\t", log.LstdFlags)
+ rhdl = log.New(ioutil.Discard, "[rhimport-dbg]\t", log.LstdFlags)
+)
+
+func init() {
+ if _, exists := os.LookupEnv("RHIMPORT_DEBUG"); exists {
+ rhdl.SetOutput(os.Stderr)
+ }
+ curl.GlobalInit(curl.GLOBAL_ALL)
+ fetcherInit()
+}
+
+type ProgressCB func(step int, stepName string, progress float64, userdata interface{}) bool
+type DoneCB func(Result, interface{}) bool
+
+type Result struct {
+ ResponseCode int
+ ErrorString string
+ Cart uint
+ Cut uint
+}
+
+type Context struct {
+ conf *Config
+ db *rddb.DBChan
+ UserName string
+ Password string
+ Trusted bool
+ ShowId uint
+ ClearShowCarts bool
+ GroupName string
+ Cart uint
+ ClearCart bool
+ Cut uint
+ Channels uint
+ NormalizationLevel int
+ AutotrimLevel int
+ UseMetaData bool
+ SourceUri string
+ SourceFile string
+ DeleteSourceFile bool
+ DeleteSourceDir bool
+ ProgressCallBack ProgressCB
+ ProgressCallBackData interface{}
+ Cancel <-chan bool
+}
+
+func NewContext(conf *Config, db *rddb.DBChan) *Context {
+ ctx := new(Context)
+ ctx.conf = conf
+ ctx.db = db
+ ctx.UserName = ""
+ ctx.Password = ""
+ ctx.Trusted = false
+ ctx.ShowId = 0
+ ctx.ClearShowCarts = false
+ ctx.GroupName = ""
+ ctx.Cart = 0
+ ctx.ClearCart = false
+ ctx.Cut = 0
+ ctx.Channels = conf.ImportParamDefaults.Channels
+ ctx.NormalizationLevel = conf.ImportParamDefaults.NormalizationLevel
+ ctx.AutotrimLevel = conf.ImportParamDefaults.AutotrimLevel
+ ctx.UseMetaData = conf.ImportParamDefaults.UseMetaData
+ ctx.SourceFile = ""
+ ctx.DeleteSourceFile = false
+ ctx.DeleteSourceDir = false
+ ctx.ProgressCallBack = nil
+ ctx.Cancel = nil
+
+ return ctx
+}
+
+func (ctx *Context) 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 {
+ if ctx.ShowId != 0 && ctx.ShowId > CART_MAX {
+ return fmt.Errorf("ShowId %d is outside of allowed range (0 < show-id < %d)", ctx.ShowId, CART_MAX)
+ }
+ if ctx.Cart != 0 && ctx.Cart > CART_MAX {
+ return fmt.Errorf("Cart %d is outside of allowed range (0 < cart < %d)", ctx.Cart, CART_MAX)
+ }
+ return nil
+ }
+ if ctx.GroupName != "" {
+ ismusic, err := ctx.checkMusicGroup()
+ if err != nil {
+ return err
+ }
+ if !ismusic {
+ return fmt.Errorf("supplied GroupName '%s' is not a music pool", ctx.GroupName)
+ }
+ if ctx.Cart != 0 || ctx.Cut != 0 {
+ return fmt.Errorf("Cart and Cut must not be supplied when importing into a music group")
+ }
+ return nil
+ }
+ if ctx.Cart == 0 {
+ return fmt.Errorf("either ShowId, PoolName or CartNumber must be supplied")
+ }
+ if ctx.Cart > CART_MAX {
+ return fmt.Errorf("Cart %d is outside of allowed range (0 < cart < %d)", ctx.Cart, CART_MAX)
+ }
+ if ctx.Cut != 0 && ctx.Cut > CUT_MAX {
+ return fmt.Errorf("Cut %d is outside of allowed range (0 < cart < %d)", ctx.Cut, CUT_MAX)
+ }
+ if ctx.Channels != 1 && ctx.Channels != 2 {
+ return fmt.Errorf("channles must be 1 or 2")
+ }
+ return nil
+}
+
+func (ctx *Context) getPassword(cached bool) (err error) {
+ ctx.Password, err = ctx.db.GetPassword(ctx.UserName, cached)
+ return
+}
+
+func (ctx *Context) CheckPassword() (bool, error) {
+ return ctx.db.CheckPassword(ctx.UserName, ctx.Password)
+}
+
+func (ctx *Context) getGroupOfCart() (err error) {
+ ctx.GroupName, err = ctx.db.GetGroupOfCart(ctx.Cart)
+ return
+}
+
+func (ctx *Context) getShowInfo() (carts []uint, err error) {
+ ctx.GroupName, ctx.NormalizationLevel, ctx.AutotrimLevel, carts, err = ctx.db.GetShowInfo(ctx.ShowId)
+ ctx.Channels = 2
+ ctx.UseMetaData = true
+ return
+}
+
+func (ctx *Context) checkMusicGroup() (bool, error) {
+ return ctx.db.CheckMusicGroup(ctx.GroupName)
+}
+
+func (ctx *Context) getMusicInfo() (err error) {
+ ctx.NormalizationLevel, ctx.AutotrimLevel, err = ctx.db.GetMusicInfo(ctx.GroupName)
+ ctx.Channels = 2
+ ctx.UseMetaData = true
+ ctx.Cart = 0
+ ctx.Cut = 0
+ return
+}