FileUtils.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.IO;
  3. using System.IO.Abstractions;
  4. using System.Reflection;
  5. using System.Text.RegularExpressions;
  6. using Quadarax.Foundation.Core.Value.Extensions;
  7. namespace Quadarax.Foundation.Core.IO
  8. {
  9. public static class FileUtils
  10. {
  11. private const string CS_CFG_VAR = "LocalAppData";
  12. private static Regex? _removeInvalidChars;
  13. public static string SanitizedFileName(IFileSystem fileSystemAbstraction, string fileName, string replacement = "_")
  14. {
  15. if (fileSystemAbstraction == null)
  16. throw new ArgumentNullException(nameof(fileSystemAbstraction));
  17. if (string.IsNullOrEmpty(fileName))
  18. throw new ArgumentNullException(nameof(fileName));
  19. _removeInvalidChars ??= new Regex($"[{Regex.Escape(new string(fileSystemAbstraction.Path.GetInvalidFileNameChars()))}]",
  20. RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.CultureInvariant);
  21. return _removeInvalidChars.Replace(fileName, replacement);
  22. }
  23. public static void TestWriteTempFile(IFileSystem fileSystemAbstraction, string directory)
  24. {
  25. if (fileSystemAbstraction == null)
  26. throw new ArgumentNullException(nameof(fileSystemAbstraction));
  27. var fileName = fileSystemAbstraction.Path.GetRandomFileName();
  28. var fullFileName = directory.EnsurePathBackslash() + fileName;
  29. using (var sw = fileSystemAbstraction.File.CreateText(fullFileName))
  30. {
  31. sw.Write('T');
  32. sw.Flush();
  33. }
  34. if (fileSystemAbstraction.File.Exists(fullFileName))
  35. fileSystemAbstraction.File.Delete(fullFileName);
  36. }
  37. public static string EnsurePathEndsWithDirSeparator(IFileSystem fileSystemAbstraction, string directoryPath)
  38. {
  39. if (fileSystemAbstraction == null && !string.IsNullOrEmpty(directoryPath))
  40. throw new ArgumentNullException(nameof(fileSystemAbstraction));
  41. var sepChar = '\\';
  42. if (fileSystemAbstraction != null)
  43. sepChar = fileSystemAbstraction.Path.DirectorySeparatorChar;
  44. if (string.IsNullOrEmpty(directoryPath))
  45. return directoryPath;
  46. directoryPath = directoryPath.Trim();
  47. if (directoryPath.EndsWith(sepChar))
  48. return directoryPath;
  49. return directoryPath + sepChar;
  50. }
  51. public static string EnsurePathNotEndsWithDirSeparator(IFileSystem fileSystemAbstraction, string directoryPath)
  52. {
  53. if (fileSystemAbstraction == null && !string.IsNullOrEmpty(directoryPath))
  54. throw new ArgumentNullException(nameof(fileSystemAbstraction));
  55. if (string.IsNullOrEmpty(directoryPath))
  56. return directoryPath;
  57. directoryPath = directoryPath.Trim();
  58. if (fileSystemAbstraction != null && !directoryPath.EndsWith(GetDirSepChar(fileSystemAbstraction)))
  59. return directoryPath;
  60. return directoryPath.RemoveLast();
  61. }
  62. public static string? GetConfigBasePath(IFileSystem fileSystemAbstraction)
  63. {
  64. #if LINUX
  65. return $"{fileSystemAbstraction.Path.DirectorySeparatorChar}etc";
  66. #else
  67. #if DEBUG
  68. var localAppDir = fileSystemAbstraction.Directory.GetCurrentDirectory();
  69. #else
  70. var localAppDir = Environment.GetEnvironmentVariable(CS_CFG_VAR);
  71. #endif
  72. if (string.IsNullOrEmpty(localAppDir))
  73. return Assembly.GetEntryAssembly()?.Location;
  74. return EnsurePathNotEndsWithDirSeparator(fileSystemAbstraction, localAppDir.EnsurePathNonBackslash());
  75. #endif
  76. }
  77. public static string GetConfigPath(IFileSystem fileSystemAbstraction)
  78. {
  79. return EnsurePathNotEndsWithDirSeparator(fileSystemAbstraction, (GetConfigBasePath(fileSystemAbstraction) + fileSystemAbstraction.Path.DirectorySeparatorChar + Assembly.GetEntryAssembly().GetName().Name));
  80. }
  81. public static string GetLogsPath(IFileSystem fileSystemAbstraction)
  82. {
  83. return EnsurePathNotEndsWithDirSeparator(fileSystemAbstraction,(GetLogBasePath(fileSystemAbstraction) + Path.DirectorySeparatorChar + Assembly.GetEntryAssembly()?.GetName().Name));
  84. }
  85. public static string GetLogBasePath(IFileSystem fileSystemAbstraction)
  86. {
  87. #if LINUX
  88. return $"{fileSystemAbstraction.Path.DirectorySeparatorChar}var{fileSystemAbstraction.Path.DirectorySeparatorChar}logs";
  89. #else
  90. return GetConfigBasePath(fileSystemAbstraction);
  91. #endif
  92. }
  93. private static char GetDirSepChar(IFileSystem fileSystemAbstraction)
  94. {
  95. var sepChar = fileSystemAbstraction.Path.DirectorySeparatorChar;
  96. return sepChar;
  97. }
  98. }
  99. }