1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-22 07:52:43 +00:00

Extract blurhash into it's own filter

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-08-12 10:29:40 +02:00
parent 1280b66f41
commit 53a3dc6fab
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
4 changed files with 38 additions and 21 deletions

View file

@ -66,10 +66,11 @@ config :mime, :types, %{
config :mobilizon, Mobilizon.Web.Upload, config :mobilizon, Mobilizon.Web.Upload,
uploader: Mobilizon.Web.Upload.Uploader.Local, uploader: Mobilizon.Web.Upload.Uploader.Local,
filters: [ filters: [
Mobilizon.Web.Upload.Filter.Dedupe,
Mobilizon.Web.Upload.Filter.AnalyzeMetadata, Mobilizon.Web.Upload.Filter.AnalyzeMetadata,
Mobilizon.Web.Upload.Filter.Resize, Mobilizon.Web.Upload.Filter.Resize,
Mobilizon.Web.Upload.Filter.Optimize Mobilizon.Web.Upload.Filter.Optimize,
Mobilizon.Web.Upload.Filter.BlurHash,
Mobilizon.Web.Upload.Filter.Dedupe
], ],
allow_list_mime_types: ["image/gif", "image/jpeg", "image/png", "image/webp"], allow_list_mime_types: ["image/gif", "image/jpeg", "image/png", "image/webp"],
link_name: true, link_name: true,

View file

@ -20,13 +20,7 @@ defmodule Mobilizon.Web.Upload.Filter.AnalyzeMetadata do
|> Mogrify.open() |> Mogrify.open()
|> Mogrify.verbose() |> Mogrify.verbose()
upload = {:ok, :filtered, %Upload{upload | width: image.width, height: image.height}}
upload
|> Map.put(:width, image.width)
|> Map.put(:height, image.height)
|> Map.put(:blurhash, get_blurhash(file))
{:ok, :filtered, upload}
rescue rescue
e in ErlangError -> e in ErlangError ->
Logger.warn("#{__MODULE__}: #{inspect(e)}") Logger.warn("#{__MODULE__}: #{inspect(e)}")
@ -34,14 +28,4 @@ defmodule Mobilizon.Web.Upload.Filter.AnalyzeMetadata do
end end
def filter(_), do: {:ok, :noop} def filter(_), do: {:ok, :noop}
defp get_blurhash(file) do
case :eblurhash.magick(to_charlist(file)) do
{:ok, blurhash} ->
to_string(blurhash)
_ ->
nil
end
end
end end

View file

@ -0,0 +1,31 @@
defmodule Mobilizon.Web.Upload.Filter.BlurHash do
@moduledoc """
Computes blurhash from the upload
"""
require Logger
alias Mobilizon.Web.Upload
@behaviour Mobilizon.Web.Upload.Filter
@spec filter(Upload.t()) ::
{:ok, :filtered, Upload.t()} | {:ok, :noop} | {:error, String.t()}
def filter(%Upload{tempfile: file, content_type: "image" <> _} = upload) do
{:ok, :filtered, %Upload{upload | blurhash: generate_blurhash(file)}}
rescue
e in ErlangError ->
Logger.warn("#{__MODULE__}: #{inspect(e)}")
{:ok, :noop}
end
def filter(_), do: {:ok, :noop}
defp generate_blurhash(file) do
case :eblurhash.magick(to_charlist(file)) do
{:ok, blurhash} ->
to_string(blurhash)
_ ->
nil
end
end
end

View file

@ -62,9 +62,10 @@ defmodule Mobilizon.Web.Upload do
path: String.t(), path: String.t(),
size: integer(), size: integer(),
width: integer(), width: integer(),
height: integer() height: integer(),
blurhash: String.t()
} }
defstruct [:id, :name, :tempfile, :content_type, :path, :size, :width, :height] defstruct [:id, :name, :tempfile, :content_type, :path, :size, :width, :height, :blurhash]
@spec store(source, options :: [option()]) :: {:ok, map()} | {:error, any()} @spec store(source, options :: [option()]) :: {:ok, map()} | {:error, any()}
def store(upload, opts \\ []) do def store(upload, opts \\ []) do