Update uploader.ex

This commit is contained in:
Mirai Kumiko 2025-06-21 20:02:02 +02:00
parent 9a7d7af693
commit 3a53113284
Signed by: miraikumiko
GPG key ID: 3F178B1B5E0CB278

View file

@ -5,67 +5,17 @@ defmodule Nulla.Uploader do
@upload_base "priv/static" @upload_base "priv/static"
@upload_prefix "system" @upload_prefix "system"
def upload(%Plug.Upload{path: temp_path, filename: original_name}, uploadType, name, domain) do def upload(%Plug.Upload{path: temp_path, filename: original_name}, domain, name) 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)
type =
cond do
mimetype =~ "image" -> "Image"
mimetype =~ "video" -> "Video"
mimetype =~ "audio" -> "Audio"
true -> "Document"
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,
dirs, "media_attachments/files",
partition_id(media_attachment_id), partition_id(media_attachment_id),
"original", "original",
filename filename
@ -77,6 +27,16 @@ defmodule Nulla.Uploader do
File.write!(full_path, binary) File.write!(full_path, binary)
type =
cond do
mimetype =~ "image" -> "Image"
mimetype =~ "video" -> "Video"
mimetype =~ "audio" -> "Audio"
true -> "Document"
end
{width, height} = get_width_and_height(temp_path, mimetype)
MediaAttachment.create_media_attachment(%{ MediaAttachment.create_media_attachment(%{
id: media_attachment_id, id: media_attachment_id,
type: type, type: type,
@ -88,6 +48,39 @@ defmodule Nulla.Uploader do
}) })
end end
defp get_width_and_height(path, mimetype) do
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",
path
])
width =
Regex.run(~r/width=(\d+)/, output)
|> List.last()
|> String.to_integer()
height =
Regex.run(~r/height=(\d+)/, output)
|> List.last()
|> String.to_integer()
{width, height}
true ->
{nil, nil}
end
end
defp partition_id(id) do defp partition_id(id) do
id id
|> Integer.to_string() |> Integer.to_string()