Files
projet_gestion_commande/backend/gestion/utils/utils.go
T
Nuxgrid 020ba7c1e4
Backend - Build & Lint / build (push) Failing after 30m33s
chore: build
2026-08-01 21:51:32 +02:00

33 lines
862 B
Go

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
}