Add intensity recalculation task

This commit is contained in:
byte[] 2022-01-21 15:37:39 -05:00
parent 18d385283b
commit 3709a09d6b
2 changed files with 62 additions and 9 deletions

View file

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

View file

@ -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),