diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-05-21 08:59:02 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-21 08:59:02 -0500 |
| commit | 1d0978ab2273255f0decc8c9b6239ef5d0a0c5bd (patch) | |
| tree | 432afbaf7f55c1dc571a48661564078969a6049b /src/slskd/Common | |
| parent | 510de3d91a58b60b59447a785b6b6a1b3ccc5bd7 (diff) | |
| parent | 82a38b0f0b405c03503f3b1fff81a7981227b267 (diff) | |
| download | slskd-1d0978ab2273255f0decc8c9b6239ef5d0a0c5bd.tar.gz | |
Merge pull request #77 from slskd/warnings
Resolve noisy build warnings
Diffstat (limited to 'src/slskd/Common')
| -rw-r--r-- | src/slskd/Common/Extensions.cs | 11 | ||||
| -rw-r--r-- | src/slskd/Common/SharedFileCache.cs | 5 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/CompositeValidationResult.cs | 24 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/DirectoryExistsAttributes.cs | 43 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/FileExistsAttribute.cs | 22 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/ValidateAttribute.cs | 22 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/ValidationExtensions.cs | 35 |
7 files changed, 142 insertions, 20 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 4bf9f919..32cbf443 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -75,8 +75,15 @@ namespace slskd { string[] sizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; - if (value < 0) { return "-" + SizeSuffix(-value); } - if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); } + if (value < 0) + { + return "-" + SizeSuffix(-value); + } + + if (value == 0) + { + return string.Format("{0:n" + decimalPlaces + "} bytes", 0); + } // mag is 0 for bytes, 1 for KB, 2, for MB, etc. int mag = (int)Math.Log(value, 1024); diff --git a/src/slskd/Common/SharedFileCache.cs b/src/slskd/Common/SharedFileCache.cs index e1e5d584..00496a47 100644 --- a/src/slskd/Common/SharedFileCache.cs +++ b/src/slskd/Common/SharedFileCache.cs @@ -45,11 +45,12 @@ namespace slskd public event EventHandler<(int Directories, int Files)> Refreshed; public string Directory { get; } - private Dictionary<string, Soulseek.File> Files { get; set; } public DateTime? LastFill { get; set; } + public long TTL { get; } + + private Dictionary<string, Soulseek.File> Files { get; set; } private SqliteConnection SQLite { get; set; } private ReaderWriterLockSlim SyncRoot { get; } = new ReaderWriterLockSlim(); - public long TTL { get; } /// <summary> /// Scans the configured <see cref="Directory"/> and fills the cache. diff --git a/src/slskd/Common/Validation/CompositeValidationResult.cs b/src/slskd/Common/Validation/CompositeValidationResult.cs index f8cd8bc7..5484ae64 100644 --- a/src/slskd/Common/Validation/CompositeValidationResult.cs +++ b/src/slskd/Common/Validation/CompositeValidationResult.cs @@ -1,8 +1,28 @@ -namespace slskd.Validation -{ +// <copyright file="CompositeValidationResult.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.Validation +{ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; + /// <summary> + /// The composite result of a recursive validation. + /// </summary> public class CompositeValidationResult : ValidationResult { public CompositeValidationResult(string errorMessage) diff --git a/src/slskd/Common/Validation/DirectoryExistsAttributes.cs b/src/slskd/Common/Validation/DirectoryExistsAttributes.cs new file mode 100644 index 00000000..34ea2f19 --- /dev/null +++ b/src/slskd/Common/Validation/DirectoryExistsAttributes.cs @@ -0,0 +1,43 @@ +// <copyright file="DirectoryExistsAttributes.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.Validation +{ + using System.ComponentModel.DataAnnotations; + using System.IO; + + /// <summary> + /// Validates that the directory at the specified path exists. + /// </summary> + public class DirectoryExistsAttribute : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + if (value != null) + { + var dir = Path.GetFullPath(value?.ToString()); + + if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) + { + return new ValidationResult($"The {validationContext.DisplayName} field specifies a non-existent directory '{dir}'."); + } + } + + return ValidationResult.Success; + } + } +}
\ No newline at end of file diff --git a/src/slskd/Common/Validation/FileExistsAttribute.cs b/src/slskd/Common/Validation/FileExistsAttribute.cs index 8493b783..c1b3fd15 100644 --- a/src/slskd/Common/Validation/FileExistsAttribute.cs +++ b/src/slskd/Common/Validation/FileExistsAttribute.cs @@ -1,8 +1,28 @@ -namespace slskd.Validation +// <copyright file="FileExistsAttribute.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.Validation { using System.ComponentModel.DataAnnotations; using System.IO; + /// <summary> + /// Validates that the file at the specified path exists. + /// </summary> public class FileExistsAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) diff --git a/src/slskd/Common/Validation/ValidateAttribute.cs b/src/slskd/Common/Validation/ValidateAttribute.cs index b9daab28..72ef25e1 100644 --- a/src/slskd/Common/Validation/ValidateAttribute.cs +++ b/src/slskd/Common/Validation/ValidateAttribute.cs @@ -1,8 +1,28 @@ -namespace slskd.Validation +// <copyright file="ValidateAttribute.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.Validation { using System.Collections.Generic; using System.ComponentModel.DataAnnotations; + /// <summary> + /// Indicates that attributed properties should be recursively validated. + /// </summary> public class ValidateAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) diff --git a/src/slskd/Common/Validation/ValidationExtensions.cs b/src/slskd/Common/Validation/ValidationExtensions.cs index 11cdca62..fd1da598 100644 --- a/src/slskd/Common/Validation/ValidationExtensions.cs +++ b/src/slskd/Common/Validation/ValidationExtensions.cs @@ -1,22 +1,33 @@ -namespace slskd.Validation +// <copyright file="ValidationExtensions.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.Validation { using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; + /// <summary> + /// Extension methods for types involved with validaiton. + /// </summary> public static class ValidationExtensions { - public static string GetResultView(this ValidationResult result, string message) - { - var view = result.GetResultView(0).ToList(); - - if (view.Count() > 0 && !string.IsNullOrEmpty(message)) - { - view[0] = message; - } - - return string.Join("\n", view); - } + public static string GetResultView(this CompositeValidationResult result) + => string.Join("\n", result.GetResultView(0)); private static IEnumerable<string> GetResultView(this ValidationResult result, int depth = 0) { |
