From 388137d3a915490fd6f589e9a5e6ad538f18512e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Sep 2022 12:43:46 -0500 Subject: add cancellation endpoint --- .../Shares/API/Controllers/SharesController.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/slskd/Shares/API/Controllers/SharesController.cs') diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs index 12a1c7b2..a8d5ae96 100644 --- a/src/slskd/Shares/API/Controllers/SharesController.cs +++ b/src/slskd/Shares/API/Controllers/SharesController.cs @@ -189,5 +189,26 @@ namespace slskd.Shares.API return Ok(); } + + /// + /// Cancels a share scan, if one is running. + /// + /// + /// The request completed successfully. + /// A share scan was not in progress. + [HttpDelete] + [Route("")] + [Authorize] + [ProducesResponseType(204)] + [ProducesResponseType(404)] + public IActionResult CancelShareScan() + { + if (Shares.TryCancelScan()) + { + return StatusCode(204); + } + + return NotFound(); + } } } -- cgit v1.2.3 From c6a06b2d45faeab923865ca4bc2232ff5f7c8326 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Sep 2022 16:21:59 -0500 Subject: don't wait for scan --- src/slskd/Shares/API/Controllers/SharesController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Shares/API/Controllers/SharesController.cs') diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs index a8d5ae96..44d79a18 100644 --- a/src/slskd/Shares/API/Controllers/SharesController.cs +++ b/src/slskd/Shares/API/Controllers/SharesController.cs @@ -176,11 +176,11 @@ namespace slskd.Shares.API [Authorize] [ProducesResponseType(204)] [ProducesResponseType(409)] - public async Task RescanSharesAsync() + public IActionResult RescanSharesAsync() { try { - await Shares.StartScanAsync(); + _ = Shares.ScanAsync(); } catch (ShareScanInProgressException) { -- cgit v1.2.3 From 9b8d59a93b4661f7e3650e025b725498b9a5567b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Sep 2022 17:19:20 -0500 Subject: improve shares UI performance --- .../Shares/API/Controllers/SharesController.cs | 38 ++---- src/slskd/Shares/IShareService.cs | 9 +- src/slskd/Shares/ISharedFileCache.cs | 17 ++- src/slskd/Shares/ShareService.cs | 16 ++- src/slskd/Shares/SharedFileCache.cs | 152 +++++++++++++++------ 5 files changed, 162 insertions(+), 70 deletions(-) (limited to 'src/slskd/Shares/API/Controllers/SharesController.cs') diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs index 44d79a18..5f87f900 100644 --- a/src/slskd/Shares/API/Controllers/SharesController.cs +++ b/src/slskd/Shares/API/Controllers/SharesController.cs @@ -57,18 +57,13 @@ namespace slskd.Shares.API [ProducesResponseType(typeof(IEnumerable), 200)] public async Task List() { - var browse = await Shares.BrowseAsync(); + var shares = new List(); - var summary = Shares.Shares.Select(share => + foreach (var share in Shares.Shares) { - var directories = browse.Where(directory => directory.Name.StartsWith(share.RemotePath)); - var fileCount = directories.Aggregate(seed: 0, (sum, directory) => - { - sum += directory.FileCount; - return sum; - }); + var (directories, files) = await Shares.SummarizeShareAsync(share); - return new SummarizedShare() + shares.Add(new SummarizedShare() { Id = share.Id, Alias = share.Alias, @@ -76,12 +71,12 @@ namespace slskd.Shares.API LocalPath = share.LocalPath, Raw = share.Raw, RemotePath = share.RemotePath, - Directories = directories.Count(), - Files = fileCount, - }; - }); + Directories = directories, + Files = files, + }); + } - return Ok(summary); + return Ok(shares); } /// @@ -104,13 +99,7 @@ namespace slskd.Shares.API 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 (directories, files) = await Shares.SummarizeShareAsync(share); var summary = new SummarizedShare() { @@ -120,8 +109,8 @@ namespace slskd.Shares.API LocalPath = share.LocalPath, Raw = share.Raw, RemotePath = share.RemotePath, - Directories = directories.Count(), - Files = fileCount, + Directories = directories, + Files = files, }; return Ok(summary); @@ -159,8 +148,7 @@ namespace slskd.Shares.API return NotFound(); } - var contents = (await Shares.BrowseAsync()) - .Where(directory => directory.Name.StartsWith(share.RemotePath)); + var contents = await Shares.BrowseAsync(share); return Ok(contents); } diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index b625286e..73f6a4ad 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -42,7 +42,7 @@ namespace slskd.Shares /// Returns the entire contents of the share. /// /// The entire contents of the share. - Task> BrowseAsync(); + Task> BrowseAsync(Share share = null); /// /// Returns the contents of the specified . @@ -76,6 +76,13 @@ namespace slskd.Shares /// Thrown when a scan is already in progress. Task ScanAsync(); + /// + /// Gets summary information for the specified . + /// + /// The share to summarize. + /// The summary information. + Task<(int Directories, int Files)> SummarizeShareAsync(Share share); + /// /// Cancels the currently running scan, if one is running. /// diff --git a/src/slskd/Shares/ISharedFileCache.cs b/src/slskd/Shares/ISharedFileCache.cs index 73c9cd56..c59f5bba 100644 --- a/src/slskd/Shares/ISharedFileCache.cs +++ b/src/slskd/Shares/ISharedFileCache.cs @@ -35,8 +35,23 @@ namespace slskd.Shares /// /// Returns the contents of the cache. /// + /// The optional share to which to limit the scope of the browse. /// The contents of the cache. - IEnumerable Browse(); + IEnumerable Browse(Share share = null); + + /// + /// Returns the number of directories in the specified . + /// + /// The share for which the directories are to be counted. + /// The number of directories. + int CountDirectories(Share share); + + /// + /// Returns the number of files in the specified . + /// + /// The share for which the files are to be counted. + /// The number of files. + int CountFiles(Share share); /// /// Scans the configured shares and fills the cache. diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 17817b86..4307a98f 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -91,9 +91,9 @@ namespace slskd.Shares /// Returns the entire contents of the share. /// /// The entire contents of the share. - public Task> BrowseAsync() + public Task> BrowseAsync(Share share = null) { - var results = Cache.Browse(); + var results = Cache.Browse(share); var normalizedResults = results.Select(r => new Directory(r.Name.NormalizePath(), r.Files)); return Task.FromResult(normalizedResults); @@ -169,6 +169,18 @@ namespace slskd.Shares return Cache.FillAsync(Shares, FilterRegexes); } + /// + /// Gets summary information for the specified . + /// + /// The share to summarize. + /// The summary information. + public Task<(int Directories, int Files)> SummarizeShareAsync(Share share) + { + var dirs = Cache.CountDirectories(share); + var files = Cache.CountFiles(share); + return Task.FromResult((dirs, files)); + } + /// /// Cancels the currently running scan, if one is running. /// diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 17fc5fb5..a10202e2 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -25,6 +25,7 @@ namespace slskd.Shares using System.Data; using System.Diagnostics; using System.Linq; + using System.Security.Cryptography; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Channels; @@ -68,41 +69,30 @@ namespace slskd.Shares /// /// Returns the contents of the cache. /// + /// The optional share to which to limit the scope of the browse. /// The contents of the cache. - public IEnumerable Browse() + public IEnumerable Browse(Share share = null) { var directories = new ConcurrentDictionary(); + string prefix = null; + + if (share != null) + { + prefix = share.RemotePath + Path.DirectorySeparatorChar; + } + // Soulseek requires that each directory in the tree have an entry in the list returned in a browse response. if // missing, files that are nested within directories which contain only directories (no files) are displayed as being // in the root. to get around this, prime a dictionary with all known directories, and an empty Soulseek.Directory. if // there are any files in the directory, this entry will be overwritten with a new Soulseek.Directory containing the // files. if not they'll be left as is. - foreach (var directory in ListDirectories()) + foreach (var directory in ListDirectories(prefix)) { directories.TryAdd(directory, new Directory(directory)); } - var files = new List(); - - using var conn = GetConnection(); - using var cmd = new SqliteCommand("SELECT maskedFilename, code, size, extension, attributeJson FROM files ORDER BY maskedFilename ASC;", conn); - var reader = cmd.ExecuteReader(); - - while (reader.Read()) - { - var filename = reader.GetString(0); - var code = reader.GetInt32(1); - var size = reader.GetInt64(2); - var extension = reader.GetString(3); - var attributeJson = reader.GetString(4); - - var attributeList = attributeJson.FromJson>(); - - var file = new File(code, filename, size, extension, attributeList); - - files.Add(file); - } + var files = ListFiles(prefix, includeFullPath: true); var groups = files .GroupBy(file => Path.GetDirectoryName(file.Filename)) @@ -126,6 +116,28 @@ namespace slskd.Shares return directories.Values.OrderBy(d => d.Name); } + /// + /// Returns the number of directories in the specified . + /// + /// The share for which the directories are to be counted. + /// The number of directories. + public int CountDirectories(Share share) + { + var path = share.RemotePath + Path.DirectorySeparatorChar; + return CountDirectories(path); + } + + /// + /// Returns the number of files in the specified . + /// + /// The share for which the files are to be counted. + /// The number of files. + public int CountFiles(Share share) + { + var path = share.RemotePath + Path.DirectorySeparatorChar; + return CountFiles(path); + } + /// /// Scans the configured shares and fills the cache. /// @@ -623,22 +635,60 @@ namespace slskd.Shares sourceConn.BackupDatabase(backupConn); } - private int CountDirectories() + private int CountDirectories(string prefix = null) { using var conn = GetConnection(); - using var cmd = new SqliteCommand("SELECT COUNT(*) FROM directories;", conn); - var reader = cmd.ExecuteReader(); - reader.Read(); - return reader.GetInt32(0); + + SqliteCommand cmd = default; + + try + { + if (string.IsNullOrEmpty(prefix)) + { + cmd = new SqliteCommand("SELECT COUNT(*) FROM directories;", conn); + } + else + { + cmd = new SqliteCommand("SELECT COUNT(*) FROM directories WHERE name LIKE @prefix || '%'", conn); + cmd.Parameters.AddWithValue("prefix", prefix); + } + + var reader = cmd.ExecuteReader(); + reader.Read(); + return reader.GetInt32(0); + } + finally + { + cmd.Dispose(); + } } - private int CountFiles() + private int CountFiles(string prefix = null) { using var conn = GetConnection(); - using var cmd = new SqliteCommand("SELECT COUNT(*) FROM files;", conn); - var reader = cmd.ExecuteReader(); - reader.Read(); - return reader.GetInt32(0); + + SqliteCommand cmd = default; + + try + { + if (string.IsNullOrEmpty(prefix)) + { + cmd = new SqliteCommand("SELECT COUNT(*) FROM files;", conn); + } + else + { + cmd = new SqliteCommand("SELECT COUNT(*) FROM files WHERE maskedFilename LIKE @prefix || '%'", conn); + cmd.Parameters.AddWithValue("prefix", prefix); + } + + var reader = cmd.ExecuteReader(); + reader.Read(); + return reader.GetInt32(0); + } + finally + { + cmd.Dispose(); + } } private SqliteConnection GetConnection(string connectionString = null) @@ -710,24 +760,42 @@ namespace slskd.Shares return reader.GetInt64(0); } - private IEnumerable ListDirectories() + private IEnumerable ListDirectories(string prefix = null) { var results = new List(); using var conn = GetConnection(); - using var cmd = new SqliteCommand("SELECT name FROM directories ORDER BY name ASC;", conn); - var reader = cmd.ExecuteReader(); + SqliteCommand cmd = default; - while (reader.Read()) + try { - results.Add(reader.GetString(0)); - } + if (string.IsNullOrEmpty(prefix)) + { + cmd = new SqliteCommand("SELECT name FROM directories ORDER BY name ASC;", conn); + } + else + { + cmd = new SqliteCommand("SELECT name from directories WHERE name LIKE @prefix || '%' ORDER BY name ASC;", conn); + cmd.Parameters.AddWithValue("prefix", prefix); + } + + var reader = cmd.ExecuteReader(); + + while (reader.Read()) + { + results.Add(reader.GetString(0)); + } - return results; + return results; + } + finally + { + cmd.Dispose(); + } } - private IEnumerable ListFiles(string directory = null) + private IEnumerable ListFiles(string directory = null, bool includeFullPath = false) { var results = new List(); @@ -758,7 +826,9 @@ namespace slskd.Shares var attributeList = attributeJson.FromJson>(); - var file = new File(code, filename: Path.GetFileName(filename), size, extension, attributeList); + filename = includeFullPath ? filename : Path.GetFileName(filename); + + var file = new File(code, filename, size, extension, attributeList); results.Add(file); } -- cgit v1.2.3