2020-05-27 13:40:21 +02:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2020-10-19 11:50:54 +02:00
|
|
|
"encoding/json"
|
2020-07-11 16:46:29 +02:00
|
|
|
"regexp"
|
2020-05-27 13:40:21 +02:00
|
|
|
"strings"
|
|
|
|
|
2020-06-29 11:10:24 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-07-11 16:46:29 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2020-06-29 11:10:24 +02:00
|
|
|
)
|
2020-06-27 14:15:25 +02:00
|
|
|
|
|
|
|
var UnwantedDescriptions = map[string]bool{
|
2020-07-11 10:19:06 +02:00
|
|
|
"OLYMPUS DIGITAL CAMERA": true, // Olympus
|
2020-07-16 13:02:48 +02:00
|
|
|
"SAMSUNG": true, // Samsung
|
2020-07-11 10:19:06 +02:00
|
|
|
"rhdr": true, // Huawei
|
|
|
|
"hdrpl": true,
|
|
|
|
"fbt": true,
|
|
|
|
"mon": true,
|
|
|
|
"nor": true,
|
|
|
|
"dav": true,
|
|
|
|
"mde": true,
|
2020-07-15 09:18:58 +02:00
|
|
|
"mde_soft": true,
|
2020-07-11 10:19:06 +02:00
|
|
|
"edf": true,
|
|
|
|
"btfmdn": true,
|
|
|
|
"btf": true,
|
|
|
|
"btfhdr": true,
|
|
|
|
"frem": true,
|
|
|
|
"oznor": true,
|
|
|
|
"rpt": true,
|
2020-07-13 14:20:43 +02:00
|
|
|
"burst": true,
|
2020-07-15 09:18:58 +02:00
|
|
|
"sdr_HDRB": true,
|
2020-08-12 19:14:01 +02:00
|
|
|
"cof": true,
|
2020-09-21 03:54:40 +02:00
|
|
|
"qrf": true,
|
2020-06-27 14:15:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
var LowerCaseRegexp = regexp.MustCompile("[a-z0-9_\\-]+")
|
|
|
|
|
2020-05-27 13:40:21 +02:00
|
|
|
// SanitizeString removes unwanted character from an exif value string.
|
2020-07-13 14:20:43 +02:00
|
|
|
func SanitizeString(s string) string {
|
|
|
|
if s == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
|
|
|
return strings.Replace(s, "\"", "", -1)
|
2020-05-27 13:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SanitizeUID normalizes unique IDs found in XMP or Exif metadata.
|
|
|
|
func SanitizeUID(value string) string {
|
|
|
|
value = SanitizeString(value)
|
|
|
|
|
|
|
|
if start := strings.LastIndex(value, ":"); start != -1 {
|
|
|
|
value = value[start+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a unique ID?
|
|
|
|
if len(value) < 15 || len(value) > 36 {
|
|
|
|
value = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToLower(value)
|
|
|
|
}
|
2020-06-27 14:15:25 +02:00
|
|
|
|
|
|
|
// SanitizeTitle normalizes titles and removes unwanted information.
|
2020-07-11 16:46:29 +02:00
|
|
|
func SanitizeTitle(title string) string {
|
|
|
|
s := SanitizeString(title)
|
2020-06-27 14:15:25 +02:00
|
|
|
|
2020-07-13 14:20:43 +02:00
|
|
|
if s == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
result := fs.StripKnownExt(s)
|
|
|
|
|
|
|
|
if fs.IsGenerated(result) || txt.IsTime(result) {
|
|
|
|
result = ""
|
|
|
|
} else if result == s {
|
|
|
|
// Do nothing.
|
|
|
|
} else if found := LowerCaseRegexp.FindString(result); found != result {
|
|
|
|
result = strings.ReplaceAll(strings.ReplaceAll(result, "_", " "), " ", " ")
|
|
|
|
} else if formatted := txt.FileTitle(s); formatted != "" {
|
|
|
|
result = formatted
|
|
|
|
} else {
|
|
|
|
result = txt.Title(strings.ReplaceAll(result, "-", " "))
|
2020-06-27 14:15:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
return result
|
2020-06-27 14:15:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SanitizeDescription normalizes descriptions and removes unwanted information.
|
2020-07-11 16:46:29 +02:00
|
|
|
func SanitizeDescription(s string) string {
|
|
|
|
s = SanitizeString(s)
|
2020-06-27 14:15:25 +02:00
|
|
|
|
2020-07-13 14:20:43 +02:00
|
|
|
if s == "" {
|
|
|
|
return ""
|
|
|
|
} else if remove := UnwantedDescriptions[s]; remove {
|
2020-07-11 16:46:29 +02:00
|
|
|
s = ""
|
|
|
|
} else if strings.HasPrefix(s, "DCIM\\") && !strings.Contains(s, " ") {
|
|
|
|
s = ""
|
2020-06-27 14:15:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
return s
|
2020-06-27 14:15:25 +02:00
|
|
|
}
|
2020-10-19 11:50:54 +02:00
|
|
|
|
|
|
|
// SanitizeMeta normalizes metadata fields that may contain JSON arrays like keywords and subject.
|
|
|
|
func SanitizeMeta(s string) string {
|
|
|
|
if s == "" {
|
|
|
|
return ""
|
|
|
|
} else if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
|
|
|
|
var words []string
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(s), &words); err != nil {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.Join(words, ", ")
|
|
|
|
} else {
|
|
|
|
s = SanitizeString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|