amélioration tests

This commit is contained in:
Yannick Le Duc
2025-08-29 09:43:58 +02:00
parent 0818fbd0ce
commit 88fa637ac1
4 changed files with 471 additions and 5 deletions

View File

@@ -103,18 +103,74 @@ export function downloadTemplate(type: 'propositions' | 'participants'): void {
}
export function validateFileType(file: File): { isValid: boolean; error?: string } {
const isCSV = file.type === 'text/csv' || file.name.toLowerCase().endsWith('.csv');
const isCSV = file.type === 'text/csv' || (file.name && file.name.toLowerCase().endsWith('.csv'));
const isExcel = file.type === 'application/vnd.oasis.opendocument.spreadsheet' ||
file.name.toLowerCase().endsWith('.ods') ||
file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
file.type === 'application/vnd.ms-excel' ||
(file.name && (file.name.toLowerCase().endsWith('.ods') ||
file.name.toLowerCase().endsWith('.xlsx') ||
file.name.toLowerCase().endsWith('.xls');
file.name.toLowerCase().endsWith('.xls')));
const isPDF = file.type === 'application/pdf' || (file.name && file.name.toLowerCase().endsWith('.pdf'));
if (!isCSV && !isExcel) {
if (!isCSV && !isExcel && !isPDF) {
return {
isValid: false,
error: 'Veuillez sélectionner un fichier valide (CSV, ODS, XLSX ou XLS).'
error: 'Veuillez sélectionner un fichier valide (CSV, ODS, XLSX, XLS ou PDF).'
};
}
return { isValid: true };
}
/**
* Formate une taille de fichier en bytes vers une représentation lisible
*/
export function formatFileSize(bytes: number): string {
if (bytes < 0) return '0 B';
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
/**
* Extrait l'extension d'un nom de fichier
*/
export function getFileExtension(filename: string): string {
if (!filename || filename.indexOf('.') === -1) return '';
const parts = filename.split('.');
return parts[parts.length - 1];
}
/**
* Nettoie un nom de fichier en supprimant les caractères spéciaux
*/
export function sanitizeFileName(filename: string): string {
if (!filename) return '';
// Supprimer les espaces en début et fin
let sanitized = filename.trim();
// Remplacer les caractères spéciaux par des tirets
sanitized = sanitized.replace(/[^a-zA-Z0-9.-]/g, '-');
// Supprimer les tirets multiples
sanitized = sanitized.replace(/-+/g, '-');
// Supprimer les tirets en début et fin
sanitized = sanitized.replace(/^-+|-+$/g, '');
// Limiter la longueur à 255 caractères
if (sanitized.length > 255) {
const extension = getFileExtension(sanitized);
const nameWithoutExt = sanitized.substring(0, sanitized.lastIndexOf('.'));
const maxNameLength = 255 - extension.length - 1; // -1 pour le point
sanitized = nameWithoutExt.substring(0, maxNameLength) + '.' + extension;
}
return sanitized;
}