BaseApplicationPaths.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 virtual void MakeSanityCheckOrThrow()
  67. {
  68. CreateAndCheckMarker(ConfigurationDirectoryPath, "config");
  69. CreateAndCheckMarker(LogDirectoryPath, "log");
  70. CreateAndCheckMarker(PluginsPath, "plugin");
  71. CreateAndCheckMarker(ProgramDataPath, "data");
  72. CreateAndCheckMarker(CachePath, "cache");
  73. CreateAndCheckMarker(DataPath, "data");
  74. }
  75. /// <inheritdoc />
  76. public void CreateAndCheckMarker(string path, string markerName, bool recursive = false)
  77. {
  78. Directory.CreateDirectory(path);
  79. CheckOrCreateMarker(path, $".jellyfin-{markerName}", recursive);
  80. }
  81. private IEnumerable<string> GetMarkers(string path, bool recursive = false)
  82. {
  83. return Directory.EnumerateFiles(path, ".jellyfin-*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
  84. }
  85. private void CheckOrCreateMarker(string path, string markerName, bool recursive = false)
  86. {
  87. var otherMarkers = GetMarkers(path, recursive).FirstOrDefault(e => Path.GetFileName(e) != markerName);
  88. if (otherMarkers != null)
  89. {
  90. throw new InvalidOperationException($"Exepected to find only {markerName} but found marker for {otherMarkers}.");
  91. }
  92. var markerPath = Path.Combine(path, markerName);
  93. if (!File.Exists(markerPath))
  94. {
  95. FileHelper.CreateEmpty(markerPath);
  96. }
  97. }
  98. }
  99. }