diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-09-20 20:13:05 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-20 20:13:05 -0500 |
| commit | c4544a6a14577a79d02d009f38557cbcf9ec80ad (patch) | |
| tree | 69ae056823d2d5d050e2c80ff45b1ffa2126c1b4 /src/slskd/Core | |
| parent | 2a1a7aca5f7a6fa70652135f7a7034bef55dfed4 (diff) | |
| parent | 6a307269ab99956fb2c5ea2149ae76c3ef24c8b2 (diff) | |
| download | slskd-c4544a6a14577a79d02d009f38557cbcf9ec80ad.tar.gz | |
Merge branch 'master' into aux/docs-proofreading
Diffstat (limited to 'src/slskd/Core')
| -rw-r--r-- | src/slskd/Core/ConnectionStrings.cs | 30 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 135 | ||||
| -rw-r--r-- | src/slskd/Core/State.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Core/Types/StorageMode.cs | 35 |
4 files changed, 158 insertions, 43 deletions
diff --git a/src/slskd/Core/ConnectionStrings.cs b/src/slskd/Core/ConnectionStrings.cs new file mode 100644 index 00000000..6afab93f --- /dev/null +++ b/src/slskd/Core/ConnectionStrings.cs @@ -0,0 +1,30 @@ +// <copyright file="ConnectionStrings.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd +{ + /// <summary> + /// Application connection strings. + /// </summary> + public record ConnectionStrings + { + public string Transfers { get; init; } + public string Shares { get; init; } + public string SharesBackup { get; init; } + public string Search { get; init; } + } +} diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 6a435db1..070476d1 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -197,6 +197,12 @@ namespace slskd public DirectoriesOptions Directories { get; init; } = new DirectoriesOptions(); /// <summary> + /// Gets share options. + /// </summary> + [Validate] + public SharesOptions Shares { get; init; } = new SharesOptions(); + + /// <summary> /// Gets global options. /// </summary> [Validate] @@ -324,7 +330,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 +351,60 @@ 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> + /// Share caching options. + /// </summary> + [Validate] + public ShareCacheOptions Cache { get; init; } = new ShareCacheOptions(); + + /// <summary> + /// Share caching options. + /// </summary> + public class ShareCacheOptions + { + /// <summary> + /// Gets the type of storage to use for the share cache. + /// </summary> + [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(); + + /// <summary> + /// Gets the number of workers to use while scanning shares. + /// </summary> + [Argument(default, "share-cache-workers")] + [EnvironmentVariable("SHARE_CACHE_WORKERS")] + [Description("the number of workers to use while scanning shares")] + [Range(1, 128)] + [RequiresRestart] + public int Workers { get; init; } = 16; + } /// <summary> /// Extended validation. @@ -363,17 +415,27 @@ namespace slskd { var results = new List<ValidationResult>(); - bool IsBlankPath(string share) => Regex.IsMatch(share.ToLocalOSPath(), @"^(!|-){0,1}(\[.*\])$"); - Shared.Where(share => IsBlankPath(share)).ToList() + 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}(\[.*\])$"); + 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.ToLocalOSPath(), @"^(!|-){0,1}(\[.*\])/$"); - Shared.Where(share => IsRootMount(share)).ToList() + bool IsRootMount(string share) => Regex.IsMatch(share.LocalizePath(), @"^(!|-){0,1}(\[.*\])/$"); + 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.ToLocalOSPath(), @"^(!|-){0,1}(\[.*\])?(\/|[a-zA-Z]:|\\\\).*$"); - Shared.Where(share => !IsAbsolutePath(share)).ToList() + bool IsAbsolutePath(string share) => Regex.IsMatch(share.LocalizePath(), @"^(!|-){0,1}(\[.*\])?(\/|[a-zA-Z]:|\\\\).*$"); + 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 +450,7 @@ namespace slskd return (share, share.Split(new[] { '/', '\\' }).Last(), share); } - var digestedShared = Shared + var digestedShared = Directories .Select(share => Digest(share.TrimEnd('/', '\\'))) .ToHashSet(); @@ -415,6 +477,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> @@ -642,43 +719,15 @@ namespace slskd /// <summary> /// Filter options. /// </summary> - public class FiltersOptions : IValidatableObject + public class FiltersOptions { /// <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] public SearchOptions Search { get; init; } = new SearchOptions(); /// <summary> - /// Extended validation. - /// </summary> - /// <param name="validationContext"></param> - /// <returns></returns> - public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) - { - var results = new List<ValidationResult>(); - - foreach (var filter in Share) - { - if (!filter.IsValidRegex()) - { - results.Add(new ValidationResult($"Share filter '{filter}' is not a valid regular expression")); - } - } - - return results; - } - - /// <summary> /// Search filter options. /// </summary> public class SearchOptions : IValidatableObject @@ -755,12 +804,12 @@ namespace slskd /// Gets metrics endpoint authentication options. /// </summary> [Validate] - public AuthenticationOptions Authentication { get; init; } = new AuthenticationOptions(); + public MetricsAuthenticationOptions Authentication { get; init; } = new MetricsAuthenticationOptions(); /// <summary> /// Metrics endpoint authentication options. /// </summary> - public class AuthenticationOptions + public class MetricsAuthenticationOptions { /// <summary> /// Gets a value indicating whether authentication should be disabled. @@ -1114,12 +1163,12 @@ namespace slskd /// Gets authentication options. /// </summary> [Validate] - public AuthenticationOptions Authentication { get; init; } = new AuthenticationOptions(); + public WebAuthenticationOptions Authentication { get; init; } = new WebAuthenticationOptions(); /// <summary> /// Authentication options. /// </summary> - public class AuthenticationOptions + public class WebAuthenticationOptions { /// <summary> /// Gets a value indicating whether authentication should be disabled. diff --git a/src/slskd/Core/State.cs b/src/slskd/Core/State.cs index a1da2be8..1b798ad3 100644 --- a/src/slskd/Core/State.cs +++ b/src/slskd/Core/State.cs @@ -102,6 +102,7 @@ namespace slskd { public bool ScanPending { get; init; } public bool Scanning { get; init; } + public bool Ready { get; init; } public bool Faulted { get; init; } public double ScanProgress { get; init; } public int Directories { get; init; } diff --git a/src/slskd/Core/Types/StorageMode.cs b/src/slskd/Core/Types/StorageMode.cs new file mode 100644 index 00000000..aced3050 --- /dev/null +++ b/src/slskd/Core/Types/StorageMode.cs @@ -0,0 +1,35 @@ +// <copyright file="StorageMode.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd +{ + /// <summary> + /// Storage mode. + /// </summary> + public enum StorageMode + { + /// <summary> + /// Stored in memory. + /// </summary> + Memory = 0, + + /// <summary> + /// Stored on disk. + /// </summary> + Disk = 1, + } +} |
