Skip to content

Commit

Permalink
chore: more DRY code
Browse files Browse the repository at this point in the history
- Consolidate code paths to only generate the list of all possible VFS roots in one place, then re-use the generated list elsewhere.
  • Loading branch information
revam committed Oct 4, 2024
1 parent 6220224 commit 2c2ec46
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
7 changes: 1 addition & 6 deletions Shokofin/Configuration/MediaFolderConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,9 @@ private async Task GenerateAllConfigurations(List<VirtualFolderInfo> allVirtualF
edits.add.Add(vfsPath);
}

var virtualRoots = new string[] {
Plugin.Instance.VirtualRoot_Default,
Plugin.Instance.VirtualRoot_Cache,
Plugin.Instance.VirtualRoot_Custom ?? string.Empty,
}.Where(s => !string.IsNullOrEmpty(s)).ToArray();
var toRemove = virtualFolder.Locations
.Except(shouldAttach ? [vfsPath] : [])
.Where(location => virtualRoots.Any(virtualRoot => location.StartsWith(virtualRoot, Path.DirectorySeparatorChar is '\\' ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)))
.Where(location => Plugin.Instance.AllVirtualRoots.Any(virtualRoot => location.StartsWith(virtualRoot, Path.DirectorySeparatorChar is '\\' ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)))
.ToList();
if (toRemove.Count > 0) {
if (!LibraryEdits.TryGetValue(libraryId, out var edits))
Expand Down
23 changes: 19 additions & 4 deletions Shokofin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,25 @@ public string VirtualRoot
}
}

public string VirtualRoot_Default => Path.Join(ApplicationPaths.ProgramDataPath, Name);

public string VirtualRoot_Cache => Path.Join(ApplicationPaths.CachePath, Name);
private string[]? _allVirtualRoots;

public string? VirtualRoot_Custom => string.IsNullOrWhiteSpace(Configuration.VFS_CustomLocation) ? null : Path.Combine(ApplicationPaths.ProgramDataPath, Configuration.VFS_CustomLocation);
/// <summary>
/// All "Virtual" File System Root Directories.
/// </summary>
public string[] AllVirtualRoots => _allVirtualRoots ??= (new string[] {
VirtualRoot_Default,
VirtualRoot_Cache,
VirtualRoot_Custom ?? string.Empty
})
.Except([string.Empty])
.Distinct()
.ToArray();

private string VirtualRoot_Default => Path.Join(ApplicationPaths.ProgramDataPath, Name);

private string VirtualRoot_Cache => Path.Join(ApplicationPaths.CachePath, Name);

private string? VirtualRoot_Custom => string.IsNullOrWhiteSpace(Configuration.VFS_CustomLocation) ? null : Path.Combine(ApplicationPaths.ProgramDataPath, Configuration.VFS_CustomLocation);

/// <summary>
/// Gets or sets the event handler that is triggered when this configuration changes.
Expand Down Expand Up @@ -218,6 +232,7 @@ public void OnConfigChanged(object? sender, BasePluginConfiguration e)

// Reset the cached VFS root directory in case it has changed.
_virtualRoot = null;
_allVirtualRoots = null;

ConfigurationChanged?.Invoke(sender, config);
}
Expand Down
7 changes: 1 addition & 6 deletions Shokofin/Tasks/CleanupVirtualRootTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellat
return Task.CompletedTask;

var start = DateTime.Now;
var virtualRoots = new string[] {
Plugin.Instance.VirtualRoot_Default,
Plugin.Instance.VirtualRoot_Cache,
Plugin.Instance.VirtualRoot_Custom ?? string.Empty,
}
.Except([Plugin.Instance.VirtualRoot, string.Empty])
var virtualRoots = Plugin.Instance.AllVirtualRoots
.Where(Directory.Exists)
.ToList();
Logger.LogDebug("Found {RemoveCount} VFS roots to remove.", virtualRoots.Count);
Expand Down

0 comments on commit 2c2ec46

Please sign in to comment.