diff --git a/internal/handlers/web/document_remove.go b/internal/handlers/web/document_remove.go index 379b545..1bddfae 100644 --- a/internal/handlers/web/document_remove.go +++ b/internal/handlers/web/document_remove.go @@ -483,7 +483,7 @@ func BulkRemoveDocumentsHandler(w http.ResponseWriter, r *http.Request) { // Get age filter (optional) var ageFilterDays int - if ageStr := r.FormValue("ageFilter"); ageStr != "" { + if ageStr := r.FormValue("age-filter"); ageStr != "" { if days, err := strconv.Atoi(ageStr); err == nil && days > 0 { ageFilterDays = days log.Printf("Using age filter: older than %d days", ageFilterDays) @@ -494,6 +494,7 @@ func BulkRemoveDocumentsHandler(w http.ResponseWriter, r *http.Request) { var cutoffDate time.Time if ageFilterDays > 0 { cutoffDate = time.Now().AddDate(0, 0, -ageFilterDays) + log.Printf("Cutoff date for age filter: %s", cutoffDate.Format("2006-01-02")) } // Structure to track results @@ -1349,9 +1350,47 @@ func BulkRemoveDocumentsHandler(w http.ResponseWriter, r *http.Request) { } } - // Comment out problematic log line - // log.Printf("Attachment %s (type: %v, created: %s) matches all criteria - queued for deletion", - // filename, purposeId, attachment["createdOn"]) + // Check filename pattern + if len(filenamePatterns) > 0 && !matchesAnyPattern(filename, filenamePatterns) { + log.Printf("Skipping attachment %s - filename '%s' doesn't match patterns: %v", + attachmentIDStr, filename, filenamePatterns) + continue + } + + // Check age filter if applicable + if ageFilterDays > 0 { + // Get creation time + var createdAt time.Time + var createdOn string + var hasDate bool + + // Try to get the creation date + if created, ok := attachment["createdOn"].(string); ok { + createdOn = created + hasDate = true + } else if created, ok := attachment["created"].(string); ok { + createdOn = created + hasDate = true + } else if lastModified, ok := attachment["lastModified"].(string); ok { + createdOn = lastModified + hasDate = true + } else if createdVal, ok := attachment["created"].(float64); ok { + createdAt = time.Unix(int64(createdVal), 0) + createdOn = createdAt.Format(time.RFC3339) + hasDate = true + } + + if hasDate { + if parsedTime, err := time.Parse(time.RFC3339, createdOn); err == nil { + createdAt = parsedTime + if createdAt.After(cutoffDate) { + log.Printf("Skipping attachment %s - created on %s is newer than cutoff %s", + filename, createdAt.Format("2006-01-02"), cutoffDate.Format("2006-01-02")) + continue // Skip if not old enough + } + } + } + } // Log that we found an attachment to delete log.Printf("Attachment %s matches criteria - will be deleted", filename) @@ -1564,10 +1603,23 @@ func stringInSlice(s string, slice []string) bool { // Helper function to check if a string matches any pattern in a slice func matchesAnyPattern(s string, patterns []string) bool { + // Convert the string to lowercase for case-insensitive comparison + sLower := strings.ToLower(s) + for _, pattern := range patterns { - match, _ := regexp.MatchString("(?i)"+pattern, s) - if match { - return true + // Check if the pattern is a wildcard pattern (contains *) + if strings.Contains(pattern, "*") { + // Convert wildcard pattern to regex + regexPattern := strings.ReplaceAll(pattern, "*", ".*") + match, _ := regexp.MatchString("(?i)^"+regexPattern+"$", s) + if match { + return true + } + } else { + // For non-wildcard patterns, check for exact match (case-insensitive) + if sLower == strings.ToLower(pattern) { + return true + } } } return false diff --git a/templates/partials/document_remove.html b/templates/partials/document_remove.html index c96ad3e..ebf13cd 100644 --- a/templates/partials/document_remove.html +++ b/templates/partials/document_remove.html @@ -54,42 +54,52 @@
- +
+ - + -
- +
+ - -
- +
+ + - - + + -
- -
+
+
+ - -
- -
- -
+ +
+ +
+
+