fix some bugs n stuff
This commit is contained in:
parent
b925726977
commit
d7559647e2
7 changed files with 90 additions and 21 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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: [
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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</.button>
|
||||
</.link>
|
||||
|
|
@ -9,9 +9,13 @@
|
|||
</:actions>
|
||||
</.header>
|
||||
|
||||
<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>
|
||||
|
||||
<%= if(@current_user) do %>
|
||||
<.back navigate={~p"/images"}>Back to images</.back>
|
||||
<% end %>
|
||||
|
||||
<.modal
|
||||
:if={@live_action == :edit}
|
||||
|
|
|
|||
55
lib/zoeyscomputer_web/plugs/discord_plug.ex
Normal file
55
lib/zoeyscomputer_web/plugs/discord_plug.ex
Normal 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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue