-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.go
64 lines (59 loc) · 1.59 KB
/
helpers.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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
// Sanitises to make a filesystem-safe name.
func sanitiseForFilesystem(s string) string {
s = strings.Replace(s, "/", "-", -1) // For linux.
s = strings.Replace(s, "<", "-", -1) // For windows or FAT disks on linux.
s = strings.Replace(s, ">", "-", -1)
s = strings.Replace(s, ":", "-", -1)
s = strings.Replace(s, "\"", "-", -1)
s = strings.Replace(s, "\\", "-", -1)
s = strings.Replace(s, "|", "-", -1)
s = strings.Replace(s, "?", "-", -1)
s = strings.Replace(s, "*", "-", -1)
return s
}
func getMovieImageIfNeeded(image string, size string, folder string, filename string) {
if image == "" {
return
}
path := filepath.Join(folder, filename)
if _, statErr := os.Stat(path); os.IsNotExist(statErr) {
image, imageErr := tmdbDownloadImage(image, size)
if imageErr == nil {
ioutil.WriteFile(path, image, os.ModePerm)
} else {
log.Println("Couldn't download image:", imageErr)
}
}
}
func getTVImageIfNeeded(image string, folder string, filename string) {
if image == "" {
return
}
path := filepath.Join(folder, filename)
if _, statErr := os.Stat(path); os.IsNotExist(statErr) {
image, imageErr := vanillaDownload(image)
if imageErr == nil || len(image) < 10000 {
ioutil.WriteFile(path, image, os.ModePerm)
} else {
log.Println("Couldn't download image:", imageErr)
}
}
}
// Downloads contents of a url.
func vanillaDownload(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}