Extract blurhash into it's own filter
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
1280b66f41
commit
53a3dc6fab
@ -66,10 +66,11 @@ config :mime, :types, %{
|
||||
config :mobilizon, Mobilizon.Web.Upload,
|
||||
uploader: Mobilizon.Web.Upload.Uploader.Local,
|
||||
filters: [
|
||||
Mobilizon.Web.Upload.Filter.Dedupe,
|
||||
Mobilizon.Web.Upload.Filter.AnalyzeMetadata,
|
||||
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"],
|
||||
link_name: true,
|
||||
|
@ -20,13 +20,7 @@ defmodule Mobilizon.Web.Upload.Filter.AnalyzeMetadata do
|
||||
|> Mogrify.open()
|
||||
|> Mogrify.verbose()
|
||||
|
||||
upload =
|
||||
upload
|
||||
|> Map.put(:width, image.width)
|
||||
|> Map.put(:height, image.height)
|
||||
|> Map.put(:blurhash, get_blurhash(file))
|
||||
|
||||
{:ok, :filtered, upload}
|
||||
{:ok, :filtered, %Upload{upload | width: image.width, height: image.height}}
|
||||
rescue
|
||||
e in ErlangError ->
|
||||
Logger.warn("#{__MODULE__}: #{inspect(e)}")
|
||||
@ -34,14 +28,4 @@ defmodule Mobilizon.Web.Upload.Filter.AnalyzeMetadata do
|
||||
end
|
||||
|
||||
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
|
||||
|
31
lib/web/upload/filter/blurhash.ex
Normal file
31
lib/web/upload/filter/blurhash.ex
Normal 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
|
@ -62,9 +62,10 @@ defmodule Mobilizon.Web.Upload do
|
||||
path: String.t(),
|
||||
size: 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()}
|
||||
def store(upload, opts \\ []) do
|
||||
|
Loading…
Reference in New Issue
Block a user