Add Pleroma.Utils.command_available?/1 and use where appropriate
authorRoman Chvanikov <chvanikoff@pm.me>
Sat, 11 Jul 2020 12:48:45 +0000 (15:48 +0300)
committerRoman Chvanikov <chvanikoff@pm.me>
Sat, 11 Jul 2020 12:48:45 +0000 (15:48 +0300)
lib/pleroma/upload/filter/exiftool.ex
lib/pleroma/utils.ex
mix.exs
test/upload/filter/exiftool_test.exs

index c7fb6aefa95e87d69b5ce3fd24681ce2984a9ce3..94622acd0910084017b7174ba102ec6be4f4a012 100644 (file)
@@ -9,8 +9,15 @@ defmodule Pleroma.Upload.Filter.Exiftool do
   """
   @behaviour Pleroma.Upload.Filter
 
+  require Logger
+
   def filter(%Pleroma.Upload{tempfile: file, content_type: "image" <> _}) do
-    System.cmd("exiftool", ["-overwrite_original", "-gps:all=", file], parallelism: true)
+    if Pleroma.Utils.command_available?("exiftool") do
+      System.cmd("exiftool", ["-overwrite_original", "-gps:all=", file], parallelism: true)
+    else
+      Logger.warn("exiftool is not available, filter #{__MODULE__} skipped")
+    end
+
     :ok
   end
 
index 6b8e3accf1e04018561ceafe3a4660aad30c4137..21d1159be84383426262ee49bb7d4758dccb3523 100644 (file)
@@ -9,4 +9,19 @@ defmodule Pleroma.Utils do
     |> Enum.map(&Path.join(dir, &1))
     |> Kernel.ParallelCompiler.compile()
   end
+
+  @doc """
+  POSIX-compliant check if command is available in the system
+
+  ## Examples
+      iex> command_available?("git")
+      true
+      iex> command_available?("wrongcmd")
+      false
+
+  """
+  @spec command_available?(String.t()) :: boolean()
+  def command_available?(command) do
+    match?({_output, 0}, System.cmd("sh", ["-c", "command -v #{command}"]))
+  end
 end
diff --git a/mix.exs b/mix.exs
index d7992ee37181180cfbe687e92e70193df87e4364..5cd06e8fd3aa7da1492ce064c98c5e71a012b26b 100644 (file)
--- a/mix.exs
+++ b/mix.exs
@@ -234,10 +234,10 @@ defmodule Pleroma.Mixfile do
   defp version(version) do
     identifier_filter = ~r/[^0-9a-z\-]+/i
 
-    {_cmdgit, cmdgit_err} = System.cmd("sh", ["-c", "command -v git"])
+    git_available? = Pleroma.Utils.command_available?("git")
 
     git_pre_release =
-      if cmdgit_err == 0 do
+      if git_available? do
         {tag, tag_err} =
           System.cmd("git", ["describe", "--tags", "--abbrev=0"], stderr_to_stdout: true)
 
@@ -263,7 +263,7 @@ defmodule Pleroma.Mixfile do
 
     # Branch name as pre-release version component, denoted with a dot
     branch_name =
-      with 0 <- cmdgit_err,
+      with true <- git_available?,
            {branch_name, 0} <- System.cmd("git", ["rev-parse", "--abbrev-ref", "HEAD"]),
            branch_name <- String.trim(branch_name),
            branch_name <- System.get_env("PLEROMA_BUILD_BRANCH") || branch_name,
index a1b7e46cd3e84acd82e4822fc1400bd3a739ab51..8ed7d650bfb74ef7a255a11422d3e0f973dac99e 100644 (file)
@@ -7,6 +7,8 @@ defmodule Pleroma.Upload.Filter.ExiftoolTest do
   alias Pleroma.Upload.Filter
 
   test "apply exiftool filter" do
+    assert Pleroma.Utils.command_available?("exiftool")
+
     File.cp!(
       "test/fixtures/DSCN0010.jpg",
       "test/fixtures/DSCN0010_tmp.jpg"