Use ByteSize for file size in attachments

This commit is contained in:
Alexey Golub
2019-03-03 16:38:41 +02:00
parent 019278f6d4
commit a564906719
6 changed files with 12 additions and 21 deletions

View File

@@ -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<int>();
var height = json["height"]?.Value<int>();
var fileName = json["filename"].Value<string>();
var fileSize = json["size"].Value<long>();
var fileSizeBytes = json["size"].Value<long>();
var fileSize = ByteSize.FromBytes(fileSizeBytes);
return new Attachment(id, width, height, url, fileName, fileSize);
}

View File

@@ -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<Node> nodes)
{
var buffer = new StringBuilder();
@@ -250,7 +235,6 @@ namespace DiscordChatExporter.Core.Services
scriptObject.Import(nameof(GroupMessages), new Func<IEnumerable<Message>, IEnumerable<MessageGroup>>(GroupMessages));
scriptObject.Import(nameof(Format), new Func<IFormattable, string, string>(Format));
scriptObject.Import(nameof(FormatDate), new Func<DateTime, string>(FormatDate));
scriptObject.Import(nameof(FormatFileSize), new Func<long, string>(FormatFileSize));
scriptObject.Import(nameof(FormatMarkdown), new Func<string, string>(FormatMarkdown));
return scriptObject;