Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Shoko.IntegrationTests/DatabaseMigrationFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public DatabaseMigrationFixture()

// SystemService() bootstraps ISettingsProvider.Instance with default settings (FirstRun=true).
// No settings file yet — defaults are valid and pass schema validation.
var systemService = new SystemService();
var systemService = new SystemService(["--home", _tempDir.Replace('\\', '/')]);

// Mutate the live settings: disable first-run, inject fake AniDB credentials so the
// settings custom-validator is satisfied, and move the web port away from 8111 so this
Expand Down
2 changes: 2 additions & 0 deletions Shoko.Server/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand All @@ -11,6 +12,7 @@
// COM, set the ComVisible attribute to true on that type.

[assembly: ComVisible(false)]
[assembly: InternalsVisibleTo("Shoko.Tests")]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
Expand Down
43 changes: 39 additions & 4 deletions Shoko.Server/Services/ApplicationPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using Shoko.Abstractions.Plugin;
using Shoko.Abstractions.Utilities;
using Shoko.Server.Settings;
Expand All @@ -23,10 +24,13 @@
=> _applicationPath ??= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;

private string? _webPath = null;
private static readonly AsyncLocal<string?> s_dataPathOverride = new();

/// <inheritdoc/>
public string WebPath
=> _webPath ??= Path.Combine(DataPath, ISettingsProvider.Instance.GetSettings().Web.WebUIPath);
=> s_dataPathOverride.Value is { Length: > 0 }
? Path.Combine(DataPath, ISettingsProvider.Instance.GetSettings().Web.WebUIPath)
: _webPath ??= Path.Combine(DataPath, ISettingsProvider.Instance.GetSettings().Web.WebUIPath);

private static string? _dataPath = null;

Expand All @@ -37,6 +41,9 @@
{
get
{
if (s_dataPathOverride.Value is { Length: > 0 } overriddenPath)
return overriddenPath;

if (_dataPath != null)
return _dataPath;

Expand Down Expand Up @@ -94,9 +101,13 @@

/// <inheritdoc/>
public string ImagesPath
=> _imagesPath ??= ISettingsProvider.Instance.GetSettings().ImagesPath is { Length: > 0 } imagePath
? Path.Combine(DataPath, imagePath)
: DefaultImagePath;
=> s_dataPathOverride.Value is { Length: > 0 }
? ISettingsProvider.Instance.GetSettings().ImagesPath is { Length: > 0 } configuredImagePath
? Path.Combine(DataPath, configuredImagePath)
: DefaultImagePath

Check warning on line 107 in Shoko.Server/Services/ApplicationPaths.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=ShokoAnime_ShokoServer&issues=AZ5RT0wNavhadsG2BU68&open=AZ5RT0wNavhadsG2BU68&pullRequest=1327
: _imagesPath ??= ISettingsProvider.Instance.GetSettings().ImagesPath is { Length: > 0 } cachedConfiguredImagePath
? Path.Combine(DataPath, cachedConfiguredImagePath)
: DefaultImagePath;

Check warning on line 110 in Shoko.Server/Services/ApplicationPaths.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=ShokoAnime_ShokoServer&issues=AZ5RT0wNavhadsG2BU69&open=AZ5RT0wNavhadsG2BU69&pullRequest=1327

public static string DefaultImagePath => Path.Combine(StaticDataPath, "images");

Expand All @@ -114,4 +125,28 @@
/// <inheritdoc/>
public string LogsPath
=> Path.Combine(DataPath, "logs");

internal static IDisposable PushDataPathOverride(string absolutePath)
=> new ScopedDataPathOverride(absolutePath);

private sealed class ScopedDataPathOverride : IDisposable
{
private readonly string? _previousValue;
private bool _disposed;

public ScopedDataPathOverride(string value)
{
_previousValue = s_dataPathOverride.Value;
s_dataPathOverride.Value = value;
}

public void Dispose()
{
if (_disposed)
return;

s_dataPathOverride.Value = _previousValue;
_disposed = true;
}
}
}
Loading
Loading