namespace slskd { using System; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; /// /// Extensions. /// public static class Extensions { /// /// Converts the given path to the local format (normalizes path separators). /// /// /// public static string ToLocalOSPath(this string path) { return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); } /// /// Returns the directory from the given path, regardless of separator format. /// /// /// 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. /// /// /// public static string Sha1(this string str) { using (var sha1 = new SHA1Managed()) { return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", ""); } } /// /// Formats byte to nearest size (KB, MB, etc.) /// /// /// /// 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]); } } }