Switched general YYYYMMDD Filter to a WhatsApp specific [IMG,VID]-YYYYMMDD-WA* Filter
This commit is contained in:
parent
37c40c9b3d
commit
08ba1e1c05
|
@ -11,8 +11,8 @@ import (
|
||||||
|
|
||||||
var DateRegexp = regexp.MustCompile("\\D\\d{4}[\\-_]\\d{2}[\\-_]\\d{2,}")
|
var DateRegexp = regexp.MustCompile("\\D\\d{4}[\\-_]\\d{2}[\\-_]\\d{2,}")
|
||||||
var DatePathRegexp = regexp.MustCompile("\\D\\d{4}/\\d{1,2}/?\\d*")
|
var DatePathRegexp = regexp.MustCompile("\\D\\d{4}/\\d{1,2}/?\\d*")
|
||||||
var DateNoSepRegexp = regexp.MustCompile("\\D(?P<year>\\d{4})(?P<month>\\d{2})(?P<day>\\d{2})\\D")
|
|
||||||
var DateTimeRegexp = regexp.MustCompile("\\D\\d{2,4}[\\-_]\\d{2}[\\-_]\\d{2}.{1,4}\\d{2}\\D\\d{2}\\D\\d{2,}")
|
var DateTimeRegexp = regexp.MustCompile("\\D\\d{2,4}[\\-_]\\d{2}[\\-_]\\d{2}.{1,4}\\d{2}\\D\\d{2}\\D\\d{2,}")
|
||||||
|
var DateWhatsAppRegexp = regexp.MustCompile("(?:IMG|VID)-(?P<year>\\d{4})(?P<month>\\d{2})(?P<day>\\d{2})-WA")
|
||||||
var DateIntRegexp = regexp.MustCompile("\\d{1,4}")
|
var DateIntRegexp = regexp.MustCompile("\\d{1,4}")
|
||||||
var YearRegexp = regexp.MustCompile("\\d{4,5}")
|
var YearRegexp = regexp.MustCompile("\\d{4,5}")
|
||||||
var IsDateRegexp = regexp.MustCompile("\\d{4}[\\-_]?\\d{2}[\\-_]?\\d{2}")
|
var IsDateRegexp = regexp.MustCompile("\\d{4}[\\-_]?\\d{2}[\\-_]?\\d{2}")
|
||||||
|
|
|
@ -73,38 +73,6 @@ func DateFromFilePath(s string) (result time.Time) {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
result = time.Date(
|
|
||||||
year,
|
|
||||||
time.Month(month),
|
|
||||||
day,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
time.UTC)
|
|
||||||
} else if found = DateNoSepRegexp.Find(b); len(found) > 0 { // Is it a date path like "20200103"?
|
|
||||||
match := DateNoSepRegexp.FindSubmatch(b)
|
|
||||||
|
|
||||||
if len(match) != 4 {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
matchMap := make(map[string]string)
|
|
||||||
for i, name := range DateNoSepRegexp.SubexpNames() {
|
|
||||||
if i != 0 {
|
|
||||||
matchMap[name] = string(match[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
year := ExpandYear(matchMap["year"])
|
|
||||||
month := Int(matchMap["month"])
|
|
||||||
day := Int(matchMap["day"])
|
|
||||||
|
|
||||||
// Perform date plausibility check.
|
|
||||||
if year < YearMin || year > YearMax || month < MonthMin || month > MonthMax || day < DayMin || day > DayMax {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
result = time.Date(
|
result = time.Date(
|
||||||
year,
|
year,
|
||||||
time.Month(month),
|
time.Month(month),
|
||||||
|
@ -149,6 +117,38 @@ func DateFromFilePath(s string) (result time.Time) {
|
||||||
0,
|
0,
|
||||||
time.UTC)
|
time.UTC)
|
||||||
}
|
}
|
||||||
|
} else if found = DateWhatsAppRegexp.Find(b); len(found) > 0 { // Is it a WhatsApp date path like "VID-20191120-WA0001.jpg"?
|
||||||
|
match := DateWhatsAppRegexp.FindSubmatch(b)
|
||||||
|
|
||||||
|
if len(match) != 4 {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
matchMap := make(map[string]string)
|
||||||
|
for i, name := range DateWhatsAppRegexp.SubexpNames() {
|
||||||
|
if i != 0 {
|
||||||
|
matchMap[name] = string(match[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
year := ExpandYear(matchMap["year"])
|
||||||
|
month := Int(matchMap["month"])
|
||||||
|
day := Int(matchMap["day"])
|
||||||
|
|
||||||
|
// Perform date plausibility check.
|
||||||
|
if year < YearMin || year > YearMax || month < MonthMin || month > MonthMax || day < DayMin || day > DayMax {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
result = time.Date(
|
||||||
|
year,
|
||||||
|
time.Month(month),
|
||||||
|
day,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
time.UTC)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.UTC()
|
return result.UTC()
|
||||||
|
|
|
@ -61,13 +61,11 @@ func TestDateFromFilePath(t *testing.T) {
|
||||||
})
|
})
|
||||||
t.Run("/2020/1212/20130518_142022_3D657EBD.jpg", func(t *testing.T) {
|
t.Run("/2020/1212/20130518_142022_3D657EBD.jpg", func(t *testing.T) {
|
||||||
result := DateFromFilePath("/2020/1212/20130518_142022_3D657EBD.jpg")
|
result := DateFromFilePath("/2020/1212/20130518_142022_3D657EBD.jpg")
|
||||||
assert.False(t, result.IsZero())
|
assert.True(t, result.IsZero(), "\"/2020/1212/20130518_142022_3D657EBD.jpg\" should not generate a valid Date. This is the filename which PhotoPrism generates when importing photos")
|
||||||
assert.Equal(t, "2013-05-18 00:00:00 +0000 UTC", result.String())
|
|
||||||
})
|
})
|
||||||
t.Run("20130518_142022_3D657EBD.jpg", func(t *testing.T) {
|
t.Run("20130518_142022_3D657EBD.jpg", func(t *testing.T) {
|
||||||
result := DateFromFilePath("20130518_142022_3D657EBD.jpg")
|
result := DateFromFilePath("20130518_142022_3D657EBD.jpg")
|
||||||
assert.False(t, result.IsZero())
|
assert.True(t, result.IsZero(), "\"20130518_142022_3D657EBD.jpg\" should not generate a valid Date. This is the filename which PhotoPrism generates when importing photos")
|
||||||
assert.Equal(t, "2013-05-18 00:00:00 +0000 UTC", result.String())
|
|
||||||
})
|
})
|
||||||
t.Run("telegram_2020_01_30_09_57_18.jpg", func(t *testing.T) {
|
t.Run("telegram_2020_01_30_09_57_18.jpg", func(t *testing.T) {
|
||||||
result := DateFromFilePath("telegram_2020_01_30_09_57_18.jpg")
|
result := DateFromFilePath("telegram_2020_01_30_09_57_18.jpg")
|
||||||
|
|
Loading…
Reference in a new issue