-
Notifications
You must be signed in to change notification settings - Fork 1
/
directories.go
123 lines (105 loc) · 2.77 KB
/
directories.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
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
package vangogh_local_data
import (
"fmt"
"github.com/boggydigital/pathways"
"path/filepath"
"strings"
)
const DefaultVangoghRootDir = "/var/lib/vangogh"
const (
Backups pathways.AbsDir = "backups"
Metadata pathways.AbsDir = "metadata"
Input pathways.AbsDir = "input"
Output pathways.AbsDir = "output"
Images pathways.AbsDir = "images"
Items pathways.AbsDir = "items"
Downloads pathways.AbsDir = "downloads"
Checksums pathways.AbsDir = "checksums"
RecycleBin pathways.AbsDir = "recycle_bin"
Logs pathways.AbsDir = "logs"
)
var AllAbsDirs = []pathways.AbsDir{
Backups,
Metadata,
Input,
Output,
Images,
Items,
Downloads,
Checksums,
RecycleBin,
Logs,
}
const (
Redux pathways.RelDir = "_redux"
DLCs pathways.RelDir = "dlc"
Extras pathways.RelDir = "extras"
)
var RelToAbsDirs = map[pathways.RelDir]pathways.AbsDir{
Redux: Metadata,
DLCs: Downloads,
Extras: Downloads,
}
func AbsImagesDirByImageId(imageId string) (string, error) {
if imageId == "" {
return "", fmt.Errorf("imageId cannot be empty")
}
imageId = strings.TrimPrefix(imageId, "/")
if len(imageId) < 2 {
return "", fmt.Errorf("imageId is too short")
}
idp, err := pathways.GetAbsDir(Images)
return filepath.Join(idp, imageId[0:2]), err
}
func AbsItemPath(path string) (string, error) {
if path == "" {
return "", fmt.Errorf("item path cannot be empty")
}
//GOG.com quirk - some item URLs path has multiple slashes
//e.g. https://items.gog.com//atom_rpg_trudograd/mp4/TGWMap_Night_%281%29.gif.mp4
//so we need to keep trimming while there is something to trim
for strings.HasPrefix(path, "/") {
path = strings.TrimPrefix(path, "/")
}
if len(path) < 1 {
return "", fmt.Errorf("sanitized item path cannot be empty")
}
idp, err := pathways.GetAbsDir(Items)
if err != nil {
return "", err
}
return filepath.Join(idp, path[0:1], path), nil
}
func AbsLocalProductTypeDir(pt ProductType) (string, error) {
if !IsValidProductType(pt) {
return "", fmt.Errorf("no local destination for product type %s", pt)
}
amd, err := pathways.GetAbsDir(Metadata)
if err != nil {
return "", err
}
return filepath.Join(amd, pt.String()), nil
}
func RelProductDownloadsDir(slug string) (string, error) {
if slug == "" {
return "", fmt.Errorf("vangogh_urls: empty slug")
}
if len(slug) < 1 {
return "", fmt.Errorf("vangogh_urls: slug is too short")
}
return filepath.Join(strings.ToLower(slug[0:1]), slug), nil
}
func AbsProductDownloadsDir(slug string) (string, error) {
rDir, err := RelProductDownloadsDir(slug)
if err != nil {
return rDir, err
}
return AbsDownloadDirFromRel(rDir)
}
func AbsDownloadDirFromRel(p string) (string, error) {
adp, err := pathways.GetAbsDir(Downloads)
if err != nil {
return "", err
}
return filepath.Join(adp, p), nil
}