FileUtils.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. if (string.IsNullOrEmpty(directoryPath))
  42. return directoryPath;
  43. directoryPath = directoryPath.Trim();
  44. if (directoryPath.EndsWith(fileSystemAbstraction.Path.DirectorySeparatorChar))
  45. return directoryPath;
  46. return directoryPath + fileSystemAbstraction.Path.DirectorySeparatorChar;
  47. }
  48. public static string EnsurePathNotEndsWithDirSeparator(IFileSystem fileSystemAbstraction, string directoryPath)
  49. {
  50. if (fileSystemAbstraction == null && !string.IsNullOrEmpty(directoryPath))
  51. throw new ArgumentNullException(nameof(fileSystemAbstraction));
  52. if (string.IsNullOrEmpty(directoryPath))
  53. return directoryPath;
  54. directoryPath = directoryPath.Trim();
  55. if (!directoryPath.EndsWith(fileSystemAbstraction.Path.DirectorySeparatorChar))
  56. return directoryPath;
  57. return directoryPath.RemoveLast();
  58. }
  59. public static string GetConfigBasePath(IFileSystem fileSystemAbstraction)
  60. {
  61. #if LINUX
  62. return $"{fileSystemAbstraction.Path.DirectorySeparatorChar}etc";
  63. #else
  64. #if DEBUG
  65. var localAppDir = fileSystemAbstraction.Directory.GetCurrentDirectory();
  66. #else
  67. var localAppDir = Environment.GetEnvironmentVariable(CS_CFG_VAR);
  68. #endif
  69. if (string.IsNullOrEmpty(localAppDir))
  70. return Assembly.GetEntryAssembly().Location;
  71. return EnsurePathNotEndsWithDirSeparator(fileSystemAbstraction, localAppDir.EnsurePathNonBackslash());
  72. #endif
  73. }
  74. public static string GetConfigPath(IFileSystem fileSystemAbstraction)
  75. {
  76. return EnsurePathNotEndsWithDirSeparator(fileSystemAbstraction, (GetConfigBasePath(fileSystemAbstraction) + fileSystemAbstraction.Path.DirectorySeparatorChar + Assembly.GetEntryAssembly().GetName().Name));
  77. }
  78. public static string GetLogsPath(IFileSystem fileSystemAbstraction)
  79. {
  80. return EnsurePathNotEndsWithDirSeparator(fileSystemAbstraction,(GetLogBasePath(fileSystemAbstraction) + Path.DirectorySeparatorChar + Assembly.GetEntryAssembly().GetName().Name));
  81. }
  82. public static string GetLogBasePath(IFileSystem fileSystemAbstraction)
  83. {
  84. #if LINUX
  85. return $"{fileSystemAbstraction.Path.DirectorySeparatorChar}var{fileSystemAbstraction.Path.DirectorySeparatorChar}logs";
  86. #else
  87. return GetConfigBasePath(fileSystemAbstraction);
  88. #endif
  89. }
  90. }
  91. }