-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
126 lines (111 loc) · 2.69 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
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
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
type CaseInsensitiveReplacerParams struct {
toReplace *regexp.Regexp
replaceWith string
}
func AppendSubstitution(slice *[]Substitution, newItem Substitution) {
var appears int
for _, item := range *slice {
if item.To == newItem.To {
appears++
}
}
if appears > 0 {
newItem.Suffix = appears
}
*slice = append(*slice, newItem)
}
func ToPathWithSuffix(toPath string, suffix int) string {
if suffix == 0 {
return toPath
}
dir := filepath.Dir(toPath)
ext := filepath.Ext(toPath)
fileName := filepath.Base(toPath)
fileName = fileName[:len(fileName)-len(ext)]
newFileName := fmt.Sprintf("%s-%d%s", fileName, suffix, ext)
return filepath.Join(dir, newFileName)
}
func IsValidExtension(ext string) bool {
ext = strings.ToLower(ext)
switch ext {
case
".jpg",
".jpeg",
".png":
return true
}
return false
}
func AppendPath(slice []string, str string, isRecursive bool) []string {
var newSlice []string
keepParent := false
str = NormalizePath(str)
for _, s := range slice {
if (strings.HasPrefix(s, str+"/") && isRecursive) || str == s {
continue
}
if strings.HasPrefix(str, s+"/") {
keepParent = true
}
newSlice = append(newSlice, s)
}
if (!keepParent && isRecursive) || !isRecursive {
newSlice = append(newSlice, str)
}
return newSlice
}
func RemovePathsWithCommonParent(paths []string) []string {
var filteredPaths []string
seenChild := make(map[string]bool)
for _, path := range paths {
isChildPath := false
for _, otherPath := range paths {
if path != otherPath && strings.HasPrefix(path, otherPath) {
isChildPath = true
break
}
}
if !isChildPath {
filteredPaths = append(filteredPaths, path)
seenChild[path] = true
}
}
return filteredPaths
}
func CaseInsensitiveReplacer(toReplace, replaceWith string) *CaseInsensitiveReplacerParams {
return &CaseInsensitiveReplacerParams{
toReplace: regexp.MustCompile("(?i)" + toReplace),
replaceWith: replaceWith,
}
}
func (cir *CaseInsensitiveReplacerParams) Replace(str string) string {
return cir.toReplace.ReplaceAllString(str, cir.replaceWith)
}
func NormalizePath(path string) string {
return filepath.ToSlash(filepath.Clean(path))
}
func HasLocationPlaceholders(str string) bool {
re := regexp.MustCompile(`\{location_\w+}`)
match := re.FindAllString(str, -1)
return len(match) > 0
}
func UniqueFileName(filePath string) string {
dir := filepath.Dir(filePath)
ext := filepath.Ext(filePath)
base := strings.TrimSuffix(filepath.Base(filePath), ext)
for {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return filePath
}
base = base + " (duplicate)"
filePath = filepath.Join(dir, base+ext)
}
}