package utils import ( "crypto/rand" "fmt" "math/big" "path/filepath" "strings" ) func GenerateUniqueFileName(productName string, originalFileName string) string { n, _ := rand.Int(rand.Reader, big.NewInt(1000000)) randomString := fmt.Sprintf("%d", n.Int64()) ext := filepath.Ext(originalFileName) return fmt.Sprintf("%s_%s%s", productName, randomString, ext) } // SanitizeFilePath valide qu'un chemin de fichier local reste bien dans le // dossier uploads/ et ne contient pas de tentative de path traversal. func SanitizeFilePath(path string) (string, error) { cleaned := filepath.Clean(path) if strings.Contains(cleaned, "..") { return "", fmt.Errorf("path traversal détecté") } if !strings.HasPrefix(cleaned, "uploads/") && !strings.HasPrefix(cleaned, "uploads\\") { return "", fmt.Errorf("chemin invalide") } return cleaned, nil }