Use ByteSize for file size in attachments

pull/162/head
Alexey Golub 6 years ago
parent 019278f6d4
commit a564906719

@ -20,6 +20,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="ByteSize" Version="1.3.0" />
<PackageReference Include="Failsafe" Version="1.0.0" /> <PackageReference Include="Failsafe" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Onova" Version="2.2.0" /> <PackageReference Include="Onova" Version="2.2.0" />

@ -1,4 +1,6 @@
namespace DiscordChatExporter.Core.Models using ByteSizeLib;
namespace DiscordChatExporter.Core.Models
{ {
// https://discordapp.com/developers/docs/resources/channel#attachment-object // https://discordapp.com/developers/docs/resources/channel#attachment-object
@ -16,9 +18,9 @@
public string FileName { get; } 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; Id = id;
Url = url; Url = url;

@ -79,7 +79,7 @@
<img class="chatlog__attachment-thumbnail" src="{{ attachment.Url }}" /> <img class="chatlog__attachment-thumbnail" src="{{ attachment.Url }}" />
{{~ # Non-image ~}} {{~ # Non-image ~}}
{{~ else ~}} {{~ else ~}}
Attachment: {{ attachment.FileName }} ({{ attachment.FileSize | FormatFileSize }}) Attachment: {{ attachment.FileName }} ({{ attachment.FileSize }})
{{~ end ~}} {{~ end ~}}
</a> </a>
</div> </div>

@ -1,6 +1,7 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using ByteSizeLib;
using DiscordChatExporter.Core.Internal; using DiscordChatExporter.Core.Internal;
using DiscordChatExporter.Core.Models; using DiscordChatExporter.Core.Models;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -69,7 +70,9 @@ namespace DiscordChatExporter.Core.Services
var width = json["width"]?.Value<int>(); var width = json["width"]?.Value<int>();
var height = json["height"]?.Value<int>(); var height = json["height"]?.Value<int>();
var fileName = json["filename"].Value<string>(); 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); return new Attachment(id, width, height, url, fileName, fileSize);
} }

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

@ -34,6 +34,7 @@ DiscordChatExporter can be used to export message history from a [Discord](https
- [Scriban](https://github.com/lunet-io/scriban) - [Scriban](https://github.com/lunet-io/scriban)
- [CommandLineParser](https://github.com/commandlineparser/commandline) - [CommandLineParser](https://github.com/commandlineparser/commandline)
- [Ookii.Dialogs](https://github.com/caioproiete/ookii-dialogs-wpf) - [Ookii.Dialogs](https://github.com/caioproiete/ookii-dialogs-wpf)
- [ByteSize](https://github.com/omar/ByteSize)
- [Failsafe](https://github.com/Tyrrrz/Failsafe) - [Failsafe](https://github.com/Tyrrrz/Failsafe)
- [Gress](https://github.com/Tyrrrz/Gress) - [Gress](https://github.com/Tyrrrz/Gress)
- [Onova](https://github.com/Tyrrrz/Onova) - [Onova](https://github.com/Tyrrrz/Onova)

Loading…
Cancel
Save