From d7559647e293c9504b1ac85cf31843da63aac4d0 Mon Sep 17 00:00:00 2001 From: zack Date: Thu, 24 Oct 2024 20:22:37 -0400 Subject: [PATCH] fix some bugs n stuff --- config/config.exs | 4 -- config/dev.exs | 4 -- config/prod.exs | 4 -- config/runtime.exs | 4 -- .../live/image_live/show.html.heex | 10 +++- lib/zoeyscomputer_web/plugs/discord_plug.ex | 55 +++++++++++++++++++ lib/zoeyscomputer_web/router.ex | 30 +++++++++- 7 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 lib/zoeyscomputer_web/plugs/discord_plug.ex diff --git a/config/config.exs b/config/config.exs index 8723a50..b933638 100644 --- a/config/config.exs +++ b/config/config.exs @@ -22,10 +22,6 @@ config :zoeyscomputer, ZoeyscomputerWeb.Endpoint, pubsub_server: Zoeyscomputer.PubSub, live_view: [signing_salt: "uUcDyRmg"] -config :nanoid, - alphabet: "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", - size: 7 - # Configures the mailer # # By default it uses the "Local" adapter which stores the emails diff --git a/config/dev.exs b/config/dev.exs index 8627c25..f41b0c2 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -52,10 +52,6 @@ config :zoeyscomputer, ZoeyscomputerWeb.Endpoint, # configured to run both http and https servers on # different ports -config :nanoid, - size: 7, - alphabet: "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - # Watch static and templates for browser reloading. config :zoeyscomputer, ZoeyscomputerWeb.Endpoint, live_reload: [ diff --git a/config/prod.exs b/config/prod.exs index f97d46f..d9ca7f8 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -17,9 +17,5 @@ config :swoosh, local: false # Do not print debug messages in production config :logger, level: :info -config :nanoid, - size: 7, - alphabet: "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - # Runtime production configuration, including reading # of environment variables, is done on config/runtime.exs. diff --git a/config/runtime.exs b/config/runtime.exs index 99a756a..951308a 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -65,10 +65,6 @@ if config_env() == :prod do ], secret_key_base: secret_key_base - config :nanoid, - alphabet: "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", - size: 7 - # ## SSL Support # # To get SSL working, you will need to add the `https` key diff --git a/lib/zoeyscomputer_web/live/image_live/show.html.heex b/lib/zoeyscomputer_web/live/image_live/show.html.heex index 8e2c430..3434ddf 100644 --- a/lib/zoeyscomputer_web/live/image_live/show.html.heex +++ b/lib/zoeyscomputer_web/live/image_live/show.html.heex @@ -1,7 +1,7 @@ <.header> uploaded by <%= @image.user.email %> <:actions> - <%= if(@image.user.email == @current_user.email) do %> + <%= if(@current_user && @image.user.email == @current_user.email) do %> <.link patch={~p"/images/#{@image.file}/show/edit"} phx-click={JS.push_focus()}> <.button>Edit image @@ -9,9 +9,13 @@ - + + + -<.back navigate={~p"/images"}>Back to images +<%= if(@current_user) do %> + <.back navigate={~p"/images"}>Back to images +<% end %> <.modal :if={@live_action == :edit} diff --git a/lib/zoeyscomputer_web/plugs/discord_plug.ex b/lib/zoeyscomputer_web/plugs/discord_plug.ex new file mode 100644 index 0000000..a26ed73 --- /dev/null +++ b/lib/zoeyscomputer_web/plugs/discord_plug.ex @@ -0,0 +1,55 @@ +defmodule ZoeyscomputerWeb.DiscordPlug do + alias ExAws.S3 + alias Zoeyscomputer.Images + import Plug.Conn + + def init(opts), do: opts + + def call(%Plug.Conn{path_info: ["images", id]} = conn, _opts) do + case Images.get_image_by!(id) do + nil -> + conn + |> send_resp(404, "Image not found") + |> halt() + + image -> + serve_s3_image(conn, image) + end + end + + def call(conn, _opts), do: conn + + defp serve_s3_image(conn, image) do + key = image.key + bucket = "imgs" + + case download_from_s3(bucket, key) do + {:ok, image_binary, content_type} -> + conn + |> put_resp_header("content-type", content_type) + |> send_resp(200, image_binary) + |> halt() + + {:error, _reason} -> + conn + |> send_resp(500, "Failed to retrieve image") + |> halt() + end + end + + defp download_from_s3(bucket, key) do + case S3.get_object(bucket, key) |> ExAws.request() do + {:ok, %{body: image_binary, headers: headers}} -> + content_type = + Enum.find_value(headers, fn + {"Content-Type", value} -> value + {"content-type", value} -> value + end) + + {:ok, image_binary, content_type || "application/octet-stream"} + + error -> + error + end + end +end diff --git a/lib/zoeyscomputer_web/router.ex b/lib/zoeyscomputer_web/router.ex index 8ca9371..3eede26 100644 --- a/lib/zoeyscomputer_web/router.ex +++ b/lib/zoeyscomputer_web/router.ex @@ -1,4 +1,5 @@ defmodule ZoeyscomputerWeb.Router do + alias ZoeyscomputerWeb.DiscordPlug use ZoeyscomputerWeb, :router import ZoeyscomputerWeb.UserAuth @@ -17,6 +18,10 @@ defmodule ZoeyscomputerWeb.Router do plug ZoeyscomputerWeb.Plugs.ApiAuthentication end + pipeline :discord do + plug DiscordPlug + end + pipeline :api do plug :accepts, ["json"] end @@ -80,6 +85,8 @@ defmodule ZoeyscomputerWeb.Router do live "/images/new", ImageLive.Index, :new live "/images/:id/edit", ImageLive.Index, :edit + live "/images", ImageLive.Index, :index + live "/images/:id/show/edit", ImageLive.Show, :edit live "/api-keys", ApiKeyLive.Index, :index @@ -101,9 +108,28 @@ defmodule ZoeyscomputerWeb.Router do live "/users/confirm/:token", UserConfirmationLive, :edit live "/users/confirm", UserConfirmationInstructionsLive, :new live "/", HomeLive, :index + end + end - live "/images", ImageLive.Index, :index - live "/images/:id", ImageLive.Show, :show + scope "/", ZoeyscomputerWeb do + pipe_through :check_discord + live "/images/:id", ImageController, :show + end + + def check_discord(conn, _opts) do + case get_req_header(conn, "user-agent") do + ["Discord" <> _rest | _] -> + conn + |> put_private(:phoenix_pipeline, {:doscord, []}) + |> DiscordPlug.call([]) + + ["Discord-Bot/" <> _rest | _] -> + conn + |> put_private(:phoenix_pipeline, {:discord, []}) + |> DiscordPlug.call([]) + + _ -> + conn |> put_private(:phoenix_pipeline, {:browser, []}) end end end