mobilizon.chapril.org-mobil.../lib/web/upload/filter/blurhash.ex
Thomas Citharel de047c8939
Various typespec and compilation improvements
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2021-09-26 17:52:20 +02:00

32 lines
756 B
Elixir

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