mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-08 06:16:37 +08:00
refactor local metadata helpers
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func firstRemoteURL(baseDir string, values ...string) string {
|
||||
for _, value := range values {
|
||||
value = cleanXMLText(value)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
if isHTTPURL(value) {
|
||||
return value
|
||||
}
|
||||
if filepath.IsAbs(value) && fileExists(value) {
|
||||
return filepath.Clean(value)
|
||||
}
|
||||
if baseDir != "" {
|
||||
local := filepath.Join(baseDir, filepath.FromSlash(value))
|
||||
if fileExists(local) {
|
||||
return filepath.Clean(local)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func firstText(values ...string) string {
|
||||
for _, value := range values {
|
||||
if text := cleanXMLText(value); text != "" {
|
||||
return text
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func cleanXMLText(value string) string {
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
|
||||
func joinNFOValues(values []string) string {
|
||||
out := make([]string, 0, len(values))
|
||||
seen := map[string]struct{}{}
|
||||
for _, value := range values {
|
||||
for _, part := range strings.Split(value, ",") {
|
||||
part = cleanXMLText(part)
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
key := strings.ToLower(part)
|
||||
if _, ok := seen[key]; ok {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
out = append(out, part)
|
||||
}
|
||||
}
|
||||
return strings.Join(out, ",")
|
||||
}
|
||||
|
||||
func yearFromDate(value string) int {
|
||||
if len(value) < 4 {
|
||||
return 0
|
||||
}
|
||||
year, _ := strconv.Atoi(value[:4])
|
||||
return year
|
||||
}
|
||||
|
||||
func samePath(a, b string) bool {
|
||||
return strings.EqualFold(filepath.Clean(a), filepath.Clean(b))
|
||||
}
|
||||
Reference in New Issue
Block a user