move to 2.5.5
[anni] / lib / pleroma / upload / filter / exiftool / strip_location.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Upload.Filter.Exiftool.StripLocation do
6   @moduledoc """
7   Strips GPS related EXIF tags and overwrites the file in place.
8   Also strips or replaces filesystem metadata e.g., timestamps.
9   """
10   @behaviour Pleroma.Upload.Filter
11
12   @spec filter(Pleroma.Upload.t()) :: {:ok, any()} | {:error, String.t()}
13
14   # Formats not compatible with exiftool at this time
15   def filter(%Pleroma.Upload{content_type: "image/heic"}), do: {:ok, :noop}
16   def filter(%Pleroma.Upload{content_type: "image/webp"}), do: {:ok, :noop}
17   def filter(%Pleroma.Upload{content_type: "image/svg" <> _}), do: {:ok, :noop}
18
19   def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
20     try do
21       case System.cmd("exiftool", ["-overwrite_original", "-gps:all=", file], parallelism: true) do
22         {_response, 0} -> {:ok, :filtered}
23         {error, 1} -> {:error, error}
24       end
25     rescue
26       e in ErlangError ->
27         {:error, "#{__MODULE__}: #{inspect(e)}"}
28     end
29   end
30
31   def filter(_), do: {:ok, :noop}
32 end