pleroma/lib/pleroma/mime.ex

121 lines
3.2 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2018-12-31 10:41:47 -05:00
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2018-11-29 15:11:45 -05:00
defmodule Pleroma.MIME do
@moduledoc """
2018-11-30 11:56:28 -05:00
Returns the mime-type of a binary and optionally a normalized file-name.
2018-11-29 15:11:45 -05:00
"""
@default "application/octet-stream"
2018-12-02 16:22:19 -05:00
@read_bytes 35
2018-11-29 15:11:45 -05:00
2020-02-25 09:34:56 -05:00
@spec file_mime_type(String.t(), String.t()) ::
2018-11-29 15:11:45 -05:00
{:ok, content_type :: String.t(), filename :: String.t()} | {:error, any()} | :error
def file_mime_type(path, filename) do
with {:ok, content_type} <- file_mime_type(path),
filename <- fix_extension(filename, content_type) do
{:ok, content_type, filename}
end
end
@spec file_mime_type(String.t()) :: {:ok, String.t()} | {:error, any()} | :error
def file_mime_type(filename) do
File.open(filename, [:read], fn f ->
2018-11-30 11:56:28 -05:00
check_mime_type(IO.binread(f, @read_bytes))
2018-11-29 15:11:45 -05:00
end)
end
def bin_mime_type(binary, filename) do
with {:ok, content_type} <- bin_mime_type(binary),
filename <- fix_extension(filename, content_type) do
{:ok, content_type, filename}
end
end
@spec bin_mime_type(binary()) :: {:ok, String.t()} | :error
2018-11-30 11:56:28 -05:00
def bin_mime_type(<<head::binary-size(@read_bytes), _::binary>>) do
2018-11-29 15:11:45 -05:00
{:ok, check_mime_type(head)}
end
def bin_mime_type(_), do: :error
2018-12-09 04:12:48 -05:00
def mime_type(<<_::binary>>), do: {:ok, @default}
2018-11-29 15:11:45 -05:00
defp fix_extension(filename, content_type) do
parts = String.split(filename, ".")
new_filename =
if length(parts) > 1 do
Enum.drop(parts, -1) |> Enum.join(".")
else
Enum.join(parts)
end
cond do
content_type == "application/octet-stream" ->
filename
ext = List.first(MIME.extensions(content_type)) ->
new_filename <> "." <> ext
true ->
Enum.join([new_filename, String.split(content_type, "/") |> List.last()], ".")
end
end
2018-11-30 11:56:28 -05:00
defp check_mime_type(<<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, _::binary>>) do
2018-11-29 15:11:45 -05:00
"image/png"
end
2018-11-30 11:56:28 -05:00
defp check_mime_type(<<0x47, 0x49, 0x46, 0x38, _, 0x61, _::binary>>) do
2018-11-29 15:11:45 -05:00
"image/gif"
end
2018-11-30 11:56:28 -05:00
defp check_mime_type(<<0xFF, 0xD8, 0xFF, _::binary>>) do
2018-11-29 15:11:45 -05:00
"image/jpeg"
end
2018-11-30 11:56:28 -05:00
defp check_mime_type(<<0x1A, 0x45, 0xDF, 0xA3, _::binary>>) do
2018-11-29 15:11:45 -05:00
"video/webm"
end
2018-11-30 11:56:28 -05:00
defp check_mime_type(<<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70, _::binary>>) do
2018-11-29 15:11:45 -05:00
"video/mp4"
end
2018-11-30 11:56:28 -05:00
defp check_mime_type(<<0x49, 0x44, 0x33, _::binary>>) do
2018-11-29 15:11:45 -05:00
"audio/mpeg"
end
2018-11-30 11:56:28 -05:00
defp check_mime_type(<<255, 251, _, 68, 0, 0, 0, 0, _::binary>>) do
2018-11-29 15:11:45 -05:00
"audio/mpeg"
end
2018-11-30 11:56:28 -05:00
defp check_mime_type(
<<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, _::size(160), 0x80, 0x74, 0x68, 0x65,
0x6F, 0x72, 0x61, _::binary>>
) do
"video/ogg"
end
defp check_mime_type(<<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, _::binary>>) do
2018-11-29 15:11:45 -05:00
"audio/ogg"
end
defp check_mime_type(<<"RIFF", _::binary-size(4), "WAVE", _::binary>>) do
2018-11-29 15:11:45 -05:00
"audio/wav"
end
defp check_mime_type(<<"RIFF", _::binary-size(4), "WEBP", _::binary>>) do
"image/webp"
end
defp check_mime_type(<<"RIFF", _::binary-size(4), "AVI.", _::binary>>) do
"video/avi"
end
2018-11-29 15:11:45 -05:00
defp check_mime_type(_) do
@default
end
end