mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 12:08:00 +01:00
Fix handling for nil upload
This commit is contained in:
parent
be77133136
commit
1dbae45ede
3 changed files with 22 additions and 15 deletions
|
@ -76,7 +76,7 @@ defmodule Philomena.Images.Thumbnailer do
|
||||||
def generate_thumbnails(image_id) do
|
def generate_thumbnails(image_id) do
|
||||||
image = Repo.get!(Image, image_id)
|
image = Repo.get!(Image, image_id)
|
||||||
file = download_image_file(image)
|
file = download_image_file(image)
|
||||||
{:ok, analysis} = Analyzers.analyze(file)
|
{:ok, analysis} = Analyzers.analyze_path(file)
|
||||||
|
|
||||||
file =
|
file =
|
||||||
apply_edit_script(image, file, Processors.process(analysis, file, generated_sizes(image)))
|
apply_edit_script(image, file, Processors.process(analysis, file, generated_sizes(image)))
|
||||||
|
@ -127,7 +127,7 @@ defmodule Philomena.Images.Thumbnailer do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp recompute_meta(image, file, changeset_fn) do
|
defp recompute_meta(image, file, changeset_fn) do
|
||||||
{:ok, %{dimensions: {width, height}}} = Analyzers.analyze(file)
|
{:ok, %{dimensions: {width, height}}} = Analyzers.analyze_path(file)
|
||||||
|
|
||||||
image
|
image
|
||||||
|> changeset_fn.(%{
|
|> changeset_fn.(%{
|
||||||
|
|
|
@ -40,25 +40,32 @@ defmodule PhilomenaMedia.Analyzers do
|
||||||
def analyzer(_content_type), do: :error
|
def analyzer(_content_type), do: :error
|
||||||
|
|
||||||
@doc """
|
@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
|
## Examples
|
||||||
|
|
||||||
file = "image_file.png"
|
file = "image_file.png"
|
||||||
{:ok, %Result{...}} = Analyzers.analyze(file)
|
{:ok, %Result{...}} = Analyzers.analyze_path(file)
|
||||||
|
|
||||||
file = %Plug.Upload{...}
|
|
||||||
{:ok, %Result{...}} = Analyzers.analyze(file)
|
|
||||||
|
|
||||||
file = "text_file.txt"
|
file = "text_file.txt"
|
||||||
:error = Analyzers.analyze(file)
|
:error = Analyzers.analyze_path(file)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@spec analyze(Plug.Upload.t() | Path.t()) ::
|
def analyze_path(path) when is_binary(path) do
|
||||||
{: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
|
|
||||||
with {:ok, mime} <- Mime.file(path),
|
with {:ok, mime} <- Mime.file(path),
|
||||||
{:ok, analyzer} <- analyzer(mime) do
|
{:ok, analyzer} <- analyzer(mime) do
|
||||||
{:ok, analyzer.analyze(path)}
|
{:ok, analyzer.analyze(path)}
|
||||||
|
@ -68,5 +75,5 @@ defmodule PhilomenaMedia.Analyzers do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def analyze(_path), do: :error
|
def analyze_path(_path), do: :error
|
||||||
end
|
end
|
||||||
|
|
|
@ -210,7 +210,7 @@ defmodule PhilomenaMedia.Uploader do
|
||||||
(schema_or_changeset(), map() -> Ecto.Changeset.t())
|
(schema_or_changeset(), map() -> Ecto.Changeset.t())
|
||||||
) :: Ecto.Changeset.t()
|
) :: Ecto.Changeset.t()
|
||||||
def analyze_upload(schema_or_changeset, field_name, upload_parameter, changeset_fn) do
|
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
|
analysis <- extra_attributes(analysis, upload_parameter) do
|
||||||
removed =
|
removed =
|
||||||
schema_or_changeset
|
schema_or_changeset
|
||||||
|
|
Loading…
Reference in a new issue