Simplify CreateApplicationPaths (#9171)

This commit is contained in:
Bond-009 2023-02-04 18:24:55 +01:00 committed by GitHub
parent d1af317d98
commit 6bf34f8e22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 56 additions and 119 deletions

View File

@ -73,137 +73,55 @@ public static class StartupHelpers
/// <returns><see cref="ServerApplicationPaths" />.</returns> /// <returns><see cref="ServerApplicationPaths" />.</returns>
public static ServerApplicationPaths CreateApplicationPaths(StartupOptions options) public static ServerApplicationPaths CreateApplicationPaths(StartupOptions options)
{ {
// dataDir // LocalApplicationData
// IF --datadir // Windows: %LocalAppData%
// ELSE IF $JELLYFIN_DATA_DIR // macOS: NSApplicationSupportDirectory
// ELSE IF windows, use <%APPDATA%>/jellyfin // UNIX: $XDG_DATA_HOME
// ELSE IF $XDG_DATA_HOME then use $XDG_DATA_HOME/jellyfin var dataDir = options.DataDir
// ELSE use $HOME/.local/share/jellyfin ?? Environment.GetEnvironmentVariable("JELLYFIN_DATA_DIR")
var dataDir = options.DataDir; ?? Path.Join(
if (string.IsNullOrEmpty(dataDir)) Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
{ "jellyfin");
dataDir = Environment.GetEnvironmentVariable("JELLYFIN_DATA_DIR");
if (string.IsNullOrEmpty(dataDir)) var configDir = options.ConfigDir ?? Environment.GetEnvironmentVariable("JELLYFIN_CONFIG_DIR");
if (configDir is null)
{
configDir = Path.Join(dataDir, "config");
if (options.DataDir is null
&& !Directory.Exists(configDir)
&& !OperatingSystem.IsWindows()
&& !OperatingSystem.IsMacOS())
{ {
// LocalApplicationData follows the XDG spec on unix machines // UNIX: $XDG_CONFIG_HOME
dataDir = Path.Combine( configDir = Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"jellyfin"); "jellyfin");
} }
} }
// configDir var cacheDir = options.CacheDir ?? Environment.GetEnvironmentVariable("JELLYFIN_CACHE_DIR");
// IF --configdir if (cacheDir is null)
// ELSE IF $JELLYFIN_CONFIG_DIR
// ELSE IF --datadir, use <datadir>/config (assume portable run)
// ELSE IF <datadir>/config exists, use that
// ELSE IF windows, use <datadir>/config
// ELSE IF $XDG_CONFIG_HOME use $XDG_CONFIG_HOME/jellyfin
// ELSE $HOME/.config/jellyfin
var configDir = options.ConfigDir;
if (string.IsNullOrEmpty(configDir))
{ {
configDir = Environment.GetEnvironmentVariable("JELLYFIN_CONFIG_DIR"); if (OperatingSystem.IsWindows() || OperatingSystem.IsMacOS())
if (string.IsNullOrEmpty(configDir))
{ {
if (options.DataDir is not null cacheDir = Path.Join(dataDir, "cache");
|| Directory.Exists(Path.Combine(dataDir, "config")) }
|| OperatingSystem.IsWindows()) else
{ {
// Hang config folder off already set dataDir cacheDir = Path.Join(GetXdgCacheHome(), "jellyfin");
configDir = Path.Combine(dataDir, "config");
}
else
{
// $XDG_CONFIG_HOME defines the base directory relative to which
// user specific configuration files should be stored.
configDir = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME");
// If $XDG_CONFIG_HOME is either not set or empty,
// a default equal to $HOME /.config should be used.
if (string.IsNullOrEmpty(configDir))
{
configDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".config");
}
configDir = Path.Combine(configDir, "jellyfin");
}
} }
} }
// cacheDir var webDir = options.WebDir ?? Environment.GetEnvironmentVariable("JELLYFIN_WEB_DIR");
// IF --cachedir if (webDir is null)
// ELSE IF $JELLYFIN_CACHE_DIR
// ELSE IF windows, use <datadir>/cache
// ELSE IF XDG_CACHE_HOME, use $XDG_CACHE_HOME/jellyfin
// ELSE HOME/.cache/jellyfin
var cacheDir = options.CacheDir;
if (string.IsNullOrEmpty(cacheDir))
{ {
cacheDir = Environment.GetEnvironmentVariable("JELLYFIN_CACHE_DIR"); webDir = Path.Join(AppContext.BaseDirectory, "jellyfin-web");
if (string.IsNullOrEmpty(cacheDir))
{
if (OperatingSystem.IsWindows())
{
// Hang cache folder off already set dataDir
cacheDir = Path.Combine(dataDir, "cache");
}
else
{
// $XDG_CACHE_HOME defines the base directory relative to which
// user specific non-essential data files should be stored.
cacheDir = Environment.GetEnvironmentVariable("XDG_CACHE_HOME");
// If $XDG_CACHE_HOME is either not set or empty,
// a default equal to $HOME/.cache should be used.
if (string.IsNullOrEmpty(cacheDir))
{
cacheDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".cache");
}
cacheDir = Path.Combine(cacheDir, "jellyfin");
}
}
} }
// webDir var logDir = options.LogDir ?? Environment.GetEnvironmentVariable("JELLYFIN_LOG_DIR");
// IF --webdir if (logDir is null)
// ELSE IF $JELLYFIN_WEB_DIR
// ELSE <bindir>/jellyfin-web
var webDir = options.WebDir;
if (string.IsNullOrEmpty(webDir))
{ {
webDir = Environment.GetEnvironmentVariable("JELLYFIN_WEB_DIR"); logDir = Path.Join(dataDir, "log");
if (string.IsNullOrEmpty(webDir))
{
// Use default location under ResourcesPath
webDir = Path.Combine(AppContext.BaseDirectory, "jellyfin-web");
}
}
// logDir
// IF --logdir
// ELSE IF $JELLYFIN_LOG_DIR
// ELSE IF --datadir, use <datadir>/log (assume portable run)
// ELSE <datadir>/log
var logDir = options.LogDir;
if (string.IsNullOrEmpty(logDir))
{
logDir = Environment.GetEnvironmentVariable("JELLYFIN_LOG_DIR");
if (string.IsNullOrEmpty(logDir))
{
// Hang log folder off already set dataDir
logDir = Path.Combine(dataDir, "log");
}
} }
// Normalize paths. Only possible with GetFullPath for now - https://github.com/dotnet/runtime/issues/2162 // Normalize paths. Only possible with GetFullPath for now - https://github.com/dotnet/runtime/issues/2162
@ -231,6 +149,24 @@ public static class StartupHelpers
return new ServerApplicationPaths(dataDir, logDir, configDir, cacheDir, webDir); return new ServerApplicationPaths(dataDir, logDir, configDir, cacheDir, webDir);
} }
private static string GetXdgCacheHome()
{
// $XDG_CACHE_HOME defines the base directory relative to which
// user specific non-essential data files should be stored.
var cacheHome = Environment.GetEnvironmentVariable("XDG_CACHE_HOME");
// If $XDG_CACHE_HOME is either not set or a relative path,
// a default equal to $HOME/.cache should be used.
if (cacheHome is null || !cacheHome.StartsWith('/'))
{
cacheHome = Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".cache");
}
return cacheHome;
}
/// <summary> /// <summary>
/// Gets the path for the unix socket Kestrel should bind to. /// Gets the path for the unix socket Kestrel should bind to.
/// </summary> /// </summary>
@ -243,16 +179,17 @@ public static class StartupHelpers
if (string.IsNullOrEmpty(socketPath)) if (string.IsNullOrEmpty(socketPath))
{ {
const string SocketFile = "jellyfin.sock";
var xdgRuntimeDir = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR"); var xdgRuntimeDir = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR");
var socketFile = "jellyfin.sock";
if (xdgRuntimeDir is null) if (xdgRuntimeDir is null)
{ {
// Fall back to config dir // Fall back to config dir
socketPath = Path.Join(appPaths.ConfigurationDirectoryPath, socketFile); socketPath = Path.Join(appPaths.ConfigurationDirectoryPath, SocketFile);
} }
else else
{ {
socketPath = Path.Join(xdgRuntimeDir, socketFile); socketPath = Path.Join(xdgRuntimeDir, SocketFile);
} }
} }