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,6 +1,8 @@
defmodule ZoeyscomputerWeb.ImageController do
use ZoeyscomputerWeb, :controller
import Mogrify
alias Zoeyscomputer.IdGenerator
alias Zoeyscomputer.Images
alias Zoeyscomputer.Images.Image
@ -59,12 +61,23 @@ defmodule ZoeyscomputerWeb.ImageController do
defp handle_upload(upload) do
extension = Path.extname(upload.filename)
id = IdGenerator.generate()
key = "uploads/#{id}#{extension}"
key = "uploads/#{id}.png"
bucket = "imgs"
local_path = "/tmp/#{id}#{extension}"
IO.puts("upload path: #{upload.path}")
{:ok, file_binary} = File.read(upload.path)
case ExAws.S3.put_object(bucket, key, file_binary)
File.write!(local_path, file_binary)
IO.puts("wrote to #{local_path}")
image = open(local_path) |> format("png") |> save()
IO.inspect(image)
{:ok, new_file_binary} = File.read(image.path)
case ExAws.S3.put_object(bucket, key, new_file_binary, %{content_type: "image/png"})
|> ExAws.request() do
{:ok, _response} -> {:ok, id}
{:error, reason} -> {:error, reason}

View file

@ -1,55 +0,0 @@
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

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