chore: build
This commit is contained in:
@@ -3,25 +3,16 @@ package utils
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"gestion/db"
|
||||
"gestion/models"
|
||||
"math/big"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/text/runes"
|
||||
"golang.org/x/text/transform"
|
||||
"golang.org/x/text/unicode/norm"
|
||||
)
|
||||
|
||||
// --------------------------------------------
|
||||
// MEDIA
|
||||
// --------------------------------------------
|
||||
|
||||
func GetMediaForProduct(productID int) ([]models.Media, error) {
|
||||
return db.DB.GetMediaByProductID(productID)
|
||||
}
|
||||
|
||||
// --------------------------------------------
|
||||
// FILES NAMES
|
||||
// --------------------------------------------
|
||||
|
||||
func GenerateUniqueFileName(productName string, originalFileName string) string {
|
||||
n, _ := rand.Int(rand.Reader, big.NewInt(1000000))
|
||||
randomString := fmt.Sprintf("%d", n.Int64())
|
||||
@@ -29,6 +20,26 @@ func GenerateUniqueFileName(productName string, originalFileName string) string
|
||||
return fmt.Sprintf("%s_%s%s", productName, randomString, ext)
|
||||
}
|
||||
|
||||
func DeleteOldFile(filePath string) error {
|
||||
return os.Remove(filePath)
|
||||
// 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
|
||||
}
|
||||
|
||||
// NormalizeAddress supprime les accents et normalise les espaces, pour
|
||||
// comparer deux adresses saisies différemment (casse/accents/espaces).
|
||||
func NormalizeAddress(s string) string {
|
||||
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
|
||||
result, _, _ := transform.String(t, s)
|
||||
return strings.Join(strings.Fields(result), " ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user