From 3709a09d6b446c10c81f343723e95fe465cd10d7 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 21 Jan 2022 15:37:39 -0500 Subject: [PATCH] Add intensity recalculation task --- lib/mix/tasks/recalculate_intensities.ex | 53 ++++++++++++++++++++++++ lib/philomena/images/thumbnailer.ex | 18 ++++---- 2 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 lib/mix/tasks/recalculate_intensities.ex diff --git a/lib/mix/tasks/recalculate_intensities.ex b/lib/mix/tasks/recalculate_intensities.ex new file mode 100644 index 00000000..ad3ee171 --- /dev/null +++ b/lib/mix/tasks/recalculate_intensities.ex @@ -0,0 +1,53 @@ +defmodule Mix.Tasks.RecalculateIntensities do + use Mix.Task + + alias Philomena.Images.{Image, Thumbnailer} + alias Philomena.ImageIntensities.ImageIntensity + alias Philomena.Batch + alias Philomena.Repo + + import Ecto.Query + + @shortdoc "Recalculates all intensities for reverse search." + @requirements ["app.start"] + @impl Mix.Task + def run(_args) do + Batch.record_batches(Image, fn batch -> + batch + |> Stream.with_index() + |> Stream.each(fn {image, i} -> + image_file = + cond do + image.image_mime_type in ["image/png", "image/jpeg"] -> + Thumbnailer.image_file(image) + + true -> + Path.join(Thumbnailer.image_thumb_dir(image), "rendered.png") + end + + case System.cmd("image-intensities", [image_file]) do + {output, 0} -> + [nw, ne, sw, se] = + output + |> String.trim() + |> String.split("\t") + |> Enum.map(&String.to_float/1) + + ImageIntensity + |> where(image_id: ^image.id) + |> Repo.update_all(set: [nw: nw, ne: ne, sw: sw, se: se]) + + _ -> + :err + end + + if rem(i, 100) == 0 do + IO.write("\r#{image.id}") + end + end) + |> Stream.run() + end) + + IO.puts("\nDone") + end +end diff --git a/lib/philomena/images/thumbnailer.ex b/lib/philomena/images/thumbnailer.ex index 1868831a..97da4083 100644 --- a/lib/philomena/images/thumbnailer.ex +++ b/lib/philomena/images/thumbnailer.ex @@ -132,18 +132,18 @@ defmodule Philomena.Images.Thumbnailer do |> File.mkdir_p!() end - defp image_file(%Image{image: image}), + def image_file(%Image{image: image}), do: Path.join(image_file_root(), image) - defp image_thumb_dir(%Image{ - created_at: created_at, - id: id, - hidden_from_users: true, - hidden_image_key: key - }), - do: Path.join([image_thumbnail_root(), time_identifier(created_at), "#{id}-#{key}"]) + def image_thumb_dir(%Image{ + created_at: created_at, + id: id, + hidden_from_users: true, + hidden_image_key: key + }), + do: Path.join([image_thumbnail_root(), time_identifier(created_at), "#{id}-#{key}"]) - defp image_thumb_dir(%Image{created_at: created_at, id: id}), + def image_thumb_dir(%Image{created_at: created_at, id: id}), do: Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(id)]) defp image_url_base(%Image{created_at: created_at, id: id}, nil),