diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-09-10 13:17:18 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-09-10 13:17:18 -0500 |
| commit | 5e11cc0fc0703dc161140b28f7e2cd0f65facf9c (patch) | |
| tree | e3fd43aca6207fae410b49569db86ed25de26303 /src | |
| parent | b0420bd49b64995671b80deeef54cffece0a4d41 (diff) | |
| download | slskd-5e11cc0fc0703dc161140b28f7e2cd0f65facf9c.tar.gz | |
consolidate share options, add cache storage option
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Options.cs | 76 |
1 files changed, 61 insertions, 15 deletions
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 @@ -197,6 +197,13 @@ namespace slskd public DirectoriesOptions Directories { get; init; } = new DirectoriesOptions(); /// <summary> + /// Gets share options. + /// </summary> + [Validate] + [RequiresRestart] + public SharesOptions Shares { get; init; } = new SharesOptions(); + + /// <summary> /// Gets global options. /// </summary> [Validate] @@ -324,7 +331,7 @@ namespace slskd /// <summary> /// Directory options. /// </summary> - public class DirectoriesOptions : IValidatableObject + public class DirectoriesOptions { /// <summary> /// 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; + } + /// <summary> + /// Share options. + /// </summary> + public class SharesOptions : IValidatableObject + { /// <summary> /// Gets the list of paths to shared files. /// </summary> [Argument('s', "shared")] [EnvironmentVariable("SHARED_DIR")] - [Description("path to shared files")] - public string[] Shared { get; init; } = Array.Empty<string>(); + [Description("paths to shared files")] + public string[] Directories { get; init; } = Array.Empty<string>(); + + /// <summary> + /// Gets the list of shared file filters. + /// </summary> + [Argument(default, "share-filter")] + [EnvironmentVariable("SHARE_FILTER")] + [Description("regular expressions to filter files from shares")] + public string[] Filters { get; init; } = Array.Empty<string>(); + + /// <summary> + /// Gets the type of storage to use for the share cache. + /// </summary> + [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; /// <summary> /// Extended validation. @@ -363,17 +392,27 @@ namespace slskd { var results = new List<ValidationResult>(); + results.AddRange(ValidateShares()); + results.AddRange(ValidateFilters()); + + return results; + } + + private IEnumerable<ValidationResult> ValidateShares() + { + var results = new List<ValidationResult>(); + 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<ValidationResult> ValidateFilters() + { + var results = new List<ValidationResult>(); + + foreach (var filter in Filters) + { + if (!filter.IsValidRegex()) + { + results.Add(new ValidationResult($"Share filter '{filter}' is not a valid regular expression")); + } + } + + return results; + } } /// <summary> @@ -645,14 +699,6 @@ namespace slskd public class FiltersOptions : IValidatableObject { /// <summary> - /// Gets the list of shared file filters. - /// </summary> - [Argument(default, "share-filter")] - [EnvironmentVariable("SHARE_FILTER")] - [Description("regular expressions to filter files from shares")] - public string[] Share { get; init; } = Array.Empty<string>(); - - /// <summary> /// Gets search filter options. /// </summary> [Validate] |
