Update
This commit is contained in:
parent
987d32398b
commit
9a7d7af693
5 changed files with 78 additions and 34 deletions
|
@ -5,12 +5,7 @@ defmodule Nulla.Uploader do
|
||||||
@upload_base "priv/static"
|
@upload_base "priv/static"
|
||||||
@upload_prefix "system"
|
@upload_prefix "system"
|
||||||
|
|
||||||
def upload(
|
def upload(%Plug.Upload{path: temp_path, filename: original_name}, uploadType, name, domain) do
|
||||||
%Plug.Upload{path: temp_path, filename: original_name},
|
|
||||||
dir,
|
|
||||||
description,
|
|
||||||
domain
|
|
||||||
) do
|
|
||||||
{:ok, binary} = File.read(temp_path)
|
{:ok, binary} = File.read(temp_path)
|
||||||
ext = Path.extname(original_name)
|
ext = Path.extname(original_name)
|
||||||
mimetype = MIME.type(ext)
|
mimetype = MIME.type(ext)
|
||||||
|
@ -23,14 +18,54 @@ defmodule Nulla.Uploader do
|
||||||
true -> "Document"
|
true -> "Document"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
{width, height} =
|
||||||
|
cond do
|
||||||
|
mimetype =~ "image" or "video" ->
|
||||||
|
{output, 0} =
|
||||||
|
System.cmd("ffprobe", [
|
||||||
|
"-v",
|
||||||
|
"error",
|
||||||
|
"-select_streams",
|
||||||
|
"v:0",
|
||||||
|
"-show_entries",
|
||||||
|
"stream=width,height",
|
||||||
|
"-of",
|
||||||
|
"default=noprint_wrappers=1",
|
||||||
|
temp_path
|
||||||
|
])
|
||||||
|
|
||||||
|
parsed_width =
|
||||||
|
Regex.run(~r/width=(\d+)/, output)
|
||||||
|
|> List.last()
|
||||||
|
|> String.to_integer()
|
||||||
|
|
||||||
|
parsed_height =
|
||||||
|
Regex.run(~r/height=(\d+)/, output)
|
||||||
|
|> List.last()
|
||||||
|
|> String.to_integer()
|
||||||
|
|
||||||
|
{parsed_width, parsed_height}
|
||||||
|
|
||||||
|
true ->
|
||||||
|
{nil, nil}
|
||||||
|
end
|
||||||
|
|
||||||
filename = Base.encode16(:crypto.strong_rand_bytes(8), case: :lower) <> ext
|
filename = Base.encode16(:crypto.strong_rand_bytes(8), case: :lower) <> ext
|
||||||
|
|
||||||
media_attachment_id = Snowflake.next_id()
|
media_attachment_id = Snowflake.next_id()
|
||||||
|
|
||||||
|
dirs =
|
||||||
|
cond do
|
||||||
|
uploadType == :avatar -> "accounts/avatars"
|
||||||
|
uploadType == :header -> "accounts/headers"
|
||||||
|
uploadType == :attachment -> "media_attachments/files"
|
||||||
|
uploadType == :emoji -> "custom_emojis/images"
|
||||||
|
end
|
||||||
|
|
||||||
relative_path =
|
relative_path =
|
||||||
Path.join([
|
Path.join([
|
||||||
@upload_prefix,
|
@upload_prefix,
|
||||||
dir,
|
dirs,
|
||||||
partition_id(media_attachment_id),
|
partition_id(media_attachment_id),
|
||||||
"original",
|
"original",
|
||||||
filename
|
filename
|
||||||
|
@ -47,9 +82,9 @@ defmodule Nulla.Uploader do
|
||||||
type: type,
|
type: type,
|
||||||
mediaType: mimetype,
|
mediaType: mimetype,
|
||||||
url: "https://#{domain}/#{relative_path}",
|
url: "https://#{domain}/#{relative_path}",
|
||||||
name: description,
|
name: name,
|
||||||
width: 1,
|
width: width,
|
||||||
height: 1
|
height: height
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
<main>
|
||||||
|
<.flash_group flash={@flash} />
|
||||||
|
</main>
|
|
@ -1,4 +1,6 @@
|
||||||
<main class="flex min-h-screen items-center justify-center">
|
<main class="flex min-h-screen items-center justify-center">
|
||||||
|
<.flash_group flash={@flash} />
|
||||||
|
|
||||||
<form class="flex flex-col gap-2" action="/auth/sign_in" method="post">
|
<form class="flex flex-col gap-2" action="/auth/sign_in" method="post">
|
||||||
<input type="hidden" name="_csrf_token" value={get_csrf_token()} />
|
<input type="hidden" name="_csrf_token" value={get_csrf_token()} />
|
||||||
<label for="email">E-mail address *</label>
|
<label for="email">E-mail address *</label>
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
<main class="flex min-h-screen items-center justify-center">
|
<main class="flex min-h-screen items-center justify-center">
|
||||||
|
<.flash_group flash={@flash} />
|
||||||
|
|
||||||
<form class="flex flex-col gap-2" action="/auth" method="post">
|
<form class="flex flex-col gap-2" action="/auth" method="post">
|
||||||
<input type="hidden" name="_csrf_token" value={get_csrf_token()} />
|
<input type="hidden" name="_csrf_token" value={get_csrf_token()} />
|
||||||
<label for="username">Username *</label>
|
<label for="username">Username *</label>
|
||||||
|
|
|
@ -11,7 +11,7 @@ defmodule NullaWeb.AuthController do
|
||||||
if Argon2.verify_pass(password, user.password) do
|
if Argon2.verify_pass(password, user.password) do
|
||||||
conn
|
conn
|
||||||
|> put_session(:user_id, user.id)
|
|> put_session(:user_id, user.id)
|
||||||
|> redirect(to: "/")
|
|> redirect(to: ~p"/")
|
||||||
else
|
else
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Invalid login or password.")
|
|> put_flash(:error, "Invalid login or password.")
|
||||||
|
@ -28,37 +28,39 @@ defmodule NullaWeb.AuthController do
|
||||||
conn
|
conn
|
||||||
|> configure_session(drop: true)
|
|> configure_session(drop: true)
|
||||||
|> put_flash(:info, "You have been logged out.")
|
|> put_flash(:info, "You have been logged out.")
|
||||||
|> redirect(to: "/")
|
|> redirect(to: ~p"/")
|
||||||
end
|
end
|
||||||
|
|
||||||
def sign_up(conn, %{"username" => username, "email" => email, "password" => password}) do
|
def sign_up(conn, %{"username" => username, "email" => email, "password" => password}) do
|
||||||
instance_settings = InstanceSettings.get_instance_settings!()
|
instance_settings = InstanceSettings.get_instance_settings!()
|
||||||
|
|
||||||
if not instance_settings.registration do
|
if not instance_settings.registration do
|
||||||
redirect(conn, to: "/")
|
|
||||||
end
|
|
||||||
|
|
||||||
domain = instance_settings.domain
|
|
||||||
hashed_password = Argon2.hash_pwd_salt(password)
|
|
||||||
|
|
||||||
{publicKeyPem, privateKeyPem} = Nulla.KeyGen.gen()
|
|
||||||
|
|
||||||
with {:ok, actor} <- Actor.create_actor_minimal(username, domain, publicKeyPem),
|
|
||||||
{:ok, user} <-
|
|
||||||
User.create_user(%{
|
|
||||||
id: actor.id,
|
|
||||||
email: email,
|
|
||||||
password: hashed_password,
|
|
||||||
privateKeyPem: privateKeyPem,
|
|
||||||
last_active_at: DateTime.utc_now()
|
|
||||||
}) do
|
|
||||||
conn
|
conn
|
||||||
|> put_session(:user_id, user.id)
|
|> put_flash(:error, "Registration is disabled.")
|
||||||
|> put_flash(:info, "You're registred!")
|
|> redirect(to: ~p"/")
|
||||||
|> redirect(to: "/")
|
|
||||||
else
|
else
|
||||||
{:error, %Ecto.Changeset{} = changeset} ->
|
domain = instance_settings.domain
|
||||||
render(conn, "sign_up.html", changeset: changeset)
|
hashed_password = Argon2.hash_pwd_salt(password)
|
||||||
|
|
||||||
|
{publicKeyPem, privateKeyPem} = Nulla.KeyGen.gen()
|
||||||
|
|
||||||
|
with {:ok, actor} <- Actor.create_actor_minimal(username, domain, publicKeyPem),
|
||||||
|
{:ok, user} <-
|
||||||
|
User.create_user(%{
|
||||||
|
id: actor.id,
|
||||||
|
email: email,
|
||||||
|
password: hashed_password,
|
||||||
|
privateKeyPem: privateKeyPem,
|
||||||
|
last_active_at: DateTime.utc_now()
|
||||||
|
}) do
|
||||||
|
conn
|
||||||
|
|> put_session(:user_id, user.id)
|
||||||
|
|> put_flash(:info, "You're registred!")
|
||||||
|
|> redirect(to: ~p"/")
|
||||||
|
else
|
||||||
|
{:error, %Ecto.Changeset{} = changeset} ->
|
||||||
|
render(conn, "sign_up.html", changeset: changeset)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue