From d64d555931c085631268fcb6d98da86caf616617 Mon Sep 17 00:00:00 2001 From: TJ Date: Mon, 22 Jul 2024 16:03:17 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=20storage=20=E4=BB=A5=E5=8F=8A=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/aier_bot/bot.ex | 5 +-- .../{file_downloader.ex => file_helper.ex} | 36 +++++++++++++------ 2 files changed, 29 insertions(+), 12 deletions(-) rename lib/aier_bot/{file_downloader.ex => file_helper.ex} (63%) diff --git a/lib/aier_bot/bot.ex b/lib/aier_bot/bot.ex index 6f45deb..cb7280d 100644 --- a/lib/aier_bot/bot.ex +++ b/lib/aier_bot/bot.ex @@ -1,6 +1,6 @@ defmodule AierBot.Bot do alias AierBot.CobaltClient - alias AierBot.FileDownloader + alias AierBot.FileHelper @bot :save_it_bot @@ -38,9 +38,10 @@ defmodule AierBot.Bot do case CobaltClient.get_download_url(url) do {:ok, download_url} -> - {:ok, file_name, file_content} = FileDownloader.download(download_url) + {:ok, file_name, file_content} = FileHelper.download(download_url) {:ok, _} = bot_send_file(chat.id, file_name, file_content, url) + FileHelper.write_into_file(chat.id, file_name, file_content) {:error, error} -> ExGram.send_message(chat.id, "Failed to download file. Reason: #{inspect(error)}") diff --git a/lib/aier_bot/file_downloader.ex b/lib/aier_bot/file_helper.ex similarity index 63% rename from lib/aier_bot/file_downloader.ex rename to lib/aier_bot/file_helper.ex index 8621b2c..92774df 100644 --- a/lib/aier_bot/file_downloader.ex +++ b/lib/aier_bot/file_helper.ex @@ -1,4 +1,4 @@ -defmodule AierBot.FileDownloader do +defmodule AierBot.FileHelper do use Tesla # youtube video download url: "https://olly.imput.net/api/stream?id=WpsLJCeQ24MBD_xM_3uwu&exp=1721625834931&sig=4UvjCvFD57jU7yrLdwmzRmfsPgPb8KhFIE1DwmnOj14&sec=C1Hty_eEXvswFhzdrDfDZ4cmkSUDgex1aV6mzDSK0dc&iv=ozku3rLJzeV_rVRSzWVlFw" @@ -8,7 +8,6 @@ defmodule AierBot.FileDownloader do case get(url) do {:ok, %Tesla.Env{status: 200, body: body}} -> file_name = gen_desc_filename() <> ".mp4" - File.write("./.local/storage/#{file_name}", body) {:ok, file_name, body} {:ok, %Tesla.Env{status: status}} -> @@ -34,7 +33,6 @@ defmodule AierBot.FileDownloader do |> List.last() file_name = gen_desc_filename() <> "." <> ext - File.write("./.local/storage/#{file_name}", body) {:ok, file_name, body} {:ok, %Tesla.Env{status: status}} -> @@ -47,14 +45,32 @@ defmodule AierBot.FileDownloader do end end - @spec gen_desc_filename() :: String.t() - defp gen_desc_filename() do - # 10^16 - current_time = 9999999999999999 - current_time = 9999999999999999 - 1630848000000 = 9999999998361159 - max = :math.pow(10, 16) |> round() - utc_now = DateTime.utc_now() |> DateTime.to_unix() + def write_into_file(chat_id, file_name, file_content) do + dir = Path.join(["./.local/storage", Integer.to_string(chat_id)]) - (max - utc_now) + case File.mkdir_p(dir) do + :ok -> + case File.write(Path.join([dir, file_name]), file_content) do + :ok -> + IO.puts("File written successfully.") + + {:error, reason} -> + IO.puts("Failed to write file: #{reason}") + end + + {:error, reason} -> + IO.puts("Failed to create directory: #{reason}") + end + end + + # version 2: 2124-01-01 00:00:00 UTC is 4624022400 + # version 1: 10^16 - current_time, since JS max_safe_integer is 2^53 - 1 = 9007199254740991 + defp gen_desc_filename(datetime \\ DateTime.utc_now()) do + last = ~U[2124-01-01 00:00:00Z] |> DateTime.to_unix() + current = datetime |> DateTime.to_unix() + + (last - current) |> Integer.to_string() - |> String.pad_leading(16, "0") + |> String.pad_leading(10, "0") end end