Files
projet_gestion_commande/backend/gestion/utils/utils.go
T
2026-03-11 21:21:04 +01:00

35 lines
838 B
Go

package utils
import (
"crypto/rand"
"fmt"
"gestion/db"
"gestion/models"
"math/big"
"os"
"path/filepath"
)
// --------------------------------------------
// 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())
ext := filepath.Ext(originalFileName)
return fmt.Sprintf("%s_%s%s", productName, randomString, ext)
}
func DeleteOldFile(filePath string) error {
return os.Remove(filePath)
}