diff --git a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj index 276e305..9b95e78 100644 --- a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj +++ b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj @@ -20,6 +20,7 @@ + diff --git a/DiscordChatExporter.Core/Models/Attachment.cs b/DiscordChatExporter.Core/Models/Attachment.cs index ebf5f76..f8d6336 100644 --- a/DiscordChatExporter.Core/Models/Attachment.cs +++ b/DiscordChatExporter.Core/Models/Attachment.cs @@ -1,4 +1,6 @@ -namespace DiscordChatExporter.Core.Models +using ByteSizeLib; + +namespace DiscordChatExporter.Core.Models { // https://discordapp.com/developers/docs/resources/channel#attachment-object @@ -16,9 +18,9 @@ public string FileName { get; } - public long FileSize { get; } + public ByteSize FileSize { get; } - public Attachment(string id, int? width, int? height, string url, string fileName, long fileSize) + public Attachment(string id, int? width, int? height, string url, string fileName, ByteSize fileSize) { Id = id; Url = url; diff --git a/DiscordChatExporter.Core/Resources/ExportTemplates/HtmlShared/Main.html b/DiscordChatExporter.Core/Resources/ExportTemplates/HtmlShared/Main.html index 85bf3c0..999714e 100644 --- a/DiscordChatExporter.Core/Resources/ExportTemplates/HtmlShared/Main.html +++ b/DiscordChatExporter.Core/Resources/ExportTemplates/HtmlShared/Main.html @@ -79,7 +79,7 @@ {{~ # Non-image ~}} {{~ else ~}} - Attachment: {{ attachment.FileName }} ({{ attachment.FileSize | FormatFileSize }}) + Attachment: {{ attachment.FileName }} ({{ attachment.FileSize }}) {{~ end ~}} diff --git a/DiscordChatExporter.Core/Services/DataService.Parsers.cs b/DiscordChatExporter.Core/Services/DataService.Parsers.cs index 4bf89db..837548c 100644 --- a/DiscordChatExporter.Core/Services/DataService.Parsers.cs +++ b/DiscordChatExporter.Core/Services/DataService.Parsers.cs @@ -1,6 +1,7 @@ using System; using System.Drawing; using System.Linq; +using ByteSizeLib; using DiscordChatExporter.Core.Internal; using DiscordChatExporter.Core.Models; using Newtonsoft.Json.Linq; @@ -69,7 +70,9 @@ namespace DiscordChatExporter.Core.Services var width = json["width"]?.Value(); var height = json["height"]?.Value(); var fileName = json["filename"].Value(); - var fileSize = json["size"].Value(); + var fileSizeBytes = json["size"].Value(); + + var fileSize = ByteSize.FromBytes(fileSizeBytes); return new Attachment(id, width, height, url, fileName, fileSize); } diff --git a/DiscordChatExporter.Core/Services/ExportService.TemplateModel.cs b/DiscordChatExporter.Core/Services/ExportService.TemplateModel.cs index f6a5824..e593cfb 100644 --- a/DiscordChatExporter.Core/Services/ExportService.TemplateModel.cs +++ b/DiscordChatExporter.Core/Services/ExportService.TemplateModel.cs @@ -77,21 +77,6 @@ namespace DiscordChatExporter.Core.Services private string FormatDate(DateTime dateTime) => Format(dateTime, _dateFormat); - private string FormatFileSize(long fileSize) - { - string[] units = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; - double size = fileSize; - var unit = 0; - - while (size >= 1024) - { - size /= 1024; - ++unit; - } - - return $"{size:0.#} {units[unit]}"; - } - private string FormatMarkdownPlainText(IEnumerable nodes) { var buffer = new StringBuilder(); @@ -250,7 +235,6 @@ namespace DiscordChatExporter.Core.Services scriptObject.Import(nameof(GroupMessages), new Func, IEnumerable>(GroupMessages)); scriptObject.Import(nameof(Format), new Func(Format)); scriptObject.Import(nameof(FormatDate), new Func(FormatDate)); - scriptObject.Import(nameof(FormatFileSize), new Func(FormatFileSize)); scriptObject.Import(nameof(FormatMarkdown), new Func(FormatMarkdown)); return scriptObject; diff --git a/Readme.md b/Readme.md index 6a396e4..647f071 100644 --- a/Readme.md +++ b/Readme.md @@ -34,6 +34,7 @@ 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)