fix some bugs n stuff

This commit is contained in:
zack 2024-10-24 20:22:37 -04:00
parent b925726977
commit d7559647e2
No known key found for this signature in database
GPG key ID: 5F873416BCF59F35
7 changed files with 90 additions and 21 deletions

View file

@ -22,10 +22,6 @@ config :zoeyscomputer, ZoeyscomputerWeb.Endpoint,
pubsub_server: Zoeyscomputer.PubSub, pubsub_server: Zoeyscomputer.PubSub,
live_view: [signing_salt: "uUcDyRmg"] live_view: [signing_salt: "uUcDyRmg"]
config :nanoid,
alphabet: "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
size: 7
# Configures the mailer # Configures the mailer
# #
# By default it uses the "Local" adapter which stores the emails # By default it uses the "Local" adapter which stores the emails

View file

@ -52,10 +52,6 @@ config :zoeyscomputer, ZoeyscomputerWeb.Endpoint,
# configured to run both http and https servers on # configured to run both http and https servers on
# different ports # different ports
config :nanoid,
size: 7,
alphabet: "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
# Watch static and templates for browser reloading. # Watch static and templates for browser reloading.
config :zoeyscomputer, ZoeyscomputerWeb.Endpoint, config :zoeyscomputer, ZoeyscomputerWeb.Endpoint,
live_reload: [ live_reload: [

View file

@ -17,9 +17,5 @@ config :swoosh, local: false
# Do not print debug messages in production # Do not print debug messages in production
config :logger, level: :info config :logger, level: :info
config :nanoid,
size: 7,
alphabet: "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
# Runtime production configuration, including reading # Runtime production configuration, including reading
# of environment variables, is done on config/runtime.exs. # of environment variables, is done on config/runtime.exs.

View file

@ -65,10 +65,6 @@ if config_env() == :prod do
], ],
secret_key_base: secret_key_base secret_key_base: secret_key_base
config :nanoid,
alphabet: "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
size: 7
# ## SSL Support # ## SSL Support
# #
# To get SSL working, you will need to add the `https` key # To get SSL working, you will need to add the `https` key

View file

@ -1,7 +1,7 @@
<.header> <.header>
uploaded by <%= @image.user.email %> uploaded by <%= @image.user.email %>
<:actions> <: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()}> <.link patch={~p"/images/#{@image.file}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit image</.button> <.button>Edit image</.button>
</.link> </.link>
@ -9,9 +9,13 @@
</:actions> </:actions>
</.header> </.header>
<img src={"https://s3.zoeys.computer/imgs/uploads/#{@image.file}.png"} /> <a target="_blank" href={"https://s3.zoeys.computer/imgs/uploads/#{@image.file}.png"}>
<img src={"https://s3.zoeys.computer/imgs/uploads/#{@image.file}.png"} />
</a>
<.back navigate={~p"/images"}>Back to images</.back> <%= if(@current_user) do %>
<.back navigate={~p"/images"}>Back to images</.back>
<% end %>
<.modal <.modal
:if={@live_action == :edit} :if={@live_action == :edit}

View file

@ -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

View file

@ -1,4 +1,5 @@
defmodule ZoeyscomputerWeb.Router do defmodule ZoeyscomputerWeb.Router do
alias ZoeyscomputerWeb.DiscordPlug
use ZoeyscomputerWeb, :router use ZoeyscomputerWeb, :router
import ZoeyscomputerWeb.UserAuth import ZoeyscomputerWeb.UserAuth
@ -17,6 +18,10 @@ defmodule ZoeyscomputerWeb.Router do
plug ZoeyscomputerWeb.Plugs.ApiAuthentication plug ZoeyscomputerWeb.Plugs.ApiAuthentication
end end
pipeline :discord do
plug DiscordPlug
end
pipeline :api do pipeline :api do
plug :accepts, ["json"] plug :accepts, ["json"]
end end
@ -80,6 +85,8 @@ defmodule ZoeyscomputerWeb.Router do
live "/images/new", ImageLive.Index, :new live "/images/new", ImageLive.Index, :new
live "/images/:id/edit", ImageLive.Index, :edit live "/images/:id/edit", ImageLive.Index, :edit
live "/images", ImageLive.Index, :index
live "/images/:id/show/edit", ImageLive.Show, :edit live "/images/:id/show/edit", ImageLive.Show, :edit
live "/api-keys", ApiKeyLive.Index, :index live "/api-keys", ApiKeyLive.Index, :index
@ -101,9 +108,28 @@ defmodule ZoeyscomputerWeb.Router do
live "/users/confirm/:token", UserConfirmationLive, :edit live "/users/confirm/:token", UserConfirmationLive, :edit
live "/users/confirm", UserConfirmationInstructionsLive, :new live "/users/confirm", UserConfirmationInstructionsLive, :new
live "/", HomeLive, :index live "/", HomeLive, :index
end
end
live "/images", ImageLive.Index, :index scope "/", ZoeyscomputerWeb do
live "/images/:id", ImageLive.Show, :show 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 end
end end