Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Sep 10, 2024
2 parents e9a2279 + 7cad231 commit 9adee67
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ jobs:

- run: npm run test
working-directory: ./assets

- run: npm run build
working-directory: ./assets
4 changes: 2 additions & 2 deletions lib/philomena/images/thumbnailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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.(%{
Expand Down
31 changes: 19 additions & 12 deletions lib/philomena_media/analyzers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "image_file.png"
{:ok, %Result{...}} = Analyzers.analyze(file)
file = %Plug.Upload{...}
{:ok, %Result{...}} = Analyzers.analyze(file)
file = "text_file.txt"
:error = Analyzers.analyze(file)
{:ok, %Result{...}} = Analyzers.analyze_upload(file)
"""
@spec analyze(Plug.Upload.t() | Path.t()) ::
@spec analyze_upload(Plug.Upload.t()) ::
{:ok, Result.t()} | {:unsupported_mime, Mime.t()} | :error
def analyze(%Plug.Upload{path: path}), do: analyze(path)
def analyze_upload(%Plug.Upload{path: path}), do: analyze_path(path)
def analyze_upload(_upload), do: :error

def analyze(path) when is_binary(path) do
@doc """
Attempts a MIME type check and analysis on the given path.
## Examples
file = "image_file.png"
{:ok, %Result{...}} = Analyzers.analyze_path(file)
file = "text_file.txt"
:error = Analyzers.analyze_path(file)
"""
def analyze_path(path) when is_binary(path) do
with {:ok, mime} <- Mime.file(path),
{:ok, analyzer} <- analyzer(mime) do
{:ok, analyzer.analyze(path)}
Expand All @@ -68,5 +75,5 @@ defmodule PhilomenaMedia.Analyzers do
end
end

def analyze(_path), do: :error
def analyze_path(_path), do: :error
end
2 changes: 1 addition & 1 deletion lib/philomena_media/uploader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ defmodule PhilomenaWeb.Api.Json.Search.ReverseController do
def create(conn, %{"image" => image_params}) do
user = conn.assigns.current_user

images =
{images, total} =
image_params
|> Map.put("distance", conn.params["distance"])
|> Map.put("limit", conn.params["limit"])
|> DuplicateReports.execute_search_query()
|> case do
{:ok, images} ->
images
{images, images.total_entries}

{:error, _changeset} ->
[]
{[], 0}
end

interactions = Interactions.user_interactions(images, user)
Expand All @@ -29,7 +29,7 @@ defmodule PhilomenaWeb.Api.Json.Search.ReverseController do
|> put_view(PhilomenaWeb.Api.Json.ImageView)
|> render("index.json",
images: images,
total: images.total_entries,
total: total,
interactions: interactions
)
end
Expand Down

0 comments on commit 9adee67

Please sign in to comment.