From 4113101c0cc31c9fe067c1cd9aad8322fb5a6bdd Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sun, 24 Mar 2019 12:36:11 +0200 Subject: [PATCH] Get rid of ByteSize in favor of our own class --- .../DiscordChatExporter.Core.csproj | 1 - DiscordChatExporter.Core/Models/Attachment.cs | 5 +- DiscordChatExporter.Core/Models/FileSize.cs | 84 +++++++++++++++++++ .../Services/DataService.Parsers.cs | 3 +- Readme.md | 1 - 5 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 DiscordChatExporter.Core/Models/FileSize.cs diff --git a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj index ea27afc..aa7adc1 100644 --- a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj +++ b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj @@ -16,7 +16,6 @@ - diff --git a/DiscordChatExporter.Core/Models/Attachment.cs b/DiscordChatExporter.Core/Models/Attachment.cs index a92c235..4e54314 100644 --- a/DiscordChatExporter.Core/Models/Attachment.cs +++ b/DiscordChatExporter.Core/Models/Attachment.cs @@ -1,5 +1,4 @@ using System; -using ByteSizeLib; namespace DiscordChatExporter.Core.Models { @@ -23,9 +22,9 @@ namespace DiscordChatExporter.Core.Models FileName.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) || FileName.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase); - public ByteSize FileSize { get; } + public FileSize FileSize { get; } - public Attachment(string id, int? width, int? height, string url, string fileName, ByteSize fileSize) + public Attachment(string id, int? width, int? height, string url, string fileName, FileSize fileSize) { Id = id; Url = url; diff --git a/DiscordChatExporter.Core/Models/FileSize.cs b/DiscordChatExporter.Core/Models/FileSize.cs new file mode 100644 index 0000000..d52464b --- /dev/null +++ b/DiscordChatExporter.Core/Models/FileSize.cs @@ -0,0 +1,84 @@ +using System; + +namespace DiscordChatExporter.Core.Models +{ + // Loosely based on https://github.com/omar/ByteSize (MIT license) + + public struct FileSize + { + public const long BytesInKiloByte = 1024; + public const long BytesInMegaByte = 1024 * BytesInKiloByte; + public const long BytesInGigaByte = 1024 * BytesInMegaByte; + public const long BytesInTeraByte = 1024 * BytesInGigaByte; + public const long BytesInPetaByte = 1024 * BytesInTeraByte; + + public const string ByteSymbol = "B"; + public const string KiloByteSymbol = "KB"; + public const string MegaByteSymbol = "MB"; + public const string GigaByteSymbol = "GB"; + public const string TeraByteSymbol = "TB"; + public const string PetaByteSymbol = "PB"; + + public double Bytes { get; } + public double KiloBytes => Bytes / BytesInKiloByte; + public double MegaBytes => Bytes / BytesInMegaByte; + public double GigaBytes => Bytes / BytesInGigaByte; + public double TeraBytes => Bytes / BytesInTeraByte; + public double PetaBytes => Bytes / BytesInPetaByte; + + public string LargestWholeNumberSymbol + { + get + { + // Absolute value is used to deal with negative values + if (Math.Abs(PetaBytes) >= 1) + return PetaByteSymbol; + + if (Math.Abs(TeraBytes) >= 1) + return TeraByteSymbol; + + if (Math.Abs(GigaBytes) >= 1) + return GigaByteSymbol; + + if (Math.Abs(MegaBytes) >= 1) + return MegaByteSymbol; + + if (Math.Abs(KiloBytes) >= 1) + return KiloByteSymbol; + + return ByteSymbol; + } + } + + public double LargestWholeNumberValue + { + get + { + // Absolute value is used to deal with negative values + if (Math.Abs(PetaBytes) >= 1) + return PetaBytes; + + if (Math.Abs(TeraBytes) >= 1) + return TeraBytes; + + if (Math.Abs(GigaBytes) >= 1) + return GigaBytes; + + if (Math.Abs(MegaBytes) >= 1) + return MegaBytes; + + if (Math.Abs(KiloBytes) >= 1) + return KiloBytes; + + return Bytes; + } + } + + public FileSize(double bytes) + { + Bytes = bytes; + } + + public override string ToString() => $"{LargestWholeNumberValue:0.##} {LargestWholeNumberSymbol}"; + } +} \ No newline at end of file diff --git a/DiscordChatExporter.Core/Services/DataService.Parsers.cs b/DiscordChatExporter.Core/Services/DataService.Parsers.cs index 837548c..426831e 100644 --- a/DiscordChatExporter.Core/Services/DataService.Parsers.cs +++ b/DiscordChatExporter.Core/Services/DataService.Parsers.cs @@ -1,7 +1,6 @@ using System; using System.Drawing; using System.Linq; -using ByteSizeLib; using DiscordChatExporter.Core.Internal; using DiscordChatExporter.Core.Models; using Newtonsoft.Json.Linq; @@ -72,7 +71,7 @@ namespace DiscordChatExporter.Core.Services var fileName = json["filename"].Value(); var fileSizeBytes = json["size"].Value(); - var fileSize = ByteSize.FromBytes(fileSizeBytes); + var fileSize = new FileSize(fileSizeBytes); return new Attachment(id, width, height, url, fileName, fileSize); } diff --git a/Readme.md b/Readme.md index 647f071..6a396e4 100644 --- a/Readme.md +++ b/Readme.md @@ -34,7 +34,6 @@ DiscordChatExporter can be used to export message history from a [Discord](https - [Scriban](https://github.com/lunet-io/scriban) - [CommandLineParser](https://github.com/commandlineparser/commandline) - [Ookii.Dialogs](https://github.com/caioproiete/ookii-dialogs-wpf) -- [ByteSize](https://github.com/omar/ByteSize) - [Failsafe](https://github.com/Tyrrrz/Failsafe) - [Gress](https://github.com/Tyrrrz/Gress) - [Onova](https://github.com/Tyrrrz/Onova)