BaseApplicationPaths.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Jellyfin.Extensions;
  6. using MediaBrowser.Common.Configuration;
  7. namespace Emby.Server.Implementations.AppBase
  8. {
  9. /// <summary>
  10. /// Provides a base class to hold common application paths used by both the UI and Server.
  11. /// This can be subclassed to add application-specific paths.
  12. /// </summary>
  13. public abstract class BaseApplicationPaths : IApplicationPaths
  14. {
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="BaseApplicationPaths"/> class.
  17. /// </summary>
  18. /// <param name="programDataPath">The program data path.</param>
  19. /// <param name="logDirectoryPath">The log directory path.</param>
  20. /// <param name="configurationDirectoryPath">The configuration directory path.</param>
  21. /// <param name="cacheDirectoryPath">The cache directory path.</param>
  22. /// <param name="webDirectoryPath">The web directory path.</param>
  23. protected BaseApplicationPaths(
  24. string programDataPath,
  25. string logDirectoryPath,
  26. string configurationDirectoryPath,
  27. string cacheDirectoryPath,
  28. string webDirectoryPath)
  29. {
  30. ProgramDataPath = programDataPath;
  31. LogDirectoryPath = logDirectoryPath;
  32. ConfigurationDirectoryPath = configurationDirectoryPath;
  33. CachePath = cacheDirectoryPath;
  34. WebPath = webDirectoryPath;
  35. DataPath = Directory.CreateDirectory(Path.Combine(ProgramDataPath, "data")).FullName;
  36. }
  37. /// <inheritdoc/>
  38. public string ProgramDataPath { get; }
  39. /// <inheritdoc/>
  40. public string WebPath { get; }
  41. /// <inheritdoc/>
  42. public string ProgramSystemPath { get; } = AppContext.BaseDirectory;
  43. /// <inheritdoc/>
  44. public string DataPath { get; }
  45. /// <inheritdoc />
  46. public string VirtualDataPath => "%AppDataPath%";
  47. /// <inheritdoc/>
  48. public string ImageCachePath => Path.Combine(CachePath, "images");
  49. /// <inheritdoc/>
  50. public string PluginsPath => Path.Combine(ProgramDataPath, "plugins");
  51. /// <inheritdoc/>
  52. public string PluginConfigurationsPath => Path.Combine(PluginsPath, "configurations");
  53. /// <inheritdoc/>
  54. public string LogDirectoryPath { get; }
  55. /// <inheritdoc/>
  56. public string ConfigurationDirectoryPath { get; }
  57. /// <inheritdoc/>
  58. public string SystemConfigurationFilePath => Path.Combine(ConfigurationDirectoryPath, "system.xml");
  59. /// <inheritdoc/>
  60. public string CachePath { get; set; }
  61. /// <inheritdoc/>
  62. public string TempDirectory => Path.Join(Path.GetTempPath(), "jellyfin");
  63. /// <inheritdoc />
  64. public string TrickplayPath => Path.Combine(DataPath, "trickplay");
  65. /// <inheritdoc />
  66. public string BackupPath => Path.Combine(DataPath, "backups");
  67. /// <inheritdoc />
  68. public virtual void MakeSanityCheckOrThrow()
  69. {
  70. CreateAndCheckMarker(ConfigurationDirectoryPath, "config");
  71. CreateAndCheckMarker(LogDirectoryPath, "log");
  72. CreateAndCheckMarker(PluginsPath, "plugin");
  73. CreateAndCheckMarker(ProgramDataPath, "data");
  74. CreateAndCheckMarker(CachePath, "cache");
  75. CreateAndCheckMarker(DataPath, "data");
  76. }
  77. /// <inheritdoc />
  78. public void CreateAndCheckMarker(string path, string markerName, bool recursive = false)
  79. {
  80. Directory.CreateDirectory(path);
  81. CheckOrCreateMarker(path, $".jellyfin-{markerName}", recursive);
  82. }
  83. private IEnumerable<string> GetMarkers(string path, bool recursive = false)
  84. {
  85. return Directory.EnumerateFiles(path, ".jellyfin-*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
  86. }
  87. private void CheckOrCreateMarker(string path, string markerName, bool recursive = false)
  88. {
  89. string? otherMarkers = null;
  90. try
  91. {
  92. otherMarkers = GetMarkers(path, recursive).FirstOrDefault(e => !Path.GetFileName(e.AsSpan()).Equals(markerName, StringComparison.OrdinalIgnoreCase));
  93. }
  94. catch
  95. {
  96. // Error while checking for marker files, assume none exist and keep going
  97. // TODO: add some logging
  98. }
  99. if (otherMarkers is not null)
  100. {
  101. throw new InvalidOperationException($"Expected to find only {markerName} but found marker for {otherMarkers}.");
  102. }
  103. var markerPath = Path.Combine(path, markerName);
  104. if (!File.Exists(markerPath))
  105. {
  106. FileHelper.CreateEmpty(markerPath);
  107. }
  108. }
  109. }
  110. }