Merge remote-tracking branch 'upstream/attachment-meta' into blurhash
authorAlex Gleason <alex@alexgleason.me>
Fri, 14 May 2021 14:09:32 +0000 (09:09 -0500)
committerAlex Gleason <alex@alexgleason.me>
Fri, 14 May 2021 14:09:32 +0000 (09:09 -0500)
lib/pleroma/upload.ex
lib/pleroma/upload/filter/set_meta.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
mix.exs
mix.lock

index 4d58abd481e96ddf2aa119b6179d8eb0383b53ce..5570ed104b106a10bdf219214fdb9d05a30d3a46 100644 (file)
@@ -25,6 +25,7 @@ defmodule Pleroma.Upload do
   path as the temporary file is also tracked by `Plug.Upload{}` and automatically deleted once the request is over.
   * `:width` - width of the media in pixels
   * `:height` - height of the media in pixels
+  * `:blurhash` - string hash of the image encoded with the blurhash algorithm (https://blurha.sh/)
 
   Related behaviors:
 
@@ -58,9 +59,10 @@ defmodule Pleroma.Upload do
           content_type: String.t(),
           width: integer(),
           height: integer(),
+          blurhash: String.t(),
           path: String.t()
         }
-  defstruct [:id, :name, :tempfile, :content_type, :width, :height, :path]
+  defstruct [:id, :name, :tempfile, :content_type, :width, :height, :blurhash, :path]
 
   defp get_description(opts, upload) do
     case {opts[:description], Pleroma.Config.get([Pleroma.Upload, :default_description])} do
@@ -98,7 +100,8 @@ defmodule Pleroma.Upload do
            |> Maps.put_if_present("height", upload.height)
          ],
          "name" => description
-       }}
+       }
+       |> Maps.put_if_present("blurhash", upload.blurhash)}
     else
       {:description_limit, _} ->
         {:error, :description_too_long}
index cccb6c371c2ae1a0ad8fe94d544f4a9f2c5d745a..81c48228a955bc060fa063d46d3d877cb1904311 100644 (file)
@@ -23,6 +23,7 @@ defmodule Pleroma.Upload.Filter.SetMeta do
         upload
         |> Map.put(:width, image.width)
         |> Map.put(:height, image.height)
+        |> Map.put(:blurhash, get_blurhash(file))
 
       {:ok, :filtered, upload}
     rescue
@@ -33,4 +34,12 @@ defmodule Pleroma.Upload.Filter.SetMeta do
   end
 
   def filter(_), do: {:ok, :noop}
+
+  defp get_blurhash(file) do
+    with {:ok, blurhash} <- :eblurhash.magick(file) do
+      blurhash
+    else
+      _ -> nil
+    end
+  end
 end
index d1a0867e261494e83d8e1cd92b46611a0376930f..a40d51c452d821a57dfcf9d9167d8cc28a4cc7ed 100644 (file)
@@ -961,6 +961,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
         }
         |> Maps.put_if_present("width", url["width"])
         |> Maps.put_if_present("height", url["height"])
+        |> Maps.put_if_present("blurhash", data["blurhash"])
       end)
 
     Map.put(object, "attachment", attachments)
diff --git a/mix.exs b/mix.exs
index b449c82b3b9bf94c0e762b766b720544cd6c8f99..a4bacba8e3dc481bf87d5898ead7ff8df13af914 100644 (file)
--- a/mix.exs
+++ b/mix.exs
@@ -196,6 +196,9 @@ defmodule Pleroma.Mixfile do
       {:majic,
        git: "https://git.pleroma.social/pleroma/elixir-libraries/majic.git",
        ref: "289cda1b6d0d70ccb2ba508a2b0bd24638db2880"},
+      {:eblurhash,
+       git: "https://github.com/zotonic/eblurhash.git",
+       ref: "04a0b76eadf4de1be17726f39b6313b88708fd12"},
       {:open_api_spex, "~> 3.10"},
 
       ## dev & test
index 22c31e0d6e32bfb991922388ea921d3d24e4b2ac..55f73ad008de445a0ef039819d946148e211c2a5 100644 (file)
--- a/mix.lock
+++ b/mix.lock
@@ -29,6 +29,7 @@
   "deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
   "earmark": {:hex, :earmark, "1.4.15", "2c7f924bf495ec1f65bd144b355d0949a05a254d0ec561740308a54946a67888", [:mix], [{:earmark_parser, ">= 1.4.13", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "3b1209b85bc9f3586f370f7c363f6533788fb4e51db23aa79565875e7f9999ee"},
   "earmark_parser": {:hex, :earmark_parser, "1.4.13", "0c98163e7d04a15feb62000e1a891489feb29f3d10cb57d4f845c405852bbef8", [:mix], [], "hexpm", "d602c26af3a0af43d2f2645613f65841657ad6efc9f0e361c3b6c06b578214ba"},
+  "eblurhash": {:git, "https://github.com/zotonic/eblurhash.git", "04a0b76eadf4de1be17726f39b6313b88708fd12", [ref: "04a0b76eadf4de1be17726f39b6313b88708fd12"]},
   "ecto": {:hex, :ecto, "3.4.6", "08f7afad3257d6eb8613309af31037e16c36808dfda5a3cd0cb4e9738db030e4", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6f13a9e2a62e75c2dcfc7207bfc65645ab387af8360db4c89fee8b5a4bf3f70b"},
   "ecto_enum": {:hex, :ecto_enum, "1.4.0", "d14b00e04b974afc69c251632d1e49594d899067ee2b376277efd8233027aec8", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "> 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "8fb55c087181c2b15eee406519dc22578fa60dd82c088be376d0010172764ee4"},
   "ecto_explain": {:hex, :ecto_explain, "0.1.2", "a9d504cbd4adc809911f796d5ef7ebb17a576a6d32286c3d464c015bd39d5541", [:mix], [], "hexpm", "1d0e7798ae30ecf4ce34e912e5354a0c1c832b7ebceba39298270b9a9f316330"},