From e531a0dd575b9e558071e624db753e38f4491c48 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 12 Feb 2021 17:48:26 -0600 Subject: [PATCH] Add filter for HEIF (.heic) files to convert them to JPEGs --- lib/pleroma/upload/filter/heif.ex | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lib/pleroma/upload/filter/heif.ex diff --git a/lib/pleroma/upload/filter/heif.ex b/lib/pleroma/upload/filter/heif.ex new file mode 100644 index 000000000..167c12595 --- /dev/null +++ b/lib/pleroma/upload/filter/heif.ex @@ -0,0 +1,41 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Upload.Filter.Heif do + @behaviour Pleroma.Upload.Filter + alias Pleroma.Upload + + require Logger + + @type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()} + @type conversions :: conversion() | [conversion()] + + @spec filter(Pleroma.Upload.t()) :: {:ok, :atom} | {:error, String.t()} + def filter( + %Pleroma.Upload{name: name, path: path, tempfile: tempfile, content_type: "image/heic"} = + upload + ) do + try do + name = name |> String.replace_suffix(".heic", ".jpg") + path = path |> String.replace_suffix(".heic", ".jpg") + convert(tempfile) + + {:ok, :filtered, %Upload{upload | name: name, path: path, content_type: "image/jpeg"}} + rescue + e in ErlangError -> + {:error, "mogrify command not found: #{inspect(e)}"} + end + end + + def filter(_), do: {:ok, :noop} + + def convert(tempfile) do + tempfile + |> Mogrify.open() + |> Mogrify.format("jpeg") + |> Mogrify.save(in_place: true) + |> Mogrify.verbose() + |> IO.inspect + end +end