This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
bdfr-browser/lib/bdfr_browser/http/plug.ex

133 lines
3.6 KiB
Elixir
Raw Normal View History

2023-05-17 22:13:55 +00:00
defmodule BdfrBrowser.HTTP.Plug do
use Plug.Router
2023-08-13 23:18:55 +00:00
alias BdfrBrowser.{Repo, Post, Subreddit}
2023-05-17 22:13:55 +00:00
plug :match
plug :dispatch
get "/" do
tpl_params = [
2023-08-13 23:18:55 +00:00
subreddits: Subreddit.names() |> Repo.all()
2023-05-17 22:13:55 +00:00
]
tpl_file = Application.app_dir(:bdfr_browser, "priv/templates/http/index.eex")
content = EEx.eval_file(tpl_file, tpl_params)
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_resp(200, content)
end
get "/r/:subreddit" do
2023-08-13 23:18:55 +00:00
subreddit_record = Repo.get_by(Subreddit, name: subreddit)
2023-05-17 22:13:55 +00:00
tpl_params = [
subreddit: subreddit,
2023-08-13 23:18:55 +00:00
dates: subreddit_record |> Post.date_listing() |> Repo.all()
2023-05-17 22:13:55 +00:00
]
tpl_file = Application.app_dir(:bdfr_browser, "priv/templates/http/subreddit.eex")
content = EEx.eval_file(tpl_file, tpl_params)
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_resp(200, content)
end
get "/r/:subreddit/:date" do
2023-08-13 23:18:55 +00:00
subreddit_record = Repo.get_by(Subreddit, name: subreddit)
2023-05-17 22:13:55 +00:00
tpl_params = [
subreddit: subreddit,
date: date,
2023-08-13 23:18:55 +00:00
posts: subreddit_record |> Post.during_month(date) |> Repo.all()
2023-05-17 22:13:55 +00:00
]
tpl_file = Application.app_dir(:bdfr_browser, "priv/templates/http/subreddit_posts.eex")
content = EEx.eval_file(tpl_file, tpl_params)
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_resp(200, content)
end
2023-08-13 23:18:55 +00:00
get "/r/:subreddit/:date/:id" do
post_record = Post |> Repo.get(id) |> Repo.preload(comments: :children)
2023-05-17 22:13:55 +00:00
tpl_params = [
subreddit: subreddit,
date: date,
2023-08-13 23:18:55 +00:00
post: post_record,
media: post_media(post_record.filename, paths: [subreddit, date]),
2023-05-17 22:13:55 +00:00
comment_template: Application.app_dir(:bdfr_browser, "priv/templates/http/_comment.eex")
]
tpl_file = Application.app_dir(:bdfr_browser, "priv/templates/http/post.eex")
content = EEx.eval_file(tpl_file, tpl_params)
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_resp(200, content)
end
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()
conn
|> put_resp_header("content-type", mime_from_ext(joined_path))
|> send_resp(200, media)
end
2023-08-13 23:18:55 +00:00
get "/_import" do
:ok = BdfrBrowser.Importer.background_import()
send_resp(conn, 200, "IMPORTING")
2023-05-17 22:13:55 +00:00
end
2023-08-13 23:18:55 +00:00
get "/_ping" do
send_resp(conn, 200, "PONG")
2023-05-17 22:13:55 +00:00
end
2023-08-13 23:18:55 +00:00
match _ do
send_resp(conn, 404, "Not Found")
2023-05-25 20:07:28 +00:00
end
2023-08-13 23:18:55 +00:00
# Helper
2023-05-17 22:13:55 +00:00
defp post_media(post, args) do
base_directory = Application.fetch_env!(:bdfr_browser, :base_directory)
post_dir = Path.join([base_directory | Keyword.fetch!(args, :paths)])
post_img = "#{post}*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}"
post_vid = "#{post}*.{mp4,MP4}"
%{
images: [post_dir, post_img] |> Path.join() |> Path.wildcard() |> Enum.map(&media_path/1),
videos: [post_dir, post_vid] |> Path.join() |> Path.wildcard() |> Enum.map(&media_path/1)
}
end
defp media_path(full_path) do
base_directory = Application.fetch_env!(:bdfr_browser, :base_directory)
2023-05-18 18:20:45 +00:00
full_path
|> String.replace("#{base_directory}/", "/media/")
|> String.split("/")
|> Enum.map(fn p -> URI.encode(p, &URI.char_unreserved?/1) end)
|> Enum.join("/")
2023-05-17 22:13:55 +00:00
end
defp mime_from_ext(path) do
normalized_path = String.downcase(path)
case Path.extname(normalized_path) do
".jpg" -> "image/jpeg"
".jpeg" -> "image/jpeg"
".png" -> "image/png"
".gif" -> "image/gif"
".mp4" -> "video/mp4"
end
end
end