2025-03-10 17:51:35 -08:00

43 lines
1.3 KiB
Elixir

defmodule Bright.Downloader do
@moduledoc """
Downloader functions
"""
require Logger
@user_agent "fp-curl/houston-we-have-a-request"
def get(url) do
filename = Bright.Cache.generate_filename(url)
Logger.debug("Downloader getting url=#{inspect(url)}")
download!(url, filename)
end
def download!(file_url, filename) do
Logger.debug("Downloader downloading file_url=#{file_url} to filename=#{filename}")
# Execute the curl command
case System.cmd(
"curl",
["--fail", "-L", "--user-agent", @user_agent, "--output", filename, file_url],
stderr_to_stdout: true
) do
{_output, 0} ->
# Success: curl completed with exit code 0
Logger.debug("Download completed successfully: #{filename}")
{:ok, filename}
{error_output, exit_code} ->
# Failure: curl returned a non-zero exit code
Logger.error("Download failed with exit code #{exit_code}: #{error_output}")
{:error, {:curl_failed, exit_code, error_output}}
end
rescue
exception ->
# Handle unexpected exceptions (e.g., file system errors)
Logger.error("Unexpected error during download: #{inspect(exception)}")
{:error, {:unexpected_error, exception}}
end
end