diff --git a/lib/banchan/accounts/accounts.ex b/lib/banchan/accounts/accounts.ex index 13b9e9e7..d090eefc 100644 --- a/lib/banchan/accounts/accounts.ex +++ b/lib/banchan/accounts/accounts.ex @@ -392,14 +392,14 @@ defmodule Banchan.Accounts do nil -> create_user_from_discord(auth) - |> add_oauth_pfp(auth) - |> add_discord_oauth_banner(auth) end end) |> Repo.transaction() |> case do {:ok, %{updated_user: user}} -> - {:ok, user} + user + |> add_oauth_pfp(auth) + |> add_discord_oauth_banner(auth) {:error, _, error, _} -> {:error, error} @@ -416,13 +416,13 @@ defmodule Banchan.Accounts do nil -> create_user_from_google(auth) - |> add_oauth_pfp(auth) end end) |> Repo.transaction() |> case do {:ok, %{updated_user: user}} -> - {:ok, user} + user + |> add_oauth_pfp(auth) {:error, _, error, _} -> {:error, error} @@ -582,19 +582,19 @@ defmodule Banchan.Accounts do end end - defp add_oauth_pfp({:ok, %User{} = user}, %Auth{info: %{image: url}}) when is_binary(url) do + defp add_oauth_pfp(%User{} = user, %Auth{info: %{image: url}}) when is_binary(url) do tmp_file = Path.join([ System.tmp_dir!(), - "oauth-pfp-#{:rand.uniform(100_000_000)}" <> Path.extname(url) + "oauth-pfp-#{:rand.uniform(100_000_000)}.jpg" ]) resp = HTTPoison.get!(url <> "?size=160") - File.write!(tmp_file, resp.body) - "." <> format = Path.extname(url) - {pfp, thumb} = - make_pfp_images!(user, user, tmp_file, "image/#{format}", Path.basename(tmp_file)) + Image.from_binary!(resp.body) + |> Image.write!(tmp_file, minimize_file_size: true) + + {pfp, thumb} = make_pfp_images!(user, user, tmp_file, "image/jpg", Path.basename(tmp_file)) File.rm!(tmp_file) @@ -604,14 +604,10 @@ defmodule Banchan.Accounts do }) end - defp add_oauth_pfp({:ok, %User{} = user}, %Auth{}) do + defp add_oauth_pfp(%User{} = user, %Auth{}) do {:ok, user} end - defp add_oauth_pfp({:error, error}, _) do - {:error, error} - end - defp random_password do :crypto.strong_rand_bytes(@rand_pass_length) |> Base.encode64() end @@ -861,7 +857,9 @@ defmodule Banchan.Accounts do end @doc """ - Saves a profile picture and schedules generation of corresponding thumbnails. + Saves a profile picture and schedules generation of corresponding + thumbnails. DO NOT CALL THIS INSIDE A TRANSACTION (the freshly-created + Upload won't be available for the Thumbnailer). """ def make_pfp_images!(%User{} = actor, %User{} = user, src, type, name) do if can_modify_user?(actor, user) do diff --git a/lib/banchan/validators.ex b/lib/banchan/validators.ex index 981a0f5c..eeb9a3b9 100644 --- a/lib/banchan/validators.ex +++ b/lib/banchan/validators.ex @@ -146,7 +146,7 @@ defmodule Banchan.Validators do |> validate_format(:furaffinity_handle, ~r/^[a-zA-Z0-9_-]+$/, message: "must be a valid Furaffinity handle." ) - |> validate_format(:discord_handle, ~r/^[a-zA-Z0-9_-]+(#\d{4})?$/, + |> validate_format(:discord_handle, ~r/^[a-zA-Z0-9_.-]+(#\d{4})?$/, message: "must be a valid Discord handle." ) |> validate_format(:artstation_handle, ~r/^[a-zA-Z0-9_]+$/,