From 5e11cc0fc0703dc161140b28f7e2cd0f65facf9c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 10 Sep 2022 13:17:18 -0500 Subject: consolidate share options, add cache storage option --- src/slskd/Core/Options.cs | 76 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 15 deletions(-) (limited to 'src/slskd/Core/Options.cs') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index d6d4b04c..327036aa 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -196,6 +196,13 @@ namespace slskd [RequiresRestart] public DirectoriesOptions Directories { get; init; } = new DirectoriesOptions(); + /// + /// Gets share options. + /// + [Validate] + [RequiresRestart] + public SharesOptions Shares { get; init; } = new SharesOptions(); + /// /// Gets global options. /// @@ -324,7 +331,7 @@ namespace slskd /// /// Directory options. /// - public class DirectoriesOptions : IValidatableObject + public class DirectoriesOptions { /// /// Gets the path where incomplete downloads are saved. @@ -345,14 +352,36 @@ namespace slskd [DirectoryExists(ensureWriteable: true)] [RequiresRestart] public string Downloads { get; init; } = Program.DefaultDownloadsDirectory; + } + /// + /// Share options. + /// + public class SharesOptions : IValidatableObject + { /// /// Gets the list of paths to shared files. /// [Argument('s', "shared")] [EnvironmentVariable("SHARED_DIR")] - [Description("path to shared files")] - public string[] Shared { get; init; } = Array.Empty(); + [Description("paths to shared files")] + public string[] Directories { get; init; } = Array.Empty(); + + /// + /// Gets the list of shared file filters. + /// + [Argument(default, "share-filter")] + [EnvironmentVariable("SHARE_FILTER")] + [Description("regular expressions to filter files from shares")] + public string[] Filters { get; init; } = Array.Empty(); + + /// + /// Gets the type of storage to use for the share cache. + /// + [Argument(default, "share-cache-storage")] + [EnvironmentVariable("SHARE_CACHE_STORAGE")] + [Description("the type of storage to use for the cache")] + public StorageType CacheStorage { get; init; } = StorageType.Memory; /// /// Extended validation. @@ -363,17 +392,27 @@ namespace slskd { var results = new List(); + results.AddRange(ValidateShares()); + results.AddRange(ValidateFilters()); + + return results; + } + + private IEnumerable ValidateShares() + { + var results = new List(); + bool IsBlankPath(string share) => Regex.IsMatch(share.LocalizePath(), @"^(!|-){0,1}(\[.*\])$"); - Shared.Where(share => IsBlankPath(share)).ToList() + Directories.Where(share => IsBlankPath(share)).ToList() .ForEach(blank => results.Add(new ValidationResult($"Share {blank} doees not specify a path"))); bool IsRootMount(string share) => Regex.IsMatch(share.LocalizePath(), @"^(!|-){0,1}(\[.*\])/$"); - Shared.Where(share => IsRootMount(share)).ToList() + Directories.Where(share => IsRootMount(share)).ToList() .ForEach(blank => results.Add(new ValidationResult($"Share {blank} specifies a root mount, which is not supported."))); // starts with '/', 'X:', or '\\' bool IsAbsolutePath(string share) => Regex.IsMatch(share.LocalizePath(), @"^(!|-){0,1}(\[.*\])?(\/|[a-zA-Z]:|\\\\).*$"); - Shared.Where(share => !IsAbsolutePath(share)).ToList() + Directories.Where(share => !IsAbsolutePath(share)).ToList() .ForEach(relativePath => results.Add(new ValidationResult($"Share {relativePath} contains a relative path; only absolute paths are supported."))); (string Raw, string Alias, string Path) Digest(string share) @@ -388,7 +427,7 @@ namespace slskd return (share, share.Split(new[] { '/', '\\' }).Last(), share); } - var digestedShared = Shared + var digestedShared = Directories .Select(share => Digest(share.TrimEnd('/', '\\'))) .ToHashSet(); @@ -415,6 +454,21 @@ namespace slskd return results; } + + private IEnumerable ValidateFilters() + { + var results = new List(); + + foreach (var filter in Filters) + { + if (!filter.IsValidRegex()) + { + results.Add(new ValidationResult($"Share filter '{filter}' is not a valid regular expression")); + } + } + + return results; + } } /// @@ -644,14 +698,6 @@ namespace slskd /// public class FiltersOptions : IValidatableObject { - /// - /// Gets the list of shared file filters. - /// - [Argument(default, "share-filter")] - [EnvironmentVariable("SHARE_FILTER")] - [Description("regular expressions to filter files from shares")] - public string[] Share { get; init; } = Array.Empty(); - /// /// Gets search filter options. /// -- cgit v1.2.3 From dab9af45aa0d06b882c661fc3cddb6af6f61545e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 10 Sep 2022 17:37:33 -0500 Subject: add cache type --- config/slskd.example.yml | 3 ++- src/slskd/Core/Options.cs | 47 ++++++++++++++++++++--------------------------- 2 files changed, 22 insertions(+), 28 deletions(-) (limited to 'src/slskd/Core/Options.cs') diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 46033bff..1f628077 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -19,7 +19,8 @@ # - \.ini$ # - Thumbs.db$ # - \.DS_Store$ -# cache_storage: memory +# cache: +# storage_mode: Memory # rooms: # - ~ # global: diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 327036aa..2d561e29 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -200,7 +200,6 @@ namespace slskd /// Gets share options. /// [Validate] - [RequiresRestart] public SharesOptions Shares { get; init; } = new SharesOptions(); /// @@ -376,12 +375,26 @@ namespace slskd public string[] Filters { get; init; } = Array.Empty(); /// - /// Gets the type of storage to use for the share cache. + /// Share caching options. /// - [Argument(default, "share-cache-storage")] - [EnvironmentVariable("SHARE_CACHE_STORAGE")] - [Description("the type of storage to use for the cache")] - public StorageType CacheStorage { get; init; } = StorageType.Memory; + [Validate] + public ShareCacheOptions Cache { get; init; } = new ShareCacheOptions(); + + /// + /// Share caching options. + /// + public class ShareCacheOptions + { + /// + /// Gets the type of storage to use for the share cache. + /// + [Argument(default, "share-cache-storage-mode")] + [EnvironmentVariable("SHARE_CACHE_STORAGE_MODE")] + [Description("the type of storage to use for the cache")] + [Enum(typeof(StorageMode))] + [RequiresRestart] + public string StorageMode { get; init; } = slskd.StorageMode.Memory.ToString().ToLowerInvariant(); + } /// /// Extended validation. @@ -696,7 +709,7 @@ namespace slskd /// /// Filter options. /// - public class FiltersOptions : IValidatableObject + public class FiltersOptions { /// /// Gets search filter options. @@ -704,26 +717,6 @@ namespace slskd [Validate] public SearchOptions Search { get; init; } = new SearchOptions(); - /// - /// Extended validation. - /// - /// - /// - public IEnumerable Validate(ValidationContext validationContext) - { - var results = new List(); - - foreach (var filter in Share) - { - if (!filter.IsValidRegex()) - { - results.Add(new ValidationResult($"Share filter '{filter}' is not a valid regular expression")); - } - } - - return results; - } - /// /// Search filter options. /// -- cgit v1.2.3