aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Shares
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-06-02 10:33:35 -0500
committerGitHub <noreply@github.com>2022-06-02 10:33:35 -0500
commite044a75f285b1b1bfb19631ea2eef5509714a703 (patch)
treea9bdec768dbd36d831fb655aeefd5754e96d8e87 /src/slskd/Shares
parentadf5e7800452e861903b00faa5729c92187ef3ce (diff)
parent908972ae3655973cb2d1536e12b7c2dd42437062 (diff)
downloadslskd-e044a75f285b1b1bfb19631ea2eef5509714a703.tar.gz
Merge branch 'master' into dependabot/npm_and_yarn/src/web/eventsource-1.1.1
Diffstat (limited to 'src/slskd/Shares')
-rw-r--r--src/slskd/Shares/API/Controllers/SharesController.cs195
-rw-r--r--src/slskd/Shares/API/DTO/SummarizedShare.cs32
-rw-r--r--src/slskd/Shares/Domain/Share.cs26
-rw-r--r--src/slskd/Shares/IShareService.cs82
-rw-r--r--src/slskd/Shares/ShareService.cs183
-rw-r--r--src/slskd/Shares/SharedFileCache.cs53
-rw-r--r--src/slskd/Shares/SoulseekFileFactory.cs2
7 files changed, 549 insertions, 24 deletions
diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs
new file mode 100644
index 00000000..41178bac
--- /dev/null
+++ b/src/slskd/Shares/API/Controllers/SharesController.cs
@@ -0,0 +1,195 @@
+// <copyright file="SharesController.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Shares.API
+{
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Threading.Tasks;
+ using Microsoft.AspNetCore.Authorization;
+ using Microsoft.AspNetCore.Mvc;
+ using Soulseek;
+
+ /// <summary>
+ /// Shares.
+ /// </summary>
+ [ApiController]
+ [ApiVersion("0")]
+ [Produces("application/json")]
+ [Consumes("application/json")]
+ [Route("api/v{version:apiVersion}/[controller]")]
+ [Authorize]
+ public class SharesController : ControllerBase
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="SharesController"/> class.
+ /// </summary>
+ /// <param name="shareService"></param>
+ public SharesController(
+ IShareService shareService)
+ {
+ Shares = shareService;
+ }
+
+ private IShareService Shares { get; }
+
+ /// <summary>
+ /// Gets the current list of shares.
+ /// </summary>
+ /// <response code="200">The request completed successfully.</response>
+ /// <returns></returns>
+ [HttpGet("")]
+ [Authorize]
+ [ProducesResponseType(typeof(IEnumerable<SummarizedShare>), 200)]
+ public async Task<IActionResult> List()
+ {
+ var browse = await Shares.BrowseAsync();
+
+ var summary = Shares.CachedShares.Select(share =>
+ {
+ var directories = browse.Where(directory => directory.Name.StartsWith(share.RemotePath));
+ var fileCount = directories.Aggregate(seed: 0, (sum, directory) =>
+ {
+ sum += directory.FileCount;
+ return sum;
+ });
+
+ return new SummarizedShare()
+ {
+ Id = share.Id,
+ Alias = share.Alias,
+ IsExcluded = share.IsExcluded,
+ LocalPath = share.LocalPath,
+ Mask = share.Mask,
+ Raw = share.Raw,
+ RemotePath = share.RemotePath,
+ Directories = directories.Count(),
+ Files = fileCount,
+ };
+ });
+
+ return Ok(summary);
+ }
+
+ /// <summary>
+ /// Gets the share associated with the specified <see paramref="id"/>.
+ /// </summary>
+ /// <param name="id">The id of the share.</param>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="404">The requested share could not be found.</response>
+ /// <returns></returns>
+ [HttpGet("{id}")]
+ [Authorize]
+ [ProducesResponseType(typeof(SummarizedShare), 200)]
+ [ProducesResponseType(404)]
+ public async Task<IActionResult> Get(string id)
+ {
+ var share = Shares.Shares.FirstOrDefault(share => share.Id == id);
+
+ if (share == default)
+ {
+ return NotFound();
+ }
+
+ var browse = await Shares.BrowseAsync();
+ var directories = browse.Where(directory => directory.Name.StartsWith(share.RemotePath));
+ var fileCount = directories.Aggregate(seed: 0, (sum, directory) =>
+ {
+ sum += directory.FileCount;
+ return sum;
+ });
+
+ var summary = new SummarizedShare()
+ {
+ Id = share.Id,
+ Alias = share.Alias,
+ IsExcluded = share.IsExcluded,
+ LocalPath = share.LocalPath,
+ Mask = share.Mask,
+ Raw = share.Raw,
+ RemotePath = share.RemotePath,
+ Directories = directories.Count(),
+ Files = fileCount,
+ };
+
+ return Ok(summary);
+ }
+
+ /// <summary>
+ /// Returns a list of all shared directories and files.
+ /// </summary>
+ /// <returns></returns>
+ /// <response code="200">The request completed successfully.</response>
+ [HttpGet("contents")]
+ [Authorize]
+ [ProducesResponseType(typeof(IEnumerable<Directory>), 200)]
+ public async Task<IActionResult> BrowseAll()
+ {
+ return Ok(await Shares.BrowseAsync());
+ }
+
+ /// <summary>
+ /// Gets the contents of the share associated with the specified <see paramref="id"/>.
+ /// </summary>
+ /// <param name="id">The id of the share.</param>
+ /// <returns></returns>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="404">The requested share could not be found.</response>
+ [HttpGet("{id}/contents")]
+ [ProducesResponseType(typeof(IEnumerable<Directory>), 200)]
+ [ProducesResponseType(404)]
+ public async Task<IActionResult> BrowseShare(string id)
+ {
+ var share = Shares.Shares.FirstOrDefault(share => share.Id == id);
+
+ if (share == default)
+ {
+ return NotFound();
+ }
+
+ var contents = (await Shares.BrowseAsync())
+ .Where(directory => directory.Name.StartsWith(share.RemotePath));
+
+ return Ok(contents);
+ }
+
+ /// <summary>
+ /// Initiates a scan of the configured shares.
+ /// </summary>
+ /// <returns></returns>
+ /// <response code="204">The request completed successfully.</response>
+ /// <response code="409">A share scan is already in progress.</response>
+ [HttpPut]
+ [Route("")]
+ [Authorize]
+ [ProducesResponseType(204)]
+ [ProducesResponseType(409)]
+ public async Task<IActionResult> RescanSharesAsync()
+ {
+ try
+ {
+ await Shares.StartScanAsync();
+ }
+ catch (ShareScanInProgressException)
+ {
+ return Conflict("A share scan is already in progress.");
+ }
+
+ return Ok();
+ }
+ }
+}
diff --git a/src/slskd/Shares/API/DTO/SummarizedShare.cs b/src/slskd/Shares/API/DTO/SummarizedShare.cs
new file mode 100644
index 00000000..a1d6ed7b
--- /dev/null
+++ b/src/slskd/Shares/API/DTO/SummarizedShare.cs
@@ -0,0 +1,32 @@
+// <copyright file="SummarizedShare.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Shares
+{
+ public record SummarizedShare
+ {
+ public string Id { get; init; }
+ public string Alias { get; init; }
+ public bool IsExcluded { get; init; }
+ public string LocalPath { get; init; }
+ public string Mask { get; init; }
+ public string Raw { get; init; }
+ public string RemotePath { get; init; }
+ public int Directories { get; init; }
+ public int Files { get; init; }
+ }
+} \ No newline at end of file
diff --git a/src/slskd/Shares/Domain/Share.cs b/src/slskd/Shares/Domain/Share.cs
index 890c8565..9bdbc002 100644
--- a/src/slskd/Shares/Domain/Share.cs
+++ b/src/slskd/Shares/Domain/Share.cs
@@ -18,6 +18,7 @@
namespace slskd.Shares
{
using System.Linq;
+ using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
/// <summary>
@@ -28,6 +29,28 @@ namespace slskd.Shares
/// <summary>
/// Initializes a new instance of the <see cref="Share"/> class.
/// </summary>
+ /// <param name="id"></param>
+ /// <param name="alias"></param>
+ /// <param name="isExcluded"></param>
+ /// <param name="localPath"></param>
+ /// <param name="mask"></param>
+ /// <param name="raw"></param>
+ /// <param name="remotePath"></param>
+ [JsonConstructor]
+ public Share (string id, string alias, bool isExcluded, string localPath, string mask, string raw, string remotePath)
+ {
+ Id = id;
+ Alias = alias;
+ IsExcluded = isExcluded;
+ LocalPath = localPath;
+ Mask = mask;
+ Raw = raw;
+ RemotePath = remotePath;
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="Share"/> class.
+ /// </summary>
/// <param name="share"></param>
public Share(string share)
{
@@ -63,8 +86,11 @@ namespace slskd.Shares
var aliasedSegment = LocalPath[(parent.Length + 1)..];
RemotePath = maskedPath.ReplaceFirst(aliasedSegment, Alias);
+
+ Id = Compute.Sha1Hash(RemotePath);
}
+ public string Id { get; init; }
public string Alias { get; init; }
public bool IsExcluded { get; init; }
public string LocalPath { get; init; }
diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs
new file mode 100644
index 00000000..fd88f2b0
--- /dev/null
+++ b/src/slskd/Shares/IShareService.cs
@@ -0,0 +1,82 @@
+// <copyright file="IShareService.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Shares
+{
+ using System.Collections.Generic;
+ using System.Threading.Tasks;
+ using Soulseek;
+
+ /// <summary>
+ /// Provides control and interactions with configured shares and shared files.
+ /// </summary>
+ public interface IShareService
+ {
+ /// <summary>
+ /// Gets the list of shares stored in the cache.
+ /// </summary>
+ IReadOnlyList<Share> CachedShares { get; }
+
+ /// <summary>
+ /// Gets the list of configured shares.
+ /// </summary>
+ IReadOnlyList<Share> Shares { get; }
+
+ /// <summary>
+ /// Gets the state monitor for the service.
+ /// </summary>
+ public IStateMonitor<ShareState> StateMonitor { get; }
+
+ /// <summary>
+ /// Returns the entire contents of the share.
+ /// </summary>
+ /// <returns>The entire contents of the share.</returns>
+ Task<IEnumerable<Directory>> BrowseAsync();
+
+ /// <summary>
+ /// Returns the contents of the specified <paramref name="directory"/>.
+ /// </summary>
+ /// <param name="directory">The directory for which the contents are to be listed.</param>
+ /// <returns>The contents of the directory.</returns>
+ Task<Directory> ListDirectoryAsync(string directory);
+
+ /// <summary>
+ /// Resolves the local filename of the specified <paramref name="remoteFilename"/>, if the mask is associated with a
+ /// configured share.
+ /// </summary>
+ /// <param name="remoteFilename">The fully qualified filename to resolve.</param>
+ /// <returns>The resolved local filename.</returns>
+ /// <exception cref="NotFoundException">
+ /// Thrown when the specified remote filename can not be associated with a configured share.
+ /// </exception>
+ Task<string> ResolveFilenameAsync(string remoteFilename);
+
+ /// <summary>
+ /// Searches the cache for the specified <paramref name="query"/> and returns the matching files.
+ /// </summary>
+ /// <param name="query">The query for which to search.</param>
+ /// <returns>The matching files.</returns>
+ Task<IEnumerable<File>> SearchAsync(SearchQuery query);
+
+ /// <summary>
+ /// Starts a scan of the configured shares.
+ /// </summary>
+ /// <returns>The operation context.</returns>
+ /// <exception cref="ShareScanInProgressException">Thrown when a scan is already in progress.</exception>
+ Task StartScanAsync();
+ }
+}
diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs
new file mode 100644
index 00000000..ead96d10
--- /dev/null
+++ b/src/slskd/Shares/ShareService.cs
@@ -0,0 +1,183 @@
+// <copyright file="ShareService.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+using Microsoft.Extensions.Options;
+
+namespace slskd.Shares
+{
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text.RegularExpressions;
+ using System.Threading;
+ using System.Threading.Tasks;
+ using Soulseek;
+
+ /// <summary>
+ /// Provides control and interactions with configured shares and shared files.
+ /// </summary>
+ public class ShareService : IShareService
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ShareService"/> class.
+ /// </summary>
+ /// <param name="optionsMonitor"></param>
+ /// <param name="sharedFileCache"></param>
+ public ShareService(
+ IOptionsMonitor<Options> optionsMonitor,
+ ISharedFileCache sharedFileCache = null)
+ {
+ Cache = sharedFileCache ?? new SharedFileCache();
+ Cache.StateMonitor.OnChange(cacheState =>
+ {
+ var (previous, current) = cacheState;
+
+ State.SetValue(state => state with
+ {
+ // scan is pending if faulted, or if state DIDN'T just transition from filling to not filling AND a scan was already pending
+ ScanPending = current.Faulted || (!(previous.Filling && !current.Filling) && state.ScanPending),
+ Scanning = current.Filling,
+ Faulted = current.Faulted,
+ ScanProgress = current.FillProgress,
+ Directories = current.Directories,
+ Files = current.Files,
+ });
+
+ if (previous.Filling && !current.Filling)
+ {
+ CachedShares = current.Shares.ToList().AsReadOnly();
+ }
+ });
+
+ OptionsMonitor = optionsMonitor;
+ OptionsMonitor.OnChange(options => Configure(options));
+
+ Configure(OptionsMonitor.CurrentValue);
+ }
+
+ /// <summary>
+ /// Gets the list of configured shares.
+ /// </summary>
+ public IReadOnlyList<Share> Shares => SharesList.AsReadOnly();
+
+ /// <summary>
+ /// Gets the list of shares stored in the cache.
+ /// </summary>
+ public IReadOnlyList<Share> CachedShares { get; private set; } = new List<Share>().AsReadOnly();
+
+ /// <summary>
+ /// Gets the state monitor for the service.
+ /// </summary>
+ public IStateMonitor<ShareState> StateMonitor => State;
+
+ private ISharedFileCache Cache { get; }
+ private string LastOptionsHash { get; set; }
+ private IOptionsMonitor<Options> OptionsMonitor { get; }
+ private List<Share> SharesList { get; set; } = new List<Share>();
+ private IManagedState<ShareState> State { get; } = new ManagedState<ShareState>();
+ private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1);
+ private List<Regex> FilterRegexes { get; set; } = new List<Regex>();
+
+ /// <summary>
+ /// Returns the entire contents of the share.
+ /// </summary>
+ /// <returns>The entire contents of the share.</returns>
+ public Task<IEnumerable<Directory>> BrowseAsync()
+ => Task.FromResult(Cache.Browse());
+
+ /// <summary>
+ /// Returns the contents of the specified <paramref name="directory"/>.
+ /// </summary>
+ /// <param name="directory">The directory for which the contents are to be listed.</param>
+ /// <returns>The contents of the directory.</returns>
+ public Task<Directory> ListDirectoryAsync(string directory)
+ => Task.FromResult(Cache.List(directory));
+
+ /// <summary>
+ /// Resolves the local filename of the specified <paramref name="remoteFilename"/>, if the mask is associated with a
+ /// configured share.
+ /// </summary>
+ /// <param name="remoteFilename">The fully qualified filename to resolve.</param>
+ /// <returns>The resolved local filename.</returns>
+ /// <exception cref="NotFoundException">
+ /// Thrown when the specified remote filename can not be associated with a configured share.
+ /// </exception>
+ public Task<string> ResolveFilenameAsync(string remoteFilename)
+ {
+ var resolvedFilename = Cache.Resolve(remoteFilename);
+
+ if (resolvedFilename == remoteFilename)
+ {
+ throw new NotFoundException($"The requested filename '{remoteFilename}' could not be resolved to a configured share.");
+ }
+
+ return Task.FromResult(resolvedFilename);
+ }
+
+ /// <summary>
+ /// Searches the cache for the specified <paramref name="query"/> and returns the matching files.
+ /// </summary>
+ /// <param name="query">The query for which to search.</param>
+ /// <returns>The matching files.</returns>
+ public Task<IEnumerable<File>> SearchAsync(SearchQuery query)
+ => Cache.SearchAsync(query);
+
+ /// <summary>
+ /// Starts a scan of the configured shares.
+ /// </summary>
+ /// <returns>The operation context.</returns>
+ /// <exception cref="ShareScanInProgressException">Thrown when a scan is already in progress.</exception>
+ public Task StartScanAsync()
+ => Cache.FillAsync(Shares, FilterRegexes);
+
+ private void Configure(Options options)
+ {
+ SyncRoot.Wait();
+
+ try
+ {
+ var optionsHash = Compute.Sha1Hash(string.Join(';', options.Directories.Shared));
+
+ if (optionsHash == LastOptionsHash)
+ {
+ return;
+ }
+
+ var shares = options.Directories.Shared
+ .Select(share => share.TrimEnd('/', '\\'))
+ .ToHashSet() // remove duplicates
+ .Select(share => new Share(share)) // convert to Shares
+ .OrderByDescending(share => share.LocalPath.Length) // process subdirectories first. this allows them to be aliased separately from their parent
+ .ToList();
+
+ var regexes = options.Filters.Share
+ .Select(filter => new Regex(filter, RegexOptions.Compiled))
+ .ToList();
+
+ SharesList = shares;
+ FilterRegexes = regexes;
+
+ State.SetValue(state => state with { ScanPending = true });
+
+ LastOptionsHash = optionsHash;
+ }
+ finally
+ {
+ SyncRoot.Release();
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs
index ac996fa6..6b7a3ca3 100644
--- a/src/slskd/Shares/SharedFileCache.cs
+++ b/src/slskd/Shares/SharedFileCache.cs
@@ -16,7 +16,6 @@
// </copyright>
using System.IO;
-using Microsoft.Extensions.Options;
namespace slskd.Shares
{
@@ -51,8 +50,11 @@ namespace slskd.Shares
/// <summary>
/// Scans the configured shares and fills the cache.
/// </summary>
+ /// <remarks>Initiates the scan, then yields execution back to the caller; does not wait for the operation to complete.</remarks>
+ /// <param name="shares">The list of shares from which to fill the cache.</param>
+ /// <param name="filters">The list of regular expressions used to exclude files or paths from scanning.</param>
/// <returns>The operation context.</returns>
- Task FillAsync();
+ Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters);
/// <summary>
/// Returns the contents of the specified <paramref name="directory"/>.
@@ -85,13 +87,9 @@ namespace slskd.Shares
/// <summary>
/// Initializes a new instance of the <see cref="SharedFileCache"/> class.
/// </summary>
- /// <param name="optionsMonitor"></param>
/// <param name="soulseekFileFactory"></param>
- public SharedFileCache(
- IOptionsMonitor<Options> optionsMonitor,
- ISoulseekFileFactory soulseekFileFactory = null)
+ public SharedFileCache(ISoulseekFileFactory soulseekFileFactory = null)
{
- OptionsMonitor = optionsMonitor;
SoulseekFileFactory = soulseekFileFactory ?? new SoulseekFileFactory();
}
@@ -100,15 +98,14 @@ namespace slskd.Shares
/// </summary>
public IStateMonitor<SharedFileCacheState> StateMonitor => State;
- private IManagedState<SharedFileCacheState> State { get; } = new ManagedState<SharedFileCacheState>();
private ILogger Log { get; } = Serilog.Log.ForContext<SharedFileCache>();
private HashSet<string> MaskedDirectories { get; set; }
private Dictionary<string, File> MaskedFiles { get; set; }
- private IOptionsMonitor<Options> OptionsMonitor { get; set; }
private List<Share> Shares { get; set; }
+ private ISoulseekFileFactory SoulseekFileFactory { get; }
private SqliteConnection SQLite { get; set; }
+ private IManagedState<SharedFileCacheState> State { get; } = new ManagedState<SharedFileCacheState>();
private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1);
- private ISoulseekFileFactory SoulseekFileFactory { get; }
/// <summary>
/// Returns the contents of the cache.
@@ -164,8 +161,10 @@ namespace slskd.Shares
/// <summary>
/// Scans the configured shares and fills the cache.
/// </summary>
+ /// <param name="shares">The list of shares from which to fill the cache.</param>
+ /// <param name="filters">The list of regular expressions used to exclude files or paths from scanning.</param>
/// <returns>The operation context.</returns>
- public async Task FillAsync()
+ public async Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters)
{
// obtain the semaphore, or fail if it can't be obtained immediately, indicating that a scan is running.
if (!await SyncRoot.WaitAsync(millisecondsTimeout: 0))
@@ -176,7 +175,14 @@ namespace slskd.Shares
try
{
- State.SetValue(state => state with { Filling = true, Filled = false, FillProgress = 0 });
+ State.SetValue(state => state with
+ {
+ Filling = true,
+ Filled = false,
+ FillProgress = 0,
+ Shares = Enumerable.Empty<Share>().ToList().AsReadOnly(),
+ });
+
Log.Debug("Starting shared file scan");
await Task.Yield();
@@ -187,12 +193,7 @@ namespace slskd.Shares
var swSnapshot = 0L;
sw.Start();
- Shares = OptionsMonitor.CurrentValue.Directories.Shared
- .Select(share => share.TrimEnd('/', '\\'))
- .ToHashSet() // remove duplicates
- .Select(share => new Share(share)) // convert to Shares
- .OrderByDescending(share => share.LocalPath.Length) // process subdirectories first. this allows them to be aliased separately from their parent
- .ToList();
+ Shares = shares.ToList();
if (!Shares.Any())
{
@@ -236,9 +237,6 @@ namespace slskd.Shares
Log.Debug("Found {Directories} shared directories (and {Excluded} were excluded) in {Elapsed}ms. Starting file scan.", unmaskedDirectories.Count, excludedDirectories.Count(), sw.ElapsedMilliseconds - swSnapshot);
swSnapshot = sw.ElapsedMilliseconds;
- var filters = OptionsMonitor.CurrentValue.Filters.Share
- .Select(filter => new Regex(filter, RegexOptions.Compiled));
-
var files = new Dictionary<string, File>();
var maskedDirectories = new HashSet<string>();
var current = 0;
@@ -300,7 +298,16 @@ namespace slskd.Shares
MaskedDirectories = maskedDirectories;
MaskedFiles = files;
- State.SetValue(state => state with { Filling = false, Faulted = false, Filled = true, FillProgress = 1, Directories = MaskedDirectories.Count, Files = MaskedFiles.Count });
+ State.SetValue(state => state with {
+ Filling = false,
+ Faulted = false,
+ Filled = true,
+ FillProgress = 1,
+ Directories = MaskedDirectories.Count,
+ Files = MaskedFiles.Count,
+ Shares = shares.ToList().AsReadOnly(),
+ });
+
Log.Debug($"Shared file cache recreated in {sw.ElapsedMilliseconds}ms. Directories: {unmaskedDirectories.Count}, Files: {files.Count}");
}
catch (Exception ex)
@@ -451,7 +458,7 @@ namespace slskd.Shares
SQLite = new SqliteConnection("Data Source=file:shares?mode=memory&cache=shared");
SQLite.Open();
- using var cmd = new SqliteCommand("CREATE VIRTUAL TABLE cache USING fts5(filename)", SQLite);
+ using var cmd = new SqliteCommand("DROP TABLE IF EXISTS cache; CREATE VIRTUAL TABLE cache USING fts5(filename);", SQLite);
cmd.ExecuteNonQuery();
}
diff --git a/src/slskd/Shares/SoulseekFileFactory.cs b/src/slskd/Shares/SoulseekFileFactory.cs
index 5221265b..7cdd6174 100644
--- a/src/slskd/Shares/SoulseekFileFactory.cs
+++ b/src/slskd/Shares/SoulseekFileFactory.cs
@@ -48,7 +48,7 @@ namespace slskd.Shares
private static readonly string[] VideoExtensions = { "mkv", "ogv", "avi", "wmv", "asf", "mp4", "m4p", "m4v", "mpg", "mpe", "mpv", "mpg", "m2v" };
private static readonly HashSet<string> SupportedExtensions = AudioExtensions.Concat(VideoExtensions).ToHashSet();
- private ILogger Log { get; } = Serilog.Log.ForContext<SharedFileCache>();
+ private ILogger Log { get; } = Serilog.Log.ForContext<SoulseekFileFactory>();
/// <summary>
/// Creates an instance of <see cref="Soulseek.File"/> from the given path.