diff --git a/pkg/txt/datetime.go b/pkg/txt/datetime.go index 2ea290da2..8b8db77b4 100644 --- a/pkg/txt/datetime.go +++ b/pkg/txt/datetime.go @@ -11,8 +11,8 @@ import ( var DateRegexp = regexp.MustCompile("\\D\\d{4}[\\-_]\\d{2}[\\-_]\\d{2,}") var DatePathRegexp = regexp.MustCompile("\\D\\d{4}/\\d{1,2}/?\\d*") -var DateNoSepRegexp = regexp.MustCompile("\\D(?P\\d{4})(?P\\d{2})(?P\\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 DateWhatsAppRegexp = regexp.MustCompile("(?:IMG|VID)-(?P\\d{4})(?P\\d{2})(?P\\d{2})-WA") var DateIntRegexp = regexp.MustCompile("\\d{1,4}") var YearRegexp = regexp.MustCompile("\\d{4,5}") var IsDateRegexp = regexp.MustCompile("\\d{4}[\\-_]?\\d{2}[\\-_]?\\d{2}") diff --git a/pkg/txt/datetime_filepath.go b/pkg/txt/datetime_filepath.go index d84a07224..5d00df8fe 100644 --- a/pkg/txt/datetime_filepath.go +++ b/pkg/txt/datetime_filepath.go @@ -73,38 +73,6 @@ func DateFromFilePath(s string) (result time.Time) { 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( year, time.Month(month), @@ -149,6 +117,38 @@ func DateFromFilePath(s string) (result time.Time) { 0, 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() diff --git a/pkg/txt/datetime_filepath_test.go b/pkg/txt/datetime_filepath_test.go index 3f3ba0749..9ba5ad64b 100644 --- a/pkg/txt/datetime_filepath_test.go +++ b/pkg/txt/datetime_filepath_test.go @@ -61,13 +61,11 @@ func TestDateFromFilePath(t *testing.T) { }) t.Run("/2020/1212/20130518_142022_3D657EBD.jpg", func(t *testing.T) { result := DateFromFilePath("/2020/1212/20130518_142022_3D657EBD.jpg") - assert.False(t, result.IsZero()) - assert.Equal(t, "2013-05-18 00:00:00 +0000 UTC", result.String()) + 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") }) t.Run("20130518_142022_3D657EBD.jpg", func(t *testing.T) { result := DateFromFilePath("20130518_142022_3D657EBD.jpg") - assert.False(t, result.IsZero()) - assert.Equal(t, "2013-05-18 00:00:00 +0000 UTC", result.String()) + 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") }) t.Run("telegram_2020_01_30_09_57_18.jpg", func(t *testing.T) { result := DateFromFilePath("telegram_2020_01_30_09_57_18.jpg")