summaryrefslogtreecommitdiff
path: root/core.go
blob: 3b8280a0d535aba623c3fc2cdccb990c64784bc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
//  rhimportd
//
//  The Radio Helsinki Rivendell Import Daemon
//
//
//  Copyright (C) 2015 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 (
	//	"io/ioutil"
	"fmt"
	"github.com/golang-basic/go-curl"
	"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(os.Stderr, "[rhimport-dbg]\t", log.LstdFlags)
	//rhdl = log.New(ioutil.Discard, "[rhimport-dbg]\t", log.LstdFlags)
)

func init() {
	curl.GlobalInit(curl.GLOBAL_ALL)
	fetcher_init()
}

type ImportProgressCB func(step int, step_name string, progress float64, userdata interface{}) bool

type ImportContext struct {
	conf                 *Config
	rddb                 *RdDbChan
	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     ImportProgressCB
	ProgressCallBackData interface{}
	Cancel               <-chan bool
}

func NewImportContext(conf *Config, rddb *RdDbChan, user string) *ImportContext {
	ctx := new(ImportContext)
	ctx.conf = conf
	ctx.rddb = rddb
	ctx.UserName = user
	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 *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 {
		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 *ImportContext) getPassword(cached bool) (err error) {
	ctx.Password, err = ctx.rddb.GetPassword(ctx.UserName, cached)
	return
}

func (ctx *ImportContext) getGroupOfCart() (err error) {
	ctx.GroupName, err = ctx.rddb.GetGroupOfCart(ctx.Cart)
	return
}

func (ctx *ImportContext) getShowInfo() (carts []uint, err error) {
	ctx.GroupName, ctx.NormalizationLevel, ctx.AutotrimLevel, carts, err = ctx.rddb.GetShowInfo(ctx.ShowId)
	ctx.Channels = 2
	ctx.UseMetaData = true
	return
}

func (ctx *ImportContext) checkMusicGroup() (bool, error) {
	return ctx.rddb.CheckMusicGroup(ctx.GroupName)
}

func (ctx *ImportContext) getMusicInfo() (err error) {
	ctx.NormalizationLevel, ctx.AutotrimLevel, err = ctx.rddb.GetMusicInfo(ctx.GroupName)
	ctx.Channels = 2
	ctx.UseMetaData = true
	ctx.Cart = 0
	ctx.Cut = 0
	return
}