fix images

This commit is contained in:
zack 2024-10-25 17:17:56 -04:00
parent d7559647e2
commit 3b63d75219
No known key found for this signature in database
GPG key ID: 5F873416BCF59F35
7 changed files with 85 additions and 76 deletions

View file

@ -1,4 +1,6 @@
defmodule ZoeyscomputerWeb.Router do
alias ExAws.S3
alias Zoeyscomputer.Images
alias ZoeyscomputerWeb.DiscordPlug
use ZoeyscomputerWeb, :router
@ -11,6 +13,7 @@ defmodule ZoeyscomputerWeb.Router do
plug :put_root_layout, html: {ZoeyscomputerWeb.Layouts, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
plug :handle_discord
plug :fetch_current_user
end
@ -18,10 +21,6 @@ defmodule ZoeyscomputerWeb.Router do
plug ZoeyscomputerWeb.Plugs.ApiAuthentication
end
pipeline :discord do
plug DiscordPlug
end
pipeline :api do
plug :accepts, ["json"]
end
@ -108,28 +107,62 @@ defmodule ZoeyscomputerWeb.Router do
live "/users/confirm/:token", UserConfirmationLive, :edit
live "/users/confirm", UserConfirmationInstructionsLive, :new
live "/", HomeLive, :index
live "/images/:id", ImageLive.Show, :show
end
end
scope "/", ZoeyscomputerWeb do
pipe_through :check_discord
live "/images/:id", ImageController, :show
pipe_through [:browser, :require_authenticated_user]
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([])
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
_ -> nil
end)
["Discord-Bot/" <> _rest | _] ->
conn
|> put_private(:phoenix_pipeline, {:discord, []})
|> DiscordPlug.call([])
{:ok, image_binary, content_type || "application/octet-stream"}
_ ->
conn |> put_private(:phoenix_pipeline, {:browser, []})
error ->
error
end
end
# Updated plug to return ID as string for Discord requests
def handle_discord(conn, _opts) do
is_discord =
case get_req_header(conn, "user-agent") do
["Discord" <> _rest | _] -> true
["Discord-Bot/" <> _rest | _] -> true
_ -> false
end
if true do
# Extract the ID from the path
id = List.last(conn.path_info)
case download_from_s3("imgs", "uploads/#{id}.png") do
{:ok, image_binary, content_type} ->
conn
|> put_resp_content_type(content_type)
|> send_resp(200, image_binary)
|> halt()
{:error, reason} ->
IO.puts(reason)
conn
|> put_resp_content_type("text/plain")
|> send_resp(500, "failed to retrieve image")
|> halt()
end
else
conn
end
end
end