From 9082bc8d2adc633b85027ca232770b49a000944e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Sep 2022 09:20:20 -0500 Subject: put ISharedFileCache back into SharedFileCache, refactor out Browse() --- src/slskd/Shares/ISharedFileCache.cs | 98 ----------------------------- src/slskd/Shares/ShareService.cs | 44 ++++++++++++- src/slskd/Shares/SharedFileCache.cs | 118 ++++++++++++++++++++--------------- 3 files changed, 110 insertions(+), 150 deletions(-) delete mode 100644 src/slskd/Shares/ISharedFileCache.cs (limited to 'src') diff --git a/src/slskd/Shares/ISharedFileCache.cs b/src/slskd/Shares/ISharedFileCache.cs deleted file mode 100644 index c59f5bba..00000000 --- a/src/slskd/Shares/ISharedFileCache.cs +++ /dev/null @@ -1,98 +0,0 @@ -// -// 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/. -// - -namespace slskd.Shares -{ - using System.Collections.Generic; - using System.Text.RegularExpressions; - using System.Threading.Tasks; - using Soulseek; - - /// - /// Shared file cache. - /// - public interface ISharedFileCache - { - /// - /// Gets the cache state monitor. - /// - IStateMonitor StateMonitor { get; } - - /// - /// 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(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. - /// - /// The list of shares from which to fill the cache. - /// The list of regular expressions used to exclude files or paths from scanning. - /// The operation context. - Task FillAsync(IEnumerable shares, IEnumerable filters); - - /// - /// Returns the contents of the specified . - /// - /// The directory for which the contents are to be listed. - /// The contents of the directory. - Directory List(string directory); - - /// - /// Substitutes the mask in the specified with the original path, if the mask is tracked - /// by the cache. - /// - /// The fully qualified filename to unmask. - /// The unmasked filename. - string Resolve(string filename); - - /// - /// Searches the cache for the specified and returns the matching files. - /// - /// The query for which to search. - /// The matching files. - IEnumerable Search(SearchQuery query); - - /// - /// Cancels the currently running fill operation, if one is running. - /// - /// A value indicating whether a fill operation was cancelled. - bool TryCancelFill(); - - /// - /// Attempts to load the cache from disk. - /// - /// A value indicating whether the load was successful. - bool TryLoad(); - } -} \ No newline at end of file diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 22bebb95..d48cd5d1 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -20,6 +20,7 @@ using Microsoft.Extensions.Options; namespace slskd.Shares { + using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; @@ -96,7 +97,48 @@ namespace slskd.Shares /// The entire contents of the share. public Task> BrowseAsync(Share share = null) { - var results = Cache.Browse(share); + 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 Repository.ListDirectories(prefix)) + { + directories.TryAdd(directory, new Directory(directory)); + } + + var files = Repository.ListFiles(prefix, includeFullPath: true); + + var groups = files + .GroupBy(file => Path.GetDirectoryName(file.Filename)) + .Select(group => new Directory(group.Key, group.Select(f => + { + return new File( + f.Code, + Path.GetFileName(f.Filename), // we can send the full path, or just the filename. save bandwidth and omit the path. + f.Size, + f.Extension, + f.Attributes); + }).OrderBy(f => f.Filename))); + + // merge the dictionary containing all directories with the Soulseek.Directory instances containing their files. + // entries with no files will remain untouched. + foreach (var group in groups) + { + directories.AddOrUpdate(group.Name, group, (_, _) => group); + } + + var results = directories.Values.OrderBy(d => d.Name); + var normalizedResults = results.Select(r => new Directory(r.Name.NormalizePath(), r.Files)); return Task.FromResult(normalizedResults); diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 92aec245..3ff3d10f 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -20,7 +20,6 @@ using System.IO; namespace slskd.Shares { using System; - using System.Collections.Concurrent; using System.Collections.Generic; using System.Data; using System.Diagnostics; @@ -33,6 +32,73 @@ namespace slskd.Shares using Serilog; using Soulseek; + /// + /// Shared file cache. + /// + public interface ISharedFileCache + { + /// + /// Gets the cache state monitor. + /// + IStateMonitor StateMonitor { get; } + + /// + /// 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. + /// + /// The list of shares from which to fill the cache. + /// The list of regular expressions used to exclude files or paths from scanning. + /// The operation context. + Task FillAsync(IEnumerable shares, IEnumerable filters); + + /// + /// Returns the contents of the specified . + /// + /// The directory for which the contents are to be listed. + /// The contents of the directory. + Directory List(string directory); + + /// + /// Substitutes the mask in the specified with the original path, if the mask is tracked + /// by the cache. + /// + /// The fully qualified filename to unmask. + /// The unmasked filename. + string Resolve(string filename); + + /// + /// Searches the cache for the specified and returns the matching files. + /// + /// The query for which to search. + /// The matching files. + IEnumerable Search(SearchQuery query); + + /// + /// Cancels the currently running fill operation, if one is running. + /// + /// A value indicating whether a fill operation was cancelled. + bool TryCancelFill(); + + /// + /// Attempts to load the cache from disk. + /// + /// A value indicating whether the load was successful. + bool TryLoad(); + } + /// /// Shared file cache. /// @@ -67,56 +133,6 @@ namespace slskd.Shares private CancellationTokenSource CancellationTokenSource { get; set; } private IShareRepository Repository { get; } - /// - /// 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(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 Repository.ListDirectories(prefix)) - { - directories.TryAdd(directory, new Directory(directory)); - } - - var files = Repository.ListFiles(prefix, includeFullPath: true); - - var groups = files - .GroupBy(file => Path.GetDirectoryName(file.Filename)) - .Select(group => new Directory(group.Key, group.Select(f => - { - return new File( - f.Code, - Path.GetFileName(f.Filename), // we can send the full path, or just the filename. save bandwidth and omit the path. - f.Size, - f.Extension, - f.Attributes); - }).OrderBy(f => f.Filename))); - - // merge the dictionary containing all directories with the Soulseek.Directory instances containing their files. - // entries with no files will remain untouched. - foreach (var group in groups) - { - directories.AddOrUpdate(group.Name, group, (_, _) => group); - } - - return directories.Values.OrderBy(d => d.Name); - } - /// /// Returns the number of directories in the specified . /// -- cgit v1.2.3