5.4 KiB
File Tracking Enhancement - Event & Article Attachments
Problem
The file tracking system was not properly tracking event attachments and article attachments, causing files like PPTX documents to show "0 usage" even when they were actively being used in events or articles.
Root Cause
-
Event Attachments: The
TrackEventFilesfunction infile_tracker.goonly trackedimage_urlandfile_urlfields, but completely ignored theAttachmentsarray that stores multiple file attachments per event. -
Article Attachments: The
TrackArticleFilesfunction attempted to parse the attachments JSON but had a bug where all attachment field names were set to"attachments", causing them to overwrite each other in the map (only the last attachment would be tracked).
Solution
Backend Changes
1. Enhanced file_tracker.go
-
Added imports:
encoding/jsonandpath/filepathfor better parsing and filename extraction -
Fixed
TrackArticleFiles:- Properly parses JSON array of attachment URLs
- Generates unique field names using the filename:
attachment_[filename] - Handles duplicate filenames by appending suffixes
- Falls back to comma-separated parsing if JSON parsing fails
-
Fixed
TrackEventFiles:- Iterates through all event attachments
- Generates field names from attachment name or filename
- Ensures unique field names using counters
- Properly tracks each attachment URL separately
2. New Admin Endpoint - RefreshFileTracking
Location: internal/controllers/files_controller.go
- Purpose: Re-scans all entities and updates file usage tracking
- Route:
POST /api/v1/admin/files/refresh-tracking - Optional parameter:
entity_type(article, event, player, sponsor, contact, team, settings) - Returns: Statistics of scanned entities by type
- Features:
- Scans all entities in the database
- Updates file usage records for each entity
- Can target specific entity types or scan all
- Provides detailed statistics on completion
3. Enhanced MIME Type Detection
Expanded detectMimeType function to support many more file types including:
- Office documents (DOCX, XLSX, PPTX, PPT, DOC, XLS)
- Archives (ZIP, RAR, 7Z, TAR, GZ)
- Media files (MP4, AVI, MOV, MP3, WAV)
- Additional image formats (BMP, ICO)
Frontend Changes
1. New API Service Function
Location: frontend/src/services/files.ts
export const refreshFileTracking = async (entityType?: string)
- Calls the new backend endpoint
- Returns statistics of scanned entities
2. Enhanced Files Admin Page
Location: frontend/src/pages/admin/FilesAdminPage.tsx
-
New Button: "Aktualizovat sledování" (Refresh Tracking)
- Green outline style to differentiate from scan button
- Located next to the existing "Skenovat soubory" button
- Shows loading state during operation
-
New Modal: Displays refresh tracking results
- Shows success message
- Displays statistics for each entity type:
- Articles (Články)
- Activities/Events (Aktivity)
- Players (Hráči)
- Sponsors (Sponzoři)
- Contacts (Kontakty)
- Teams (Týmy)
How It Works
File Tracking Flow
- When an event/article is created or updated, the tracking function is called
- The function extracts all file URLs (images, files, attachments)
- For each URL, it creates a unique field name
- The
UpdateFileUsagesfunction manages the file_usages table:- Adds new usage records
- Removes old usage records no longer present
- Updates existing records if URLs change
Field Naming Convention
- Main fields:
image_url,file_url,og_image_url,logo_url - Attachments:
attachment_[filename] - Duplicates:
attachment_[filename]_a,attachment_[filename]_b, etc.
Usage Instructions
For Existing Files (Your Case)
- Go to Admin → Files Management
- Click the green "Aktualizovat sledování" (Refresh Tracking) button
- Wait for the process to complete
- A modal will show statistics of how many entities were scanned
- Refresh the files list to see updated usage counts
For New Files
File tracking happens automatically when:
- Creating a new event with attachments
- Updating an event's attachments
- Creating/updating articles with attachments
- Any entity with file references is saved
Files Modified
Backend
internal/services/file_tracker.go- Fixed attachment tracking logicinternal/controllers/files_controller.go- Added RefreshFileTracking endpoint, enhanced MIME detectioninternal/routes/routes.go- Added new route
Frontend
frontend/src/services/files.ts- Added refreshFileTracking functionfrontend/src/pages/admin/FilesAdminPage.tsx- Added refresh button and result modal
Benefits
- Accurate Usage Tracking: All attachments are now properly tracked
- Better File Management: Admins can see which files are truly unused
- Prevents Accidental Deletion: Files in use will be protected
- Easy Maintenance: One-click refresh of all tracking data
- Comprehensive MIME Support: Better file type detection for various formats
Testing Recommendations
- Create an event with multiple PPTX attachments
- Check the files admin page - usage count should be > 0
- Click "Aktualizovat sledování" to refresh tracking
- Verify all attachment types are tracked correctly
- Try deleting a file that's in use - should show warning with usage info