/
utils.go
70 lines (57 loc) · 1.32 KB
/
utils.go
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
package nzbget
import (
"encoding/base64"
"encoding/xml"
"io"
"io/ioutil"
"net/http"
nzb "github.com/andrewstuart/go-nzb"
"github.com/pkg/errors"
)
//Source: https://github.com/dashotv/flame
func readFile(path string) (string, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return "", errors.Wrap(err, "could not read file")
}
return string(b), nil
}
func base64encode(s string) string {
data := []byte(s)
str := base64.StdEncoding.EncodeToString(data)
return str
}
func base64decode(s string) string {
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return ""
}
return string(b)
}
func downloadURL(URL string) (string, error) {
// Get the data
resp, err := http.Get(URL)
if err != nil {
return "", errors.Wrap(err, "could not http get url")
}
defer resp.Body.Close()
file, err := ioutil.TempFile("./temp", "flame-download-*")
if err != nil {
return "", errors.Wrap(err, "could not get tmp file")
}
defer file.Close()
// Write the body to file
_, err = io.Copy(file, resp.Body)
if err != nil {
return "", errors.Wrap(err, "could not copy file")
}
return file.Name(), nil
}
func nzbName(data string) (string, error) {
n := nzb.NZB{}
err := xml.Unmarshal([]byte(data), &n)
if err != nil {
return "", errors.Wrap(err, "could not unmarshal")
}
return n.Meta["name"], nil
}