BaseApplicationPaths.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Common.Configuration;
  6. namespace Emby.Server.Implementations.AppBase
  7. {
  8. /// <summary>
  9. /// Provides a base class to hold common application paths used by both the UI and Server.
  10. /// This can be subclassed to add application-specific paths.
  11. /// </summary>
  12. public abstract class BaseApplicationPaths : IApplicationPaths
  13. {
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="BaseApplicationPaths"/> class.
  16. /// </summary>
  17. /// <param name="programDataPath">The program data path.</param>
  18. /// <param name="logDirectoryPath">The log directory path.</param>
  19. /// <param name="configurationDirectoryPath">The configuration directory path.</param>
  20. /// <param name="cacheDirectoryPath">The cache directory path.</param>
  21. /// <param name="webDirectoryPath">The web directory path.</param>
  22. protected BaseApplicationPaths(
  23. string programDataPath,
  24. string logDirectoryPath,
  25. string configurationDirectoryPath,
  26. string cacheDirectoryPath,
  27. string webDirectoryPath)
  28. {
  29. ProgramDataPath = programDataPath;
  30. LogDirectoryPath = logDirectoryPath;
  31. ConfigurationDirectoryPath = configurationDirectoryPath;
  32. CachePath = cacheDirectoryPath;
  33. WebPath = webDirectoryPath;
  34. DataPath = Directory.CreateDirectory(Path.Combine(ProgramDataPath, "data")).FullName;
  35. }
  36. /// <inheritdoc/>
  37. public string ProgramDataPath { get; }
  38. /// <inheritdoc/>
  39. public string WebPath { get; }
  40. /// <inheritdoc/>
  41. public string ProgramSystemPath { get; } = AppContext.BaseDirectory;
  42. /// <inheritdoc/>
  43. public string DataPath { get; }
  44. /// <inheritdoc />
  45. public string VirtualDataPath => "%AppDataPath%";
  46. /// <inheritdoc/>
  47. public string ImageCachePath => Path.Combine(CachePath, "images");
  48. /// <inheritdoc/>
  49. public string PluginsPath => Path.Combine(ProgramDataPath, "plugins");
  50. /// <inheritdoc/>
  51. public string PluginConfigurationsPath => Path.Combine(PluginsPath, "configurations");
  52. /// <inheritdoc/>
  53. public string LogDirectoryPath { get; }
  54. /// <inheritdoc/>
  55. public string ConfigurationDirectoryPath { get; }
  56. /// <inheritdoc/>
  57. public string SystemConfigurationFilePath => Path.Combine(ConfigurationDirectoryPath, "system.xml");
  58. /// <inheritdoc/>
  59. public string CachePath { get; set; }
  60. /// <inheritdoc/>
  61. public string TempDirectory => Path.Join(Path.GetTempPath(), "jellyfin");
  62. /// <inheritdoc />
  63. public string TrickplayPath => Path.Combine(DataPath, "trickplay");
  64. /// <inheritdoc />
  65. public virtual void MakeSanityCheckOrThrow()
  66. {
  67. CreateAndCheckMarker(ConfigurationDirectoryPath, "config");
  68. CreateAndCheckMarker(LogDirectoryPath, "log");
  69. CreateAndCheckMarker(PluginsPath, "plugin");
  70. CreateAndCheckMarker(ProgramDataPath, "data");
  71. CreateAndCheckMarker(CachePath, "cache");
  72. CreateAndCheckMarker(DataPath, "data");
  73. }
  74. /// <inheritdoc />
  75. public void CreateAndCheckMarker(string path, string markerName, bool recursive = false)
  76. {
  77. if (!Directory.Exists(path))
  78. {
  79. Directory.CreateDirectory(path);
  80. }
  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. var otherMarkers = GetMarkers(path, recursive).FirstOrDefault(e => Path.GetFileName(e) != markerName);
  90. if (otherMarkers != null)
  91. {
  92. throw new InvalidOperationException($"Exepected to find only {markerName} but found marker for {otherMarkers}.");
  93. }
  94. var markerPath = Path.Combine(path, markerName);
  95. if (!File.Exists(markerPath))
  96. {
  97. File.Create(markerPath).Dispose();
  98. }
  99. }
  100. }
  101. }