Merge branch 'fix-detect-file-mime-without-filename' into 'master'

Fix detecting file MIME type if file hasn't got a name

See merge request framasoft/mobilizon!661
This commit is contained in:
Thomas Citharel 2020-10-27 16:59:41 +01:00
commit e2b70bb4e4

View File

@ -14,7 +14,7 @@ defmodule Mobilizon.Web.Upload.MIME do
{:ok, content_type :: String.t(), filename :: String.t()} | {:error, any()} | :error {:ok, content_type :: String.t(), filename :: String.t()} | {:error, any()} | :error
def file_mime_type(path, filename) do def file_mime_type(path, filename) do
with {:ok, content_type} <- file_mime_type(path), with {:ok, content_type} <- file_mime_type(path),
filename <- fix_extension(filename, content_type) do filename when is_binary(filename) <- fix_extension(filename, content_type) do
{:ok, content_type, filename} {:ok, content_type, filename}
end end
end end
@ -42,7 +42,8 @@ defmodule Mobilizon.Web.Upload.MIME do
def mime_type(<<_::binary>>), do: {:ok, @default} def mime_type(<<_::binary>>), do: {:ok, @default}
defp fix_extension(filename, content_type) do defp fix_extension(filename, content_type)
when is_binary(filename) and is_binary(content_type) do
parts = String.split(filename, ".") parts = String.split(filename, ".")
new_filename = new_filename =
@ -66,6 +67,8 @@ defmodule Mobilizon.Web.Upload.MIME do
end end
end end
defp fix_extension(_, _), do: :error
defp check_mime_type(<<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, _::binary>>) do defp check_mime_type(<<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, _::binary>>) do
"image/png" "image/png"
end end