Fix handling for nil upload

This commit is contained in:
Liam 2024-09-10 14:23:05 -04:00
parent be77133136
commit 1dbae45ede
3 changed files with 22 additions and 15 deletions

View file

@ -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.(%{

View file

@ -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

View file

@ -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