Files
jellyfin-jellyfin-1/Emby.Server.Implementations/AppBase/BaseApplicationPaths.cs

124 lines
4.5 KiB
C#
Raw Normal View History

using System;
2025-04-26 18:30:25 +03:00
using System.Collections.Generic;
using System.IO;
2025-04-26 18:30:25 +03:00
using System.Linq;
2025-05-04 16:40:34 +02:00
using Jellyfin.Extensions;
2016-10-29 01:40:15 -04:00
using MediaBrowser.Common.Configuration;
2017-02-20 15:50:58 -05:00
namespace Emby.Server.Implementations.AppBase
2016-10-29 01:40:15 -04:00
{
/// <summary>
2020-03-15 12:59:34 +01:00
/// Provides a base class to hold common application paths used by both the UI and Server.
2016-10-29 01:40:15 -04:00
/// This can be subclassed to add application-specific paths.
/// </summary>
public abstract class BaseApplicationPaths : IApplicationPaths
{
/// <summary>
/// Initializes a new instance of the <see cref="BaseApplicationPaths"/> class.
/// </summary>
/// <param name="programDataPath">The program data path.</param>
/// <param name="logDirectoryPath">The log directory path.</param>
/// <param name="configurationDirectoryPath">The configuration directory path.</param>
/// <param name="cacheDirectoryPath">The cache directory path.</param>
/// <param name="webDirectoryPath">The web directory path.</param>
2019-01-05 19:12:05 +01:00
protected BaseApplicationPaths(
string programDataPath,
string logDirectoryPath,
string configurationDirectoryPath,
string cacheDirectoryPath,
string webDirectoryPath)
2016-10-29 01:40:15 -04:00
{
ProgramDataPath = programDataPath;
2019-01-01 21:34:12 +01:00
LogDirectoryPath = logDirectoryPath;
2019-01-05 19:12:05 +01:00
ConfigurationDirectoryPath = configurationDirectoryPath;
2019-01-28 17:52:56 +01:00
CachePath = cacheDirectoryPath;
WebPath = webDirectoryPath;
2023-10-08 00:40:58 +02:00
DataPath = Directory.CreateDirectory(Path.Combine(ProgramDataPath, "data")).FullName;
2016-10-29 01:40:15 -04:00
}
/// <inheritdoc/>
2019-08-09 23:50:40 +02:00
public string ProgramDataPath { get; }
2016-10-29 01:40:15 -04:00
/// <inheritdoc/>
2019-08-09 23:50:40 +02:00
public string WebPath { get; }
/// <inheritdoc/>
public string ProgramSystemPath { get; } = AppContext.BaseDirectory;
2016-10-29 01:40:15 -04:00
/// <inheritdoc/>
2023-10-08 00:40:58 +02:00
public string DataPath { get; }
2016-10-29 01:40:15 -04:00
/// <inheritdoc />
2020-10-17 16:01:36 +02:00
public string VirtualDataPath => "%AppDataPath%";
2018-09-12 19:26:21 +02:00
/// <inheritdoc/>
public string ImageCachePath => Path.Combine(CachePath, "images");
2016-10-29 01:40:15 -04:00
/// <inheritdoc/>
public string PluginsPath => Path.Combine(ProgramDataPath, "plugins");
2016-10-29 01:40:15 -04:00
/// <inheritdoc/>
public string PluginConfigurationsPath => Path.Combine(PluginsPath, "configurations");
2016-10-29 01:40:15 -04:00
/// <inheritdoc/>
2019-08-09 23:50:40 +02:00
public string LogDirectoryPath { get; }
2019-01-05 19:12:05 +01:00
/// <inheritdoc/>
2019-08-09 23:50:40 +02:00
public string ConfigurationDirectoryPath { get; }
2016-10-29 01:40:15 -04:00
/// <inheritdoc/>
public string SystemConfigurationFilePath => Path.Combine(ConfigurationDirectoryPath, "system.xml");
2016-10-29 01:40:15 -04:00
/// <inheritdoc/>
public string CachePath { get; set; }
2016-10-29 01:40:15 -04:00
/// <inheritdoc/>
public string TempDirectory => Path.Join(Path.GetTempPath(), "jellyfin");
/// <inheritdoc />
public string TrickplayPath => Path.Combine(DataPath, "trickplay");
2025-04-26 18:30:25 +03:00
/// <inheritdoc />
public string BackupPath => Path.Combine(DataPath, "backups");
2025-04-26 18:30:25 +03:00
/// <inheritdoc />
public virtual void MakeSanityCheckOrThrow()
{
CreateAndCheckMarker(ConfigurationDirectoryPath, "config");
CreateAndCheckMarker(LogDirectoryPath, "log");
CreateAndCheckMarker(PluginsPath, "plugin");
CreateAndCheckMarker(ProgramDataPath, "data");
CreateAndCheckMarker(CachePath, "cache");
CreateAndCheckMarker(DataPath, "data");
}
/// <inheritdoc />
public void CreateAndCheckMarker(string path, string markerName, bool recursive = false)
{
2025-05-04 16:40:34 +02:00
Directory.CreateDirectory(path);
2025-04-26 18:30:25 +03:00
CheckOrCreateMarker(path, $".jellyfin-{markerName}", recursive);
}
private IEnumerable<string> GetMarkers(string path, bool recursive = false)
{
return Directory.EnumerateFiles(path, ".jellyfin-*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
}
private void CheckOrCreateMarker(string path, string markerName, bool recursive = false)
{
var otherMarkers = GetMarkers(path, recursive).FirstOrDefault(e => Path.GetFileName(e) != markerName);
if (otherMarkers != null)
{
throw new InvalidOperationException($"Exepected to find only {markerName} but found marker for {otherMarkers}.");
}
var markerPath = Path.Combine(path, markerName);
if (!File.Exists(markerPath))
{
2025-05-04 16:40:34 +02:00
FileHelper.CreateEmpty(markerPath);
2025-04-26 18:30:25 +03:00
}
}
2016-10-29 01:40:15 -04:00
}
}