feat(pastes): add support for url pastes
Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
parent
1b54edf7b3
commit
750d62cc73
@ -22,6 +22,10 @@ header {
|
||||
background-color: #1a1a1a
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: 'JetBrains Mono', monospace;
|
||||
}
|
||||
|
||||
@import "./phoenix.css";
|
||||
|
||||
/* Alerts and form errors */
|
||||
|
@ -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
|
@ -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)
|
||||
|
@ -8,7 +8,16 @@
|
||||
<div>
|
||||
<%= 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!)"] %>
|
||||
<div class="absolute top-0 right-0 p-4">
|
||||
<%= submit "Save" %>
|
||||
<button type="submit">
|
||||
<svg
|
||||
class="h-6 w-6 cursor-pointer fill-current text-white hover:text-amber"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path d="M17.6 3.6c-.4-.4-.9-.6-1.4-.6H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V7.8c0-.5-.2-1-.6-1.4l-2.8-2.8zM12 19c-1.7 0-3-1.3-3-3s1.3-3 3-3 3 1.3 3 3-1.3 3-3 3zm1-10H7c-1.1 0-2-.9-2-2s.9-2 2-2h6c1.1 0 2 .9 2 2s-.9 2-2 2z"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
@ -1,4 +1,4 @@
|
||||
|
||||
<pre class="py-6">
|
||||
<pre class="py-6 font-bold">
|
||||
<code><%= @paste.content %></code>
|
||||
</pre>
|
||||
|
Loading…
Reference in New Issue
Block a user