|
1
|
|
|
package scan |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"github.com/vvval/go-metadata-scanner/util/log" |
|
5
|
|
|
"github.com/vvval/go-metadata-scanner/vars" |
|
6
|
|
|
"path/filepath" |
|
7
|
|
|
"regexp" |
|
8
|
|
|
"runtime" |
|
9
|
|
|
"strings" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
func Candidates(filename string, files *vars.Chunk, extensions []string) (string, bool) { |
|
13
|
|
|
endings := extEndings(filename, extensions) |
|
14
|
|
|
var candidates = make(map[string]bool) |
|
15
|
|
|
|
|
16
|
|
|
for _, file := range *files { |
|
17
|
|
|
for _, ending := range endings { |
|
18
|
|
|
if strings.EqualFold(file, ending) { |
|
19
|
|
|
return file, true |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
if filesMatch(filepath.Base(ending), filepath.Base(file)) { |
|
23
|
|
|
candidates[file] = true |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
var values []string |
|
29
|
|
|
for k := range candidates { |
|
30
|
|
|
values = append(values, k) |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if len(values) == 1 { |
|
34
|
|
|
return values[0], true |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if len(values) == 0 { |
|
38
|
|
|
log.Debug("No candidates for " + filename) |
|
39
|
|
|
} else { |
|
40
|
|
|
log.Debug("Too many candidates for "+filename, values...) |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return "", false |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
func filesMatch(ending, file string) bool { |
|
47
|
|
|
if runtime.GOOS == "windows" { |
|
48
|
|
|
ending = strings.ToLower(ending) |
|
49
|
|
|
file = strings.ToLower(file) |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
var reg = ®exp.Regexp{} |
|
53
|
|
|
reg = regexp.MustCompile("^(([a-zA-Z]{1,}_)?0*)?" + regexp.QuoteMeta(ending) + "$") |
|
54
|
|
|
|
|
55
|
|
|
return reg.MatchString(file) |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
func extEndings(filename string, extensions []string) []string { |
|
59
|
|
|
ends := []string{filename} |
|
60
|
|
|
for _, extension := range extensions { |
|
61
|
|
|
ends = append(ends, filename+"."+extension) |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return ends |
|
65
|
|
|
} |
|
66
|
|
|
|