diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-07-29 06:58:40 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-29 06:58:40 -0500 |
| commit | f22fff743ffcff1f71fccf738793255c5070ece7 (patch) | |
| tree | af3c12d1180acff4ff5a8fb0af8632eb93d0e1e3 /src | |
| parent | 69d29c1eaa03992cf786d1d9b1ec1935dd85ef42 (diff) | |
| parent | c643d91c7feb9ab360539c17380e24673f0a102b (diff) | |
| download | slskd-f22fff743ffcff1f71fccf738793255c5070ece7.tar.gz | |
Merge pull request #132 from slskd/shares
Allow multiple shared directories
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application/ISharedFileCache.cs | 4 | ||||
| -rw-r--r-- | src/slskd/Application/Service.cs | 15 | ||||
| -rw-r--r-- | src/slskd/Application/SharedFileCache.cs | 45 | ||||
| -rw-r--r-- | src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs | 9 | ||||
| -rw-r--r-- | src/slskd/Options.cs | 3 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 58 |
6 files changed, 94 insertions, 40 deletions
diff --git a/src/slskd/Application/ISharedFileCache.cs b/src/slskd/Application/ISharedFileCache.cs index a7b8bf40..97133e40 100644 --- a/src/slskd/Application/ISharedFileCache.cs +++ b/src/slskd/Application/ISharedFileCache.cs @@ -25,12 +25,12 @@ namespace slskd { event EventHandler<(int Directories, int Files)> Refreshed; - string Directory { get; } + IEnumerable<string> Directories { get; } DateTime? LastFill { get; } long TTL { get; } void Fill(); - IEnumerable<Soulseek.File> Search(SearchQuery query); + IEnumerable<File> Search(SearchQuery query); } }
\ No newline at end of file diff --git a/src/slskd/Application/Service.cs b/src/slskd/Application/Service.cs index 1d2aec25..ef47cf24 100644 --- a/src/slskd/Application/Service.cs +++ b/src/slskd/Application/Service.cs @@ -19,6 +19,7 @@ namespace slskd { using System; using System.Collections.Concurrent; + using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; @@ -325,10 +326,16 @@ namespace slskd /// <returns>A Task resolving an IEnumerable of Soulseek.Directory.</returns> private Task<BrowseResponse> BrowseResponseResolver(string username, IPEndPoint endpoint) { - var directories = System.IO.Directory - .GetDirectories(OptionsMonitor.CurrentValue.Directories.Shared, "*", SearchOption.AllDirectories) - .Select(dir => new Soulseek.Directory(dir.Replace("/", @"\"), System.IO.Directory.GetFiles(dir) - .Select(f => new Soulseek.File(1, Path.GetFileName(f), new FileInfo(f).Length, Path.GetExtension(f))))); + var shares = OptionsMonitor.CurrentValue.Directories.Shared; + var directories = new List<Soulseek.Directory>(); + + foreach (var share in shares) + { + directories.AddRange(System.IO.Directory + .GetDirectories(share, "*", SearchOption.AllDirectories) + .Select(dir => new Soulseek.Directory(dir.Replace("/", @"\"), System.IO.Directory.GetFiles(dir) + .Select(f => new Soulseek.File(1, Path.GetFileName(f), new FileInfo(f).Length, Path.GetExtension(f)))))); + } return Task.FromResult(new BrowseResponse(directories)); } diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index 00496a47..45a810fc 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -34,17 +34,17 @@ namespace slskd /// <summary> /// Initializes a new instance of the <see cref="SharedFileCache"/> class. /// </summary> - /// <param name="directory"></param> + /// <param name="directories"></param> /// <param name="ttl"></param> - public SharedFileCache(string directory, long ttl) + public SharedFileCache(IEnumerable<string> directories, long ttl) { - Directory = directory; + Directories = directories; TTL = ttl; } public event EventHandler<(int Directories, int Files)> Refreshed; - public string Directory { get; } + public IEnumerable<string> Directories { get; } public DateTime? LastFill { get; set; } public long TTL { get; } @@ -53,7 +53,7 @@ namespace slskd private ReaderWriterLockSlim SyncRoot { get; } = new ReaderWriterLockSlim(); /// <summary> - /// Scans the configured <see cref="Directory"/> and fills the cache. + /// Scans the configured <see cref="Directories"/> and fills the cache. /// </summary> public void Fill() { @@ -68,20 +68,43 @@ namespace slskd { CreateTable(); - var directories = System.IO.Directory.GetDirectories(Directory, "*", SearchOption.AllDirectories); + var directories = 0; + var files = new Dictionary<string, Soulseek.File>(); - Files = System.IO.Directory.GetFiles(Directory, "*", SearchOption.AllDirectories) - .Select(f => new Soulseek.File(1, f.Replace("/", @"\"), new FileInfo(f).Length, Path.GetExtension(f))) - .ToDictionary(f => f.Filename, f => f); + foreach (var directory in Directories) + { + directories += System.IO.Directory.GetDirectories(directory, "*", SearchOption.AllDirectories).Length; + + // recursively find all files in the directory and stick a record in a dictionary, keyed on the sanitized + // filename and with a value of a Soulseek.File object + var newFiles = System.IO.Directory.GetFiles(directory, "*", SearchOption.AllDirectories) + .Select(f => new Soulseek.File(1, f.Replace("/", @"\"), new FileInfo(f).Length, Path.GetExtension(f))) + .ToDictionary(f => f.Filename, f => f); + + // merge the new dictionary with the rest + // this will overwrite any duplicate keys, but keys are the fully qualified name + // the only time this *should* cause problems is if one of the shares is a subdirectory of another. + foreach (var file in newFiles) + { + if (files.ContainsKey(file.Key)) + { + Console.WriteLine($"[WARNING] File {file.Key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories."); + } + + files[file.Key] = file.Value; + } + } // potentially optimize with multi-valued insert // https://stackoverflow.com/questions/16055566/insert-multiple-rows-in-sqlite - foreach (var file in Files) + foreach (var file in files) { InsertFilename(file.Key); } - Refreshed?.Invoke(this, (directories.Length, Files.Count)); + Files = files; + + Refreshed?.Invoke(this, (directories, Files.Count)); } finally { diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs index eae4b2eb..a13850dc 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -20,6 +20,7 @@ namespace slskd.Configuration using System; using System.Linq; using System.Reflection; + using System.Text.Json; using Microsoft.Extensions.Configuration; /// <summary> @@ -110,6 +111,14 @@ namespace slskd.Configuration Data[key] = value.ToString(); } } + else + { + // serialize array defaults and stick them on the parent key + // (not indexed by array position). this value is "stuck", and + // we want to show that in the config debug view. this isn't really + // functional, just illustrative. + Data[key] = JsonSerializer.Serialize(property.GetValue(defaults)); + } } } } diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 4a032c47..c9a107cc 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -239,9 +239,8 @@ namespace slskd [Argument('s', "shared")] [EnvironmentVariable("SHARED_DIR")] [Description("path to shared files")] - [DirectoryExists] [RequiresRestart] - public string Shared { get; private set; } = Program.DefaultSharedDirectory; + public string[] Shared { get; private set; } = new[] { Program.DefaultSharedDirectory }; } /// <summary> diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index b5275756..fa1be786 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -212,19 +212,6 @@ namespace slskd return; } - try - { - VerifyOrCreateDirectory(OptionsAtStartup.Directories.App); - VerifyOrCreateDirectory(OptionsAtStartup.Directories.Incomplete); - VerifyOrCreateDirectory(OptionsAtStartup.Directories.Downloads); - VerifyOrCreateDirectory(OptionsAtStartup.Directories.Shared, verifyWriteable: false); - } - catch (Exception ex) - { - Console.WriteLine($"Filesystem exception: {ex.Message}"); - return; - } - Log.Logger = (OptionsAtStartup.Debug ? new LoggerConfiguration().MinimumLevel.Debug() : new LoggerConfiguration().MinimumLevel.Information()) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("slskd.API.Authentication.PassthroughAuthenticationHandler", LogEventLevel.Information) @@ -249,6 +236,26 @@ namespace slskd var logger = Log.ForContext(typeof(Program)); + try + { + VerifyDirectory(OptionsAtStartup.Directories.App, createIfMissing: true, verifyWriteable: true); + VerifyDirectory(OptionsAtStartup.Directories.Incomplete, createIfMissing: true, verifyWriteable: true); + VerifyDirectory(OptionsAtStartup.Directories.Downloads, createIfMissing: true, verifyWriteable: true); + + foreach (var share in OptionsAtStartup.Directories.Shared) + { + if (!VerifyDirectory(share, createIfMissing: false, verifyWriteable: false)) + { + logger.Warning("Shared directory {Directory} does not exist", share); + } + } + } + catch (Exception ex) + { + logger.Error(ex, "Filesystem exception: {Message}", ex.Message); + return; + } + if (!OptionsAtStartup.NoLogo) { PrintLogo(Version); @@ -505,19 +512,26 @@ namespace slskd Console.WriteLine(banner); } - private static void VerifyOrCreateDirectory(string directory, bool verifyWriteable = true) + private static bool VerifyDirectory(string directory, bool createIfMissing = true, bool verifyWriteable = true) { - try + if (!Directory.Exists(directory)) { - if (!Directory.Exists(directory)) + if (createIfMissing) + { + try + { + Directory.CreateDirectory(directory); + } + catch (Exception ex) + { + throw new IOException($"Directory {directory} does not exist, and could not be created: {ex.Message}", ex); + } + } + else { - Directory.CreateDirectory(directory); + return false; } } - catch (Exception ex) - { - throw new IOException($"Directory {directory} does not exist, and could not be created: {ex.Message}", ex); - } if (verifyWriteable) { @@ -533,6 +547,8 @@ namespace slskd throw new IOException($"Directory {directory} is not writeable: {ex.Message}", ex); } } + + return true; } } }
\ No newline at end of file |
