Work around inability to change format of files without an extension

This commit is contained in:
Mark Felder 2021-02-15 21:01:04 +00:00
parent 43bde2d4e7
commit 05c96187e6

View File

@ -6,8 +6,6 @@ defmodule Pleroma.Upload.Filter.Heif do
@behaviour Pleroma.Upload.Filter @behaviour Pleroma.Upload.Filter
alias Pleroma.Upload alias Pleroma.Upload
require Logger
@type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()} @type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()}
@type conversions :: conversion() | [conversion()] @type conversions :: conversion() | [conversion()]
@ -24,18 +22,26 @@ defmodule Pleroma.Upload.Filter.Heif do
{:ok, :filtered, %Upload{upload | name: name, path: path, content_type: "image/jpeg"}} {:ok, :filtered, %Upload{upload | name: name, path: path, content_type: "image/jpeg"}}
rescue rescue
e in ErlangError -> e in ErlangError ->
{:error, "mogrify command not found: #{inspect(e)}"} {:error, "mogrify error: #{inspect(e)}"}
end end
end end
def filter(_), do: {:ok, :noop} def filter(_), do: {:ok, :noop}
def convert(tempfile) do def convert(tempfile) do
tempfile # cannot save in place when changing format, so we have to use a tmp file
# https://github.com/route/mogrify/issues/77
# also need a valid extension or it gets confused
original = tempfile <> ".heic"
File.rename!(tempfile, original)
%{path: converted} =
original
|> Mogrify.open() |> Mogrify.open()
|> Mogrify.format("jpeg") |> Mogrify.format("jpg")
|> Mogrify.save(in_place: true) |> Mogrify.save()
|> Mogrify.verbose()
|> IO.inspect File.rename!(converted, tempfile)
end end
end end