2021-08-09 08:48:23 +00:00
|
|
|
defmodule KetbinWeb.PageController do
|
2021-08-14 21:49:03 +00:00
|
|
|
require Logger
|
|
|
|
|
2021-08-09 08:48:23 +00:00
|
|
|
use KetbinWeb, :controller
|
|
|
|
|
2021-08-13 00:17:13 +00:00
|
|
|
alias Ketbin.Pastes
|
|
|
|
alias Ketbin.Pastes.Paste
|
2021-08-13 02:16:10 +00:00
|
|
|
alias Ketbin.Pastes.Utils
|
2021-08-13 00:17:13 +00:00
|
|
|
|
2021-08-09 08:48:23 +00:00
|
|
|
def index(conn, _params) do
|
2021-08-13 01:03:43 +00:00
|
|
|
changeset = Pastes.change_paste(%Paste{})
|
|
|
|
render(conn, "index.html", changeset: changeset)
|
2021-08-09 08:48:23 +00:00
|
|
|
end
|
2021-08-13 00:17:13 +00:00
|
|
|
|
2021-08-14 23:16:06 +00:00
|
|
|
def show(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do
|
2021-08-16 20:59:24 +00:00
|
|
|
[head | tail] = String.split(id, ".")
|
|
|
|
|
2021-08-16 08:23:22 +00:00
|
|
|
# fetch paste from db
|
2021-08-16 20:59:24 +00:00
|
|
|
paste = Pastes.get_paste!(head)
|
2021-08-13 02:24:07 +00:00
|
|
|
|
2021-08-16 08:23:22 +00:00
|
|
|
# paste is a url, redirect
|
|
|
|
# regular paste, show content
|
|
|
|
if paste.is_url do
|
2021-08-13 02:24:07 +00:00
|
|
|
redirect(conn, external: paste.content)
|
2021-08-16 08:23:22 +00:00
|
|
|
else
|
2021-10-07 00:28:00 +00:00
|
|
|
render(conn, "show.html",
|
|
|
|
paste: paste,
|
|
|
|
show_edit: show_edit,
|
|
|
|
extension: List.first(tail) || ""
|
|
|
|
)
|
2021-08-13 02:24:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-14 23:16:06 +00:00
|
|
|
def showlink(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do
|
2021-08-16 20:59:24 +00:00
|
|
|
[head | tail] = String.split(id, ".")
|
|
|
|
paste = Pastes.get_paste!(head)
|
2021-10-07 00:28:00 +00:00
|
|
|
|
|
|
|
render(conn, "show.html",
|
|
|
|
paste: paste,
|
|
|
|
show_edit: show_edit,
|
|
|
|
extension: if(tail == [], do: "", else: tail)
|
|
|
|
)
|
2021-08-13 00:17:13 +00:00
|
|
|
end
|
2021-08-13 01:03:43 +00:00
|
|
|
|
2021-08-14 09:49:16 +00:00
|
|
|
def raw(conn, %{"id" => id}) do
|
|
|
|
paste = Pastes.get_paste!(id)
|
|
|
|
text(conn, paste.content)
|
|
|
|
end
|
|
|
|
|
2021-08-14 23:16:06 +00:00
|
|
|
def create(%{assigns: %{current_user: current_user}} = conn, %{"paste" => paste_params}) do
|
2021-08-13 06:43:59 +00:00
|
|
|
# generate phonetic key
|
2021-08-13 02:16:10 +00:00
|
|
|
id = Utils.generate_key()
|
|
|
|
|
2021-08-13 06:43:59 +00:00
|
|
|
# check if content is a url
|
2021-08-13 02:16:10 +00:00
|
|
|
is_url =
|
|
|
|
Map.get(paste_params, "content")
|
|
|
|
|> Utils.is_url?()
|
|
|
|
|
2021-08-13 06:43:59 +00:00
|
|
|
# put id and is_url values into changeset
|
2021-08-13 02:16:10 +00:00
|
|
|
paste_params =
|
|
|
|
Map.put(paste_params, "id", id)
|
|
|
|
|> Map.put("is_url", is_url)
|
2021-08-14 22:21:31 +00:00
|
|
|
|> Map.put("belongs_to", current_user && current_user.id)
|
2021-08-13 02:16:10 +00:00
|
|
|
|
2021-08-13 06:43:59 +00:00
|
|
|
# attempt to create a paste
|
2021-08-13 01:03:43 +00:00
|
|
|
case Pastes.create_paste(paste_params) do
|
2021-08-16 08:23:22 +00:00
|
|
|
# all good, redirect
|
|
|
|
{:ok, paste} ->
|
2021-08-13 02:24:07 +00:00
|
|
|
unless is_url do
|
|
|
|
conn
|
2021-08-16 08:23:22 +00:00
|
|
|
# is a regular paste, take to regular route
|
|
|
|
|> redirect(to: Routes.page_path(conn, :show, paste))
|
2021-08-13 02:24:07 +00:00
|
|
|
else
|
|
|
|
conn
|
2021-08-16 08:23:22 +00:00
|
|
|
# is a url, take to route with /v/ prefix
|
|
|
|
|> redirect(to: Routes.page_path(conn, :showlink, paste))
|
2021-08-13 02:24:07 +00:00
|
|
|
end
|
2021-08-13 01:03:43 +00:00
|
|
|
|
2021-08-16 08:23:22 +00:00
|
|
|
# something went wrong, bail
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
2021-08-13 01:03:43 +00:00
|
|
|
render(conn, "index.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
2021-08-14 22:21:31 +00:00
|
|
|
|
|
|
|
def edit(conn, %{"id" => id}) do
|
|
|
|
paste = Pastes.get_paste!(id)
|
|
|
|
changeset = Pastes.change_paste(paste)
|
|
|
|
render(conn, "edit.html", paste: paste, changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"id" => id, "paste" => paste_params}) do
|
|
|
|
paste = Pastes.get_paste!(id)
|
|
|
|
|
2021-08-16 08:23:22 +00:00
|
|
|
# check if content is a url
|
|
|
|
is_url =
|
|
|
|
Map.get(paste_params, "content")
|
|
|
|
|> Utils.is_url?()
|
|
|
|
|
|
|
|
paste_params = Map.put(paste_params, "is_url", is_url)
|
|
|
|
|
2021-08-14 22:21:31 +00:00
|
|
|
case Pastes.update_paste(paste, paste_params) do
|
|
|
|
{:ok, paste} ->
|
2021-08-16 08:23:22 +00:00
|
|
|
unless is_url do
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Paste updated successfully.")
|
|
|
|
|> redirect(to: Routes.page_path(conn, :show, paste))
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Paste updated successfully.")
|
|
|
|
|> redirect(to: Routes.page_path(conn, :showlink, paste))
|
|
|
|
end
|
2021-08-14 22:21:31 +00:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
render(conn, "edit.html", paste: paste, changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
2021-08-09 08:48:23 +00:00
|
|
|
end
|