/
path.go
133 lines (119 loc) · 3.33 KB
/
path.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
124
125
126
127
128
129
130
131
132
133
package main
import (
"path/filepath"
"strings"
tmpl "text/template"
)
const (
Service = "<ks:service>"
Creator = "<ks:creator>"
Post = "<ks:post>"
Index = "<ks:index>"
Filename = "<ks:filename>"
Filehash = "<ks:filehash>"
Extension = "<ks:extension>"
)
const (
TmplDefault = "[" + Service + "]" + Creator + "/" + Post + "/" + Filename + Extension
TmplWithPrefixNumber = "[" + Service + "]" + Creator + "/" + Post + "/" + Index + "_" + Filename + Extension
TmplIndexNumber = "[" + Service + "]" + Creator + "/" + Post + "/" + Index + Extension
)
type PathConfig struct {
Service string
Creator string
Post string
Index int
Filename string
Filehash string
Extension string
}
func LoadPathTmpl(templateStr string, output string) (*tmpl.Template, error) {
templateStr = filepath.Join(output, templateStr)
templateStr = strings.ReplaceAll(templateStr, Service, "{{.Service}}")
templateStr = strings.ReplaceAll(templateStr, Creator, "{{.Creator}}")
templateStr = strings.ReplaceAll(templateStr, Post, "{{.Post}}")
templateStr = strings.ReplaceAll(templateStr, Index, "{{.Index}}")
templateStr = strings.ReplaceAll(templateStr, Filename, "{{.Filename}}")
templateStr = strings.ReplaceAll(templateStr, Filehash, "{{.Filehash}}")
templateStr = strings.ReplaceAll(templateStr, Extension, "{{.Extension}}")
tmpl, err := tmpl.New("path").Parse(templateStr)
if err != nil {
return nil, err
}
return tmpl, nil
}
func ExecutePathTmpl(tmpl *tmpl.Template, config *PathConfig) string {
var path strings.Builder
err := tmpl.Execute(&path, config)
if err != nil {
panic(err)
}
return path.String()
}
type TmplCache struct {
tmpl map[string]*tmpl.Template
}
func NewTmplCache() *TmplCache {
return &TmplCache{
tmpl: make(map[string]*tmpl.Template),
}
}
func (t *TmplCache) init() {
// default template
tmpl, err := LoadPathTmpl(template, output)
if err != nil {
panic(err)
}
t.tmpl["default"] = tmpl
if imageTemplate != "" {
tmpl, err := LoadPathTmpl(imageTemplate, output)
if err != nil {
panic(err)
}
t.tmpl["image"] = tmpl
}
if videoTemplate != "" {
tmpl, err := LoadPathTmpl(videoTemplate, output)
if err != nil {
panic(err)
}
t.tmpl["video"] = tmpl
}
if audioTemplate != "" {
tmpl, err := LoadPathTmpl(audioTemplate, output)
if err != nil {
panic(err)
}
t.tmpl["audio"] = tmpl
}
if archiveTemplate != "" {
tmpl, err := LoadPathTmpl(archiveTemplate, output)
if err != nil {
panic(err)
}
t.tmpl["archive"] = tmpl
}
}
func (t *TmplCache) GetTmpl(typ string) *tmpl.Template {
if tmpl, ok := t.tmpl[typ]; ok {
return tmpl
}
return t.tmpl["default"]
}
func (t *TmplCache) Execute(typ string, config *PathConfig) string {
return ExecutePathTmpl(t.GetTmpl(typ), config)
}
func getTyp(ext string) string {
switch ext {
case ".apng", ".avif", ".bmp", ".gif", ".ico", ".cur", ".jpg", ".jpeg", ".jfif", ".pjpeg", ".pjp", ".png", ".svg", ".tif", ".tiff", ".webp", ".jpe":
return "image"
case ".mp4", ".webm", ".mkv", ".avi", ".mov", ".wmv", ".flv", ".f4v", ".m4v", ".rmvb", ".rm", ".3gp", ".dat", ".ts", ".mts", ".vob":
return "video"
case ".mp3", ".wav", ".flac", ".ape", ".aac", ".ogg", ".wma", ".m4a", ".aiff", ".alac":
return "audio"
case ".zip", ".rar", ".7z", ".tar", ".gz", ".bz2", ".xz", ".zipmod":
return "archive"
default:
return "default"
}
}