Update uploader.ex
This commit is contained in:
parent
9a7d7af693
commit
3a53113284
1 changed files with 45 additions and 52 deletions
|
@ -5,67 +5,17 @@ defmodule Nulla.Uploader do
|
|||
@upload_base "priv/static"
|
||||
@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)
|
||||
ext = Path.extname(original_name)
|
||||
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
|
||||
|
||||
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 =
|
||||
Path.join([
|
||||
@upload_prefix,
|
||||
dirs,
|
||||
"media_attachments/files",
|
||||
partition_id(media_attachment_id),
|
||||
"original",
|
||||
filename
|
||||
|
@ -77,6 +27,16 @@ defmodule Nulla.Uploader do
|
|||
|
||||
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(%{
|
||||
id: media_attachment_id,
|
||||
type: type,
|
||||
|
@ -88,6 +48,39 @@ defmodule Nulla.Uploader do
|
|||
})
|
||||
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
|
||||
id
|
||||
|> Integer.to_string()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue