From a0eeb805b67e9bcbfea20d9bc8e85db172ddd7a0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 13 Feb 2021 07:58:29 -0600 Subject: move Extensions to common --- src/slskd/Common/Extensions.cs | 119 +++++++++++++++++++++++++++++++++++++++++ src/slskd/Extensions.cs | 119 ----------------------------------------- 2 files changed, 119 insertions(+), 119 deletions(-) create mode 100644 src/slskd/Common/Extensions.cs delete mode 100644 src/slskd/Extensions.cs (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs new file mode 100644 index 00000000..b7243549 --- /dev/null +++ b/src/slskd/Common/Extensions.cs @@ -0,0 +1,119 @@ +// +// 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/. +// + +namespace slskd +{ + using System; + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; + using System.IO; + using System.Linq; + using System.Security.Cryptography; + using System.Text; + using slskd.Validation; + + /// + /// Extensions. + /// + public static class Extensions + { + /// + /// Returns the directory from the given path, regardless of separator format. + /// + /// The path. + /// The directory. + public static string DirectoryName(this string path) + { + var separator = path.Contains('\\') ? '\\' : '/'; + var parts = path.Split(separator); + return string.Join(separator, parts.Take(parts.Length - 1)); + } + + /// + /// Returns the SHA1 hash of the given string. + /// + /// The string to hash. + /// The hash. + public static string Sha1(this string str) + { + using var sha1 = new SHA1Managed(); + return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty); + } + + /// + /// Formats byte to nearest size (KB, MB, etc.) + /// + /// The value to format. + /// The number of decimal places to include. + /// The formatted string. + public static string SizeSuffix(this double value, int decimalPlaces = 1) + { + 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); } + + // mag is 0 for bytes, 1 for KB, 2, for MB, etc. + int mag = (int)Math.Log(value, 1024); + + // 1L << (mag * 10) == 2 ^ (10 * mag) [i.e. the number of bytes in the unit corresponding to mag] + decimal adjustedSize = (decimal)value / (1L << (mag * 10)); + + // make adjustment when the value is large enough that it would round up to 1000 or more + if (Math.Round(adjustedSize, decimalPlaces) >= 1000) + { + mag += 1; + adjustedSize /= 1024; + } + + return string.Format( + "{0:n" + decimalPlaces + "} {1}", + adjustedSize, + sizeSuffixes[mag]); + } + + /// + /// Converts the given path to the local format (normalizes path separators). + /// + /// The path to convert. + /// The converted path. + public static string ToLocalOSPath(this string path) + { + return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); + } + + /// + /// Validates options. + /// + /// The options instance to validate. + /// The result of the validation, if invalid. + /// A value indicating whether the instance is valid. + public static bool TryValidate(this Options options, out CompositeValidationResult result) + { + result = null; + var results = new List(); + + if (!Validator.TryValidateObject(options, new ValidationContext(options), results, true)) + { + result = new CompositeValidationResult("Invalid configuration", results); + return false; + } + + return true; + } + } +} \ No newline at end of file diff --git a/src/slskd/Extensions.cs b/src/slskd/Extensions.cs deleted file mode 100644 index b7243549..00000000 --- a/src/slskd/Extensions.cs +++ /dev/null @@ -1,119 +0,0 @@ -// -// 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/. -// - -namespace slskd -{ - using System; - using System.Collections.Generic; - using System.ComponentModel.DataAnnotations; - using System.IO; - using System.Linq; - using System.Security.Cryptography; - using System.Text; - using slskd.Validation; - - /// - /// Extensions. - /// - public static class Extensions - { - /// - /// Returns the directory from the given path, regardless of separator format. - /// - /// The path. - /// The directory. - public static string DirectoryName(this string path) - { - var separator = path.Contains('\\') ? '\\' : '/'; - var parts = path.Split(separator); - return string.Join(separator, parts.Take(parts.Length - 1)); - } - - /// - /// Returns the SHA1 hash of the given string. - /// - /// The string to hash. - /// The hash. - public static string Sha1(this string str) - { - using var sha1 = new SHA1Managed(); - return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty); - } - - /// - /// Formats byte to nearest size (KB, MB, etc.) - /// - /// The value to format. - /// The number of decimal places to include. - /// The formatted string. - public static string SizeSuffix(this double value, int decimalPlaces = 1) - { - 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); } - - // mag is 0 for bytes, 1 for KB, 2, for MB, etc. - int mag = (int)Math.Log(value, 1024); - - // 1L << (mag * 10) == 2 ^ (10 * mag) [i.e. the number of bytes in the unit corresponding to mag] - decimal adjustedSize = (decimal)value / (1L << (mag * 10)); - - // make adjustment when the value is large enough that it would round up to 1000 or more - if (Math.Round(adjustedSize, decimalPlaces) >= 1000) - { - mag += 1; - adjustedSize /= 1024; - } - - return string.Format( - "{0:n" + decimalPlaces + "} {1}", - adjustedSize, - sizeSuffixes[mag]); - } - - /// - /// Converts the given path to the local format (normalizes path separators). - /// - /// The path to convert. - /// The converted path. - public static string ToLocalOSPath(this string path) - { - return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); - } - - /// - /// Validates options. - /// - /// The options instance to validate. - /// The result of the validation, if invalid. - /// A value indicating whether the instance is valid. - public static bool TryValidate(this Options options, out CompositeValidationResult result) - { - result = null; - var results = new List(); - - if (!Validator.TryValidateObject(options, new ValidationContext(options), results, true)) - { - result = new CompositeValidationResult("Invalid configuration", results); - return false; - } - - return true; - } - } -} \ No newline at end of file -- cgit v1.2.3