From 750d62cc7336dcd217adc00e7e20c317a4615a8f Mon Sep 17 00:00:00 2001 From: SphericalKat Date: Fri, 13 Aug 2021 07:46:10 +0530 Subject: [PATCH] feat(pastes): add support for url pastes Signed-off-by: SphericalKat --- assets/css/app.css | 4 ++++ lib/ketbin/pastes/utils.ex | 11 ++++++++++- lib/ketbin_web/controllers/page_controller.ex | 15 ++++++++++++--- lib/ketbin_web/templates/page/form.html.eex | 11 ++++++++++- lib/ketbin_web/templates/page/show.html.eex | 2 +- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/assets/css/app.css b/assets/css/app.css index b02fd29..6593486 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -22,6 +22,10 @@ header { background-color: #1a1a1a } +code { + font-family: 'JetBrains Mono', monospace; +} + @import "./phoenix.css"; /* Alerts and form errors */ diff --git a/lib/ketbin/pastes/utils.ex b/lib/ketbin/pastes/utils.ex index da8115e..d408c71 100644 --- a/lib/ketbin/pastes/utils.ex +++ b/lib/ketbin/pastes/utils.ex @@ -16,4 +16,13 @@ defmodule Ketbin.Pastes.Utils do Enum.map(0..length, fn i -> if Integer.mod(i, 2) == random, do: rand_consonant(), else: rand_vowel() end) |> List.to_string end -end + + def is_url?(url) do + try do + uri = URI.parse(url) + uri.scheme != nil && uri.host =~ "." && Enum.member?(["https", "http"], uri.scheme) + rescue + FunctionClauseError -> false + end + end +end \ No newline at end of file diff --git a/lib/ketbin_web/controllers/page_controller.ex b/lib/ketbin_web/controllers/page_controller.ex index f29262b..e17edcd 100644 --- a/lib/ketbin_web/controllers/page_controller.ex +++ b/lib/ketbin_web/controllers/page_controller.ex @@ -3,6 +3,7 @@ defmodule KetbinWeb.PageController do alias Ketbin.Pastes alias Ketbin.Pastes.Paste + alias Ketbin.Pastes.Utils def index(conn, _params) do changeset = Pastes.change_paste(%Paste{}) @@ -15,12 +16,20 @@ defmodule KetbinWeb.PageController do end def create(conn, %{"paste" => paste_params}) do - # paste_params = Map.put(paste_params, "id", s) + id = Utils.generate_key() + + is_url = + Map.get(paste_params, "content") + |> Utils.is_url?() + + paste_params = + Map.put(paste_params, "id", id) + |> Map.put("is_url", is_url) + case Pastes.create_paste(paste_params) do {:ok, paste} -> conn - |> put_flash(:info, "Paste created successfully.") - |> redirect(to: Routes.paste_path(conn, :show, paste)) + |> redirect(to: Routes.page_path(conn, :show, paste)) {:error, %Ecto.Changeset{} = changeset} -> render(conn, "index.html", changeset: changeset) diff --git a/lib/ketbin_web/templates/page/form.html.eex b/lib/ketbin_web/templates/page/form.html.eex index 8c0782b..be761f5 100644 --- a/lib/ketbin_web/templates/page/form.html.eex +++ b/lib/ketbin_web/templates/page/form.html.eex @@ -8,7 +8,16 @@
<%= textarea f, :content, [class: "w-full h-full px-6 py-4 outline-none bg-light-grey font-bold resize-none", placeholder: "> Paste, save, share! (Pasting just a URL will shorten it!)"] %>
- <%= submit "Save" %> +
<% end %> diff --git a/lib/ketbin_web/templates/page/show.html.eex b/lib/ketbin_web/templates/page/show.html.eex index e21465c..85ad57d 100644 --- a/lib/ketbin_web/templates/page/show.html.eex +++ b/lib/ketbin_web/templates/page/show.html.eex @@ -1,4 +1,4 @@ -
+
 	<%= @paste.content %>