diff --git a/lib/philomena/images/thumbnailer.ex b/lib/philomena/images/thumbnailer.ex index 8c566135..b8be742b 100644 --- a/lib/philomena/images/thumbnailer.ex +++ b/lib/philomena/images/thumbnailer.ex @@ -76,7 +76,7 @@ defmodule Philomena.Images.Thumbnailer do def generate_thumbnails(image_id) do image = Repo.get!(Image, image_id) file = download_image_file(image) - {:ok, analysis} = Analyzers.analyze(file) + {:ok, analysis} = Analyzers.analyze_path(file) file = apply_edit_script(image, file, Processors.process(analysis, file, generated_sizes(image))) @@ -127,7 +127,7 @@ defmodule Philomena.Images.Thumbnailer do end defp recompute_meta(image, file, changeset_fn) do - {:ok, %{dimensions: {width, height}}} = Analyzers.analyze(file) + {:ok, %{dimensions: {width, height}}} = Analyzers.analyze_path(file) image |> changeset_fn.(%{ diff --git a/lib/philomena_media/analyzers.ex b/lib/philomena_media/analyzers.ex index efa49d9a..7c97e845 100644 --- a/lib/philomena_media/analyzers.ex +++ b/lib/philomena_media/analyzers.ex @@ -40,25 +40,32 @@ defmodule PhilomenaMedia.Analyzers do def analyzer(_content_type), do: :error @doc """ - Attempts a MIME type check and analysis on the given path or `m:Plug.Upload`. + Attempts a MIME type check and analysis on the given `m:Plug.Upload`. + + ## Examples + + file = %Plug.Upload{...} + {:ok, %Result{...}} = Analyzers.analyze_upload(file) + + """ + @spec analyze_upload(Plug.Upload.t()) :: + {:ok, Result.t()} | {:unsupported_mime, Mime.t()} | :error + def analyze_upload(%Plug.Upload{path: path}), do: analyze_path(path) + def analyze_upload(_upload), do: :error + + @doc """ + Attempts a MIME type check and analysis on the given path. ## Examples file = "image_file.png" - {:ok, %Result{...}} = Analyzers.analyze(file) - - file = %Plug.Upload{...} - {:ok, %Result{...}} = Analyzers.analyze(file) + {:ok, %Result{...}} = Analyzers.analyze_path(file) file = "text_file.txt" - :error = Analyzers.analyze(file) + :error = Analyzers.analyze_path(file) """ - @spec analyze(Plug.Upload.t() | Path.t()) :: - {:ok, Result.t()} | {:unsupported_mime, Mime.t()} | :error - def analyze(%Plug.Upload{path: path}), do: analyze(path) - - def analyze(path) when is_binary(path) do + def analyze_path(path) when is_binary(path) do with {:ok, mime} <- Mime.file(path), {:ok, analyzer} <- analyzer(mime) do {:ok, analyzer.analyze(path)} @@ -68,5 +75,5 @@ defmodule PhilomenaMedia.Analyzers do end end - def analyze(_path), do: :error + def analyze_path(_path), do: :error end diff --git a/lib/philomena_media/uploader.ex b/lib/philomena_media/uploader.ex index f15e1b9c..7248d92c 100644 --- a/lib/philomena_media/uploader.ex +++ b/lib/philomena_media/uploader.ex @@ -210,7 +210,7 @@ defmodule PhilomenaMedia.Uploader do (schema_or_changeset(), map() -> Ecto.Changeset.t()) ) :: Ecto.Changeset.t() def analyze_upload(schema_or_changeset, field_name, upload_parameter, changeset_fn) do - with {:ok, analysis} <- Analyzers.analyze(upload_parameter), + with {:ok, analysis} <- Analyzers.analyze_upload(upload_parameter), analysis <- extra_attributes(analysis, upload_parameter) do removed = schema_or_changeset