2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-29 20:11:45 +00:00
|
|
|
defmodule Pleroma.MIME do
|
|
|
|
@moduledoc """
|
2018-11-30 16:56:28 +00:00
|
|
|
Returns the mime-type of a binary and optionally a normalized file-name.
|
2018-11-29 20:11:45 +00:00
|
|
|
"""
|
2018-12-02 21:22:19 +00:00
|
|
|
@read_bytes 35
|
2020-05-14 19:36:31 +00:00
|
|
|
@pool __MODULE__.GenMagicPool
|
|
|
|
|
|
|
|
def child_spec(_) do
|
|
|
|
pool_size = Pleroma.Config.get!([:gen_magic_pool, :size])
|
|
|
|
name = @pool
|
|
|
|
|
|
|
|
%{
|
|
|
|
id: __MODULE__,
|
|
|
|
start: {GenMagic.Pool, :start_link, [[name: name, pool_size: pool_size]]},
|
|
|
|
type: :worker,
|
|
|
|
restart: :permanent,
|
|
|
|
shutdown: 500
|
|
|
|
}
|
|
|
|
end
|
2018-11-29 20:11:45 +00:00
|
|
|
|
2020-02-25 14:34:56 +00:00
|
|
|
@spec file_mime_type(String.t(), String.t()) ::
|
2018-11-29 20:11:45 +00: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
|
2020-05-14 19:36:31 +00:00
|
|
|
case GenMagic.Pool.perform(@pool, filename) do
|
|
|
|
{:ok, %GenMagic.Result{mime_type: content_type}} -> {:ok, content_type}
|
|
|
|
error -> error
|
|
|
|
end
|
2018-11-29 20:11:45 +00:00
|
|
|
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 16:56:28 +00:00
|
|
|
def bin_mime_type(<<head::binary-size(@read_bytes), _::binary>>) do
|
2020-05-14 19:36:31 +00:00
|
|
|
case GenMagic.Pool.perform(@pool, {:bytes, head}) do
|
|
|
|
{:ok, %GenMagic.Result{mime_type: content_type}} -> {:ok, content_type}
|
|
|
|
error -> error
|
|
|
|
end
|
2018-11-29 20:11:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def bin_mime_type(_), do: :error
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|