Browse Source

fix: updated logic for filtering removals

document-upload-removal-layout-update
nic 12 months ago
parent
commit
a4fda8ed16
  1. 62
      internal/handlers/web/document_remove.go
  2. 20
      templates/partials/document_remove.html

62
internal/handlers/web/document_remove.go

@ -483,7 +483,7 @@ func BulkRemoveDocumentsHandler(w http.ResponseWriter, r *http.Request) {
// Get age filter (optional) // Get age filter (optional)
var ageFilterDays int 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 { if days, err := strconv.Atoi(ageStr); err == nil && days > 0 {
ageFilterDays = days ageFilterDays = days
log.Printf("Using age filter: older than %d days", ageFilterDays) 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 var cutoffDate time.Time
if ageFilterDays > 0 { if ageFilterDays > 0 {
cutoffDate = time.Now().AddDate(0, 0, -ageFilterDays) cutoffDate = time.Now().AddDate(0, 0, -ageFilterDays)
log.Printf("Cutoff date for age filter: %s", cutoffDate.Format("2006-01-02"))
} }
// Structure to track results // Structure to track results
@ -1349,9 +1350,47 @@ func BulkRemoveDocumentsHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
// Comment out problematic log line // Check filename pattern
// log.Printf("Attachment %s (type: %v, created: %s) matches all criteria - queued for deletion", if len(filenamePatterns) > 0 && !matchesAnyPattern(filename, filenamePatterns) {
// filename, purposeId, attachment["createdOn"]) 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 that we found an attachment to delete
log.Printf("Attachment %s matches criteria - will be deleted", filename) log.Printf("Attachment %s matches criteria - will be deleted", filename)
@ -1564,11 +1603,24 @@ func stringInSlice(s string, slice []string) bool {
// Helper function to check if a string matches any pattern in a slice // Helper function to check if a string matches any pattern in a slice
func matchesAnyPattern(s string, patterns []string) bool { func matchesAnyPattern(s string, patterns []string) bool {
// Convert the string to lowercase for case-insensitive comparison
sLower := strings.ToLower(s)
for _, pattern := range patterns { for _, pattern := range patterns {
match, _ := regexp.MatchString("(?i)"+pattern, s) // 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 { if match {
return true return true
} }
} else {
// For non-wildcard patterns, check for exact match (case-insensitive)
if sLower == strings.ToLower(pattern) {
return true
}
}
} }
return false return false
} }

20
templates/partials/document_remove.html

@ -54,7 +54,17 @@
<div class="form-group"> <div class="form-group">
<label for="age-filter">Remove Files Older Than (days):</label> <label for="age-filter">Remove Files Older Than (days):</label>
<input type="number" id="age-filter" name="ageFilter" class="card-input" min="0"> <select class="card-input" id="age-filter" name="age-filter">
<option value="0" selected>Any</option>
<option value="1">1 Day</option>
<option value="7">7 Days</option>
<option value="30">30 Days</option>
<option value="90">90 Days</option>
<option value="120">120 Days</option>
<option value="180">180 Days</option>
<option value="365">365 Days</option>
</select>
</div>
</div> </div>
<input type="hidden" id="bulk-job-ids" name="jobIDs"> <input type="hidden" id="bulk-job-ids" name="jobIDs">
@ -80,15 +90,15 @@
<div id="job-results" class="job-selection"> <div id="job-results" class="job-selection">
<!-- Jobs and document selection will appear here --> <!-- Jobs and document selection will appear here -->
</div> </div>
</div> </div>
<!-- Step 3: Results --> <!-- Step 3: Results -->
<div class="content"> <div class="content">
<h3 class="submenu-header">Step 3: Removal Results</h3> <h3 class="submenu-header">Step 3: Removal Results</h3>
<div id="removal-results" class="upload-results"> <div id="removal-results" class="upload-results">
<!-- Results will appear here after removing documents --> <!-- Results will appear here after removing documents -->
</div> </div>
</div> </div>
</div> </div>
<script> <script>

Loading…
Cancel
Save