mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 02:32:57 +00:00
dev day #63
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fotbal-club/internal/models"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -120,19 +123,35 @@ func (ft *FileTracker) TrackArticleFiles(article *models.Article) error {
|
||||
|
||||
// Track attachments if present
|
||||
if article.Attachments != "" {
|
||||
// Attachments is a JSON array of URLs
|
||||
// For simplicity, we'll track each attachment URL separately
|
||||
// You might want to parse the JSON properly in production
|
||||
attachments := strings.Split(article.Attachments, ",")
|
||||
for i, attachment := range attachments {
|
||||
attachment = strings.Trim(attachment, `[]" `)
|
||||
if attachment != "" {
|
||||
fieldName := "attachments"
|
||||
if i > 0 {
|
||||
// If multiple attachments, differentiate them
|
||||
fieldName = "attachments"
|
||||
// Attachments is a JSON array of URLs - parse properly
|
||||
var attachmentURLs []string
|
||||
if err := json.Unmarshal([]byte(article.Attachments), &attachmentURLs); err == nil {
|
||||
// Successfully parsed as JSON array
|
||||
for i, attachmentURL := range attachmentURLs {
|
||||
if attachmentURL != "" {
|
||||
// Extract filename from URL for better field naming
|
||||
filename := filepath.Base(attachmentURL)
|
||||
fieldName := "attachment_" + filename
|
||||
// Ensure unique field names if same filename appears multiple times
|
||||
if _, exists := fieldURLMap[fieldName]; exists {
|
||||
fieldName = "attachment_" + filename + "_" + strconv.Itoa(i)
|
||||
}
|
||||
fieldURLMap[fieldName] = attachmentURL
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Fallback to simple comma-separated parsing
|
||||
attachments := strings.Split(article.Attachments, ",")
|
||||
for i, attachment := range attachments {
|
||||
attachment = strings.Trim(attachment, `[]" `)
|
||||
if attachment != "" {
|
||||
filename := filepath.Base(attachment)
|
||||
fieldName := "attachment_" + filename
|
||||
if _, exists := fieldURLMap[fieldName]; exists {
|
||||
fieldName = "attachment_" + filename + "_" + strconv.Itoa(i)
|
||||
}
|
||||
fieldURLMap[fieldName] = attachment
|
||||
}
|
||||
fieldURLMap[fieldName] = attachment
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,6 +181,39 @@ func (ft *FileTracker) TrackEventFiles(event *models.Event) error {
|
||||
"image_url": event.ImageURL,
|
||||
"file_url": event.FileURL,
|
||||
}
|
||||
|
||||
// Track each attachment separately
|
||||
for i, attachment := range event.Attachments {
|
||||
if attachment.URL != "" {
|
||||
// Generate field name from attachment name or filename
|
||||
fieldName := ""
|
||||
if attachment.Name != "" {
|
||||
// Use attachment name if available
|
||||
fieldName = "attachment_" + strings.ReplaceAll(attachment.Name, " ", "_")
|
||||
} else {
|
||||
// Fall back to filename from URL
|
||||
filename := filepath.Base(attachment.URL)
|
||||
fieldName = "attachment_" + filename
|
||||
}
|
||||
|
||||
// Ensure unique field names if duplicates exist
|
||||
originalFieldName := fieldName
|
||||
for counter := 0; ; counter++ {
|
||||
if _, exists := fieldURLMap[fieldName]; !exists {
|
||||
break
|
||||
}
|
||||
// Add counter suffix if field name already exists
|
||||
fieldName = originalFieldName + "_" + strconv.Itoa(counter)
|
||||
if counter > 999 { // Prevent infinite loop
|
||||
fieldName = originalFieldName + "_" + strconv.Itoa(i)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
fieldURLMap[fieldName] = attachment.URL
|
||||
}
|
||||
}
|
||||
|
||||
return ft.UpdateFileUsages("event", event.ID, fieldURLMap)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user