diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2020-12-30 21:16:50 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2020-12-30 21:16:50 -0600 |
| commit | 8f1068ac3135bcccf09374bc787ef22049b43461 (patch) | |
| tree | 5d80a254f5ee7b7c6b3956f6a7307591bc5a93e2 /src/slskd/Extensions.cs | |
| parent | ec4a85e4e8b64a0ae91c494e3c8ef520f0a8b8c3 (diff) | |
| download | slskd-8f1068ac3135bcccf09374bc787ef22049b43461.tar.gz | |
copy files from example
Diffstat (limited to 'src/slskd/Extensions.cs')
| -rw-r--r-- | src/slskd/Extensions.cs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/slskd/Extensions.cs b/src/slskd/Extensions.cs new file mode 100644 index 00000000..e4d8a582 --- /dev/null +++ b/src/slskd/Extensions.cs @@ -0,0 +1,82 @@ +namespace slskd +{ + using System; + using System.IO; + using System.Linq; + using System.Security.Cryptography; + using System.Text; + + /// <summary> + /// Extensions. + /// </summary> + public static class Extensions + { + /// <summary> + /// Converts the given path to the local format (normalizes path separators). + /// </summary> + /// <param name="path"></param> + /// <returns></returns> + public static string ToLocalOSPath(this string path) + { + return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); + } + + /// <summary> + /// Returns the directory from the given path, regardless of separator format. + /// </summary> + /// <param name="path"></param> + /// <returns></returns> + 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)); + } + + /// <summary> + /// Returns the SHA1 hash of the given string. + /// </summary> + /// <param name="str"></param> + /// <returns></returns> + public static string Sha1(this string str) + { + using (var sha1 = new SHA1Managed()) + { + return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", ""); + } + } + + /// <summary> + /// Formats byte to nearest size (KB, MB, etc.) + /// </summary> + /// <param name="value"></param> + /// <param name="decimalPlaces"></param> + /// <returns></returns> + 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]); + } + } +} |
