From f9102c4c276f1b48c7070dd28dfa6d5b7df33e88 Mon Sep 17 00:00:00 2001 From: Daniel Kempkens Date: Tue, 15 Aug 2023 13:06:25 +0200 Subject: [PATCH] feat: Include images for certain chat messages --- lib/bdfr_browser/http/plug.ex | 30 ++++++++++++++++++++++++++---- priv/templates/http/chat.eex | 2 +- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/bdfr_browser/http/plug.ex b/lib/bdfr_browser/http/plug.ex index 2c79592..4e4b282 100644 --- a/lib/bdfr_browser/http/plug.ex +++ b/lib/bdfr_browser/http/plug.ex @@ -105,11 +105,33 @@ defmodule BdfrBrowser.HTTP.Plug do get "/media/*path" do base_directory = Application.fetch_env!(:bdfr_browser, :base_directory) joined_path = Path.join(path) - {:ok, media} = [base_directory, joined_path] |> Path.join() |> File.read() + file_path = Path.join([base_directory, joined_path]) - conn - |> put_resp_header("content-type", mime_from_ext(joined_path)) - |> send_resp(200, media) + if File.exists?(file_path) do + {:ok, media} = File.read(file_path) + + conn + |> put_resp_header("content-type", mime_from_ext(file_path)) + |> send_resp(200, media) + else + send_resp(conn, 404, "Not Found") + end + end + + get "/chat_media/*path" do + chat_directory = Application.fetch_env!(:bdfr_browser, :chat_directory) + joined_path = Path.join(path) + file_path = Path.join([chat_directory, "images", joined_path]) + + if File.exists?(file_path) do + {:ok, media} = File.read(file_path) + + conn + |> put_resp_header("content-type", mime_from_ext(file_path)) + |> send_resp(200, media) + else + send_resp(conn, 404, "Not Found") + end end post "/_import" do diff --git a/priv/templates/http/chat.eex b/priv/templates/http/chat.eex index 44c84e0..b560641 100644 --- a/priv/templates/http/chat.eex +++ b/priv/templates/http/chat.eex @@ -5,7 +5,7 @@
- <%= Earmark.as_html!(message.message) %> + <%= BdfrBrowser.RenderUtils.message(message.message) %>