From 1e31a281156c2b25ca21ecad6fbbc1c529145242 Mon Sep 17 00:00:00 2001 From: href Date: Thu, 2 Sep 2021 00:55:52 +0200 Subject: link: humanize file size --- lib/lsg_irc/link_plugin.ex | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/lsg_irc/link_plugin.ex b/lib/lsg_irc/link_plugin.ex index ced80b2..0c785a0 100644 --- a/lib/lsg_irc/link_plugin.ex +++ b/lib/lsg_irc/link_plugin.ex @@ -182,13 +182,13 @@ defmodule LSG.IRC.LinkPlugin do case get_body(url, 30_000_000, client, handler, <<>>) do {:ok, _} = ok -> ok :error -> - {:ok, "file: #{content_type}, size: #{length} bytes"} + {:ok, "file: #{content_type}, size: #{human_size(length)}"} end #String.starts_with?(content_type, "text/html") && length <= 30_000_000 -> # get_body(url, 30_000_000, client, <<>>) true -> :hackney.close(client) - {:ok, "file: #{content_type}, size: #{length} bytes"} + {:ok, "file: #{content_type}, size: #{human_size(length)}"} end end @@ -256,4 +256,11 @@ defmodule LSG.IRC.LinkPlugin do {:ok, [uri], "-> #{URI.to_string(uri)}"} end + + defp human_size(bytes) do + bytes + |> FileSize.new(:b) + |> FileSize.scale() + |> FileSize.format() + end end -- cgit v1.2.3