2020-06-29 11:10:24 +02:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
)
|
|
|
|
|
2020-07-14 15:00:14 +02:00
|
|
|
var DscNameRegexp = regexp.MustCompile("\\D{3}[\\d_]\\d{4,8}_?\\d{0,6}_?\\d{0,6}[\\.jpgJPGXx]{0,4}")
|
2020-07-11 16:46:29 +02:00
|
|
|
var UniqueNameRegexp = regexp.MustCompile("[a-f0-9]{8,16}_[a-f0-9]{6,16}_[A-Za-z0-9]{1,20}_?[A-Za-z0-9]{0,4}") // Example: 8263987746_d0a6055c58_o
|
|
|
|
var UUIDNameRegexp = regexp.MustCompile("[A-Fa-f0-9\\-]{16,36}_?[A-Za-z0-9_]{0,20}") // Example: 8263987746_d0a6055c58_o
|
2020-06-29 11:10:24 +02:00
|
|
|
|
|
|
|
// IsInt tests if the file base is an integer number.
|
2020-07-06 11:31:03 +02:00
|
|
|
func IsInt(s string) bool {
|
|
|
|
if s == "" {
|
2020-06-29 11:10:24 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-06 11:31:03 +02:00
|
|
|
for _, r := range s {
|
2020-06-29 11:10:24 +02:00
|
|
|
if r < 48 || r > 57 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-06 11:31:03 +02:00
|
|
|
// IsAsciiID tests if the string is a file name that only contains uppercase ascii letters and numbers like "IQVG4929".
|
|
|
|
func IsAsciiID(s string) bool {
|
|
|
|
if s == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range s {
|
2020-07-14 15:00:14 +02:00
|
|
|
if (r < 65 || r > 90) && (r < 48 || r > 57) && r != 45 && r != 95 {
|
2020-07-06 11:31:03 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
// IsUniqueName tests if the string looks like a unique file name.
|
|
|
|
func IsUniqueName(s string) bool {
|
|
|
|
if s == "" {
|
2020-06-29 11:10:24 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
if m := UniqueNameRegexp.FindString(s); s == m {
|
2020-06-29 11:10:24 +02:00
|
|
|
return true
|
2020-07-11 16:46:29 +02:00
|
|
|
} else if m := UUIDNameRegexp.FindString(s); s == m {
|
2020-06-29 11:10:24 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDscName tests if the string looks like a file name generated by a camera.
|
|
|
|
func IsDscName(s string) bool {
|
|
|
|
if s == "" {
|
|
|
|
return false
|
2020-06-29 11:10:24 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
if m := DscNameRegexp.FindString(s); s == m {
|
2020-06-29 11:10:24 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsGenerated tests if the file name looks like an automatically generated identifier.
|
|
|
|
func IsGenerated(fileName string) bool {
|
|
|
|
if fileName == "" {
|
|
|
|
return false
|
2020-06-29 11:10:24 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:00:49 +02:00
|
|
|
base := BasePrefix(fileName, false)
|
2020-07-11 16:46:29 +02:00
|
|
|
|
2020-07-14 15:00:14 +02:00
|
|
|
if IsAsciiID(base) {
|
|
|
|
return true
|
|
|
|
} else if IsHash(base) {
|
2020-07-11 16:46:29 +02:00
|
|
|
return true
|
|
|
|
} else if IsInt(base) {
|
|
|
|
return true
|
|
|
|
} else if IsDscName(base) {
|
|
|
|
return true
|
|
|
|
} else if IsUniqueName(base) {
|
|
|
|
return true
|
2022-09-28 09:01:17 +02:00
|
|
|
} else if rnd.IsUnique(base, 0) {
|
2020-07-11 16:46:29 +02:00
|
|
|
return true
|
|
|
|
} else if IsCanonical(base) {
|
|
|
|
return true
|
2020-07-06 11:31:03 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 11:10:24 +02:00
|
|
|
return false
|
|
|
|
}
|