You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.1KB

  1. using System.Text;
  2. namespace ToolsServices
  3. {
  4. public static class FilesService
  5. {
  6. public static bool IsExtensionSupport(string extension)
  7. {
  8. return extension is ".pdf" or ".docx" or ".xlsx" or ".txt" or ".csv" or ".rtf";
  9. }
  10. public static string ExtractTextFromBytes(byte[] bytes, string fileName)
  11. {
  12. var ext = Path.GetExtension(fileName).ToLowerInvariant();
  13. return ext switch
  14. {
  15. ".pdf" => PdfService.ExtractPdfFromBytes(bytes),
  16. ".docx" => DocxService.ExtractDocxFromBytes(bytes),
  17. ".xlsx" => XlsxService.ExtractXlsxFromBytes(bytes),
  18. ".txt" => Encoding.UTF8.GetString(bytes),
  19. _ => throw new NotSupportedException($"Type non supporté : {ext}")
  20. };
  21. }
  22. public static string ExtractText(string fileFullname)
  23. {
  24. LoggerService.LogInfo($"FilesService.ExtractText : {fileFullname}");
  25. if (string.IsNullOrEmpty(fileFullname))
  26. return string.Empty;
  27. string ext = System.IO.Path.GetExtension(fileFullname).ToLower();
  28. return ext switch
  29. {
  30. ".pdf" => PdfService.ExtractTextFromPdf(fileFullname),
  31. ".docx" => DocxService.ExtractTextFromDocx(fileFullname),
  32. ".xlsx" => XlsxService.ExtractTextFromXlsx(fileFullname),
  33. ".txt" => File.ReadAllText(fileFullname),
  34. ".csv" => XlsxService.ExtractTextFromCsv(fileFullname),
  35. ".rtf" => DocxService.ExtractTextFromRtf(fileFullname),
  36. _ => TxtService.ExtractTextFromTxt(fileFullname)
  37. };
  38. }
  39. public static (List<string>, List<string>) GetListesFichiers(List<string> fichiers)
  40. {
  41. LoggerService.LogInfo($"FilesService.GetListesFichiers");
  42. // Extensions d’images acceptées
  43. string[] extensionsImages = ImagesExtensions();
  44. // Séparer les images
  45. var images = fichiers
  46. .Where(f => extensionsImages.Contains(Path.GetExtension(f).ToLower()))
  47. .ToList();
  48. var autres = fichiers
  49. .Where(f => !extensionsImages.Contains(Path.GetExtension(f).ToLower()))
  50. .ToList();
  51. if (images.Count > 0)
  52. {
  53. LoggerService.LogDebug($"{images.Count} image(s) : ");
  54. foreach (var d in images)
  55. {
  56. LoggerService.LogDebug($"{d}");
  57. }
  58. }
  59. if (autres.Count > 0)
  60. {
  61. LoggerService.LogDebug($"{autres.Count} document(s) : ");
  62. foreach (var d in autres)
  63. {
  64. LoggerService.LogDebug($" - {d}");
  65. }
  66. }
  67. return (images, autres);
  68. }
  69. private static string[] ImagesExtensions()
  70. {
  71. string[] extensionsImages = { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp" };
  72. return extensionsImages;
  73. }
  74. }
  75. }