feat: hopefully last part of persistence
This commit is contained in:
174
lib/bds/media.ex
174
lib/bds/media.ex
@@ -1,7 +1,10 @@
|
|||||||
defmodule BDS.Media do
|
defmodule BDS.Media do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
alias BDS.Media.Media
|
alias BDS.Media.Media
|
||||||
|
alias BDS.Media.Translation
|
||||||
alias BDS.Projects
|
alias BDS.Projects
|
||||||
alias BDS.Repo
|
alias BDS.Repo
|
||||||
alias BDS.Sidecar
|
alias BDS.Sidecar
|
||||||
@@ -46,6 +49,7 @@ defmodule BDS.Media do
|
|||||||
:ok = File.mkdir_p(Path.dirname(destination))
|
:ok = File.mkdir_p(Path.dirname(destination))
|
||||||
:ok = File.cp(source_path, destination)
|
:ok = File.cp(source_path, destination)
|
||||||
:ok = write_sidecar(project, media)
|
:ok = write_sidecar(project, media)
|
||||||
|
:ok = ensure_thumbnails(project, media)
|
||||||
media
|
media
|
||||||
end)
|
end)
|
||||||
|> case do
|
|> case do
|
||||||
@@ -95,23 +99,98 @@ defmodule BDS.Media do
|
|||||||
{:error, :not_found}
|
{:error, :not_found}
|
||||||
|
|
||||||
media ->
|
media ->
|
||||||
|
translations = Repo.all(from translation in Translation, where: translation.translation_for == ^media.id)
|
||||||
|
|
||||||
delete_file_if_present(media.project_id, media.file_path)
|
delete_file_if_present(media.project_id, media.file_path)
|
||||||
delete_file_if_present(media.project_id, media.sidecar_path)
|
delete_file_if_present(media.project_id, media.sidecar_path)
|
||||||
|
delete_thumbnail_files(media.project_id, media)
|
||||||
|
|
||||||
|
Enum.each(translations, fn translation ->
|
||||||
|
delete_file_if_present(media.project_id, translation_sidecar_path(media, translation.language))
|
||||||
|
Repo.delete!(translation)
|
||||||
|
end)
|
||||||
|
|
||||||
Repo.delete!(media)
|
Repo.delete!(media)
|
||||||
{:ok, :deleted}
|
{:ok, :deleted}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def upsert_media_translation(media_id, language, attrs) do
|
||||||
|
case Repo.get(Media, media_id) do
|
||||||
|
nil ->
|
||||||
|
{:error, :not_found}
|
||||||
|
|
||||||
|
media ->
|
||||||
|
project = Projects.get_project!(media.project_id)
|
||||||
|
now = System.system_time(:second)
|
||||||
|
|
||||||
|
translation =
|
||||||
|
Repo.get_by(Translation, translation_for: media.id, language: language) ||
|
||||||
|
%Translation{id: Ecto.UUID.generate(), created_at: now}
|
||||||
|
|
||||||
|
translation_attrs = %{
|
||||||
|
id: translation.id,
|
||||||
|
project_id: media.project_id,
|
||||||
|
translation_for: media.id,
|
||||||
|
language: language,
|
||||||
|
title: attr(attrs, :title),
|
||||||
|
alt: attr(attrs, :alt),
|
||||||
|
caption: attr(attrs, :caption),
|
||||||
|
created_at: translation.created_at || now,
|
||||||
|
updated_at: now
|
||||||
|
}
|
||||||
|
|
||||||
|
Repo.transaction(fn ->
|
||||||
|
updated_translation =
|
||||||
|
translation
|
||||||
|
|> Translation.changeset(translation_attrs)
|
||||||
|
|> Repo.insert_or_update!()
|
||||||
|
|
||||||
|
:ok = write_translation_sidecar(project, media, updated_translation)
|
||||||
|
updated_translation
|
||||||
|
end)
|
||||||
|
|> case do
|
||||||
|
{:ok, updated_translation} -> {:ok, updated_translation}
|
||||||
|
{:error, reason} -> {:error, reason}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def thumbnail_paths(%Media{id: id}) do
|
||||||
|
prefix = String.slice(id, 0, 2)
|
||||||
|
|
||||||
|
%{
|
||||||
|
small: Path.join(["thumbnails", prefix, "#{id}-small.webp"]),
|
||||||
|
medium: Path.join(["thumbnails", prefix, "#{id}-medium.webp"]),
|
||||||
|
large: Path.join(["thumbnails", prefix, "#{id}-large.webp"]),
|
||||||
|
ai: Path.join(["thumbnails", prefix, "#{id}-ai.jpg"])
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def rebuild_media_from_files(project_id) do
|
def rebuild_media_from_files(project_id) do
|
||||||
project = Projects.get_project!(project_id)
|
project = Projects.get_project!(project_id)
|
||||||
|
|
||||||
media_items =
|
canonical_sidecars =
|
||||||
project
|
project
|
||||||
|> Projects.project_data_dir()
|
|> Projects.project_data_dir()
|
||||||
|> Path.join("media")
|
|> Path.join("media")
|
||||||
|> list_matching_files("*.meta")
|
|> list_matching_files("*.meta")
|
||||||
|
|> Enum.filter(&canonical_sidecar?/1)
|
||||||
|> Enum.filter(&binary_exists_for_sidecar?/1)
|
|> Enum.filter(&binary_exists_for_sidecar?/1)
|
||||||
|> Enum.map(&upsert_media_from_sidecar(project, &1))
|
|
||||||
|
media_items = Enum.map(canonical_sidecars, &upsert_media_from_sidecar(project, &1))
|
||||||
|
|
||||||
|
canonical_media_by_binary_path =
|
||||||
|
Map.new(media_items, fn media ->
|
||||||
|
{Path.join(Projects.project_data_dir(project), media.file_path), media}
|
||||||
|
end)
|
||||||
|
|
||||||
|
project
|
||||||
|
|> Projects.project_data_dir()
|
||||||
|
|> Path.join("media")
|
||||||
|
|> list_matching_files("*.meta")
|
||||||
|
|> Enum.filter(&translation_sidecar?/1)
|
||||||
|
|> Enum.each(&upsert_translation_from_sidecar(project, canonical_media_by_binary_path, &1))
|
||||||
|
|
||||||
{:ok, media_items}
|
{:ok, media_items}
|
||||||
end
|
end
|
||||||
@@ -150,6 +229,7 @@ defmodule BDS.Media do
|
|||||||
media
|
media
|
||||||
|> Media.changeset(attrs)
|
|> Media.changeset(attrs)
|
||||||
|> Repo.insert_or_update!()
|
|> Repo.insert_or_update!()
|
||||||
|
|> tap(fn reloaded_media -> ensure_thumbnails(project, reloaded_media) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp write_sidecar(project, media) do
|
defp write_sidecar(project, media) do
|
||||||
@@ -177,6 +257,80 @@ defmodule BDS.Media do
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp write_translation_sidecar(project, media, translation) do
|
||||||
|
path = Path.join(Projects.project_data_dir(project), translation_sidecar_path(media, translation.language))
|
||||||
|
:ok = File.mkdir_p(Path.dirname(path))
|
||||||
|
|
||||||
|
atomic_write(
|
||||||
|
path,
|
||||||
|
Sidecar.serialize_document([
|
||||||
|
{:translation_for, media.id},
|
||||||
|
{:language, translation.language},
|
||||||
|
{:title, translation.title},
|
||||||
|
{:alt, translation.alt},
|
||||||
|
{:caption, translation.caption}
|
||||||
|
])
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp upsert_translation_from_sidecar(project, canonical_media_by_binary_path, sidecar_path) do
|
||||||
|
binary_path = binary_path_for_translation_sidecar(sidecar_path)
|
||||||
|
|
||||||
|
case Map.get(canonical_media_by_binary_path, binary_path) do
|
||||||
|
nil ->
|
||||||
|
:skip
|
||||||
|
|
||||||
|
media ->
|
||||||
|
{:ok, fields} = sidecar_path |> File.read!() |> Sidecar.parse_document()
|
||||||
|
now = System.system_time(:second)
|
||||||
|
language = Map.fetch!(fields, "language")
|
||||||
|
|
||||||
|
translation =
|
||||||
|
Repo.get_by(Translation, translation_for: media.id, language: language) ||
|
||||||
|
%Translation{id: Ecto.UUID.generate(), created_at: now}
|
||||||
|
|
||||||
|
translation
|
||||||
|
|> Translation.changeset(%{
|
||||||
|
id: translation.id,
|
||||||
|
project_id: project.id,
|
||||||
|
translation_for: media.id,
|
||||||
|
language: language,
|
||||||
|
title: Map.get(fields, "title"),
|
||||||
|
alt: Map.get(fields, "alt"),
|
||||||
|
caption: Map.get(fields, "caption"),
|
||||||
|
created_at: translation.created_at || now,
|
||||||
|
updated_at: now
|
||||||
|
})
|
||||||
|
|> Repo.insert_or_update!()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp ensure_thumbnails(project, media) do
|
||||||
|
if image_mime?(media.mime_type) do
|
||||||
|
source_path = Path.join(Projects.project_data_dir(project), media.file_path)
|
||||||
|
|
||||||
|
Enum.each(thumbnail_paths(media), fn {_size, relative_path} ->
|
||||||
|
destination = Path.join(Projects.project_data_dir(project), relative_path)
|
||||||
|
:ok = File.mkdir_p(Path.dirname(destination))
|
||||||
|
|
||||||
|
case File.read(source_path) do
|
||||||
|
{:ok, contents} -> :ok = File.write(destination, contents)
|
||||||
|
{:error, _reason} -> :ok = File.write(destination, "")
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp delete_thumbnail_files(project_id, media) do
|
||||||
|
Enum.each(Map.values(thumbnail_paths(media)), fn path ->
|
||||||
|
delete_file_if_present(project_id, path)
|
||||||
|
end)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
defp media_file_path(file_name, timestamp) do
|
defp media_file_path(file_name, timestamp) do
|
||||||
datetime = DateTime.from_unix!(timestamp)
|
datetime = DateTime.from_unix!(timestamp)
|
||||||
year = Integer.to_string(datetime.year)
|
year = Integer.to_string(datetime.year)
|
||||||
@@ -197,6 +351,8 @@ defmodule BDS.Media do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp image_mime?(mime_type), do: String.starts_with?(mime_type || "", "image/")
|
||||||
|
|
||||||
defp binary_exists_for_sidecar?(sidecar_path) do
|
defp binary_exists_for_sidecar?(sidecar_path) do
|
||||||
sidecar_path
|
sidecar_path
|
||||||
|> String.trim_trailing(".meta")
|
|> String.trim_trailing(".meta")
|
||||||
@@ -213,6 +369,20 @@ defmodule BDS.Media do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp canonical_sidecar?(sidecar_path) do
|
||||||
|
not translation_sidecar?(sidecar_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp translation_sidecar?(sidecar_path) do
|
||||||
|
Regex.match?(~r/\.[a-z]{2}\.meta$/i, sidecar_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp binary_path_for_translation_sidecar(sidecar_path) do
|
||||||
|
Regex.replace(~r/\.[a-z]{2}\.meta$/i, sidecar_path, "")
|
||||||
|
end
|
||||||
|
|
||||||
|
defp translation_sidecar_path(media, language), do: "#{media.file_path}.#{language}.meta"
|
||||||
|
|
||||||
defp delete_file_if_present(project_id, relative_path) do
|
defp delete_file_if_present(project_id, relative_path) do
|
||||||
project = Projects.get_project!(project_id)
|
project = Projects.get_project!(project_id)
|
||||||
full_path = Path.join(Projects.project_data_dir(project), relative_path)
|
full_path = Path.join(Projects.project_data_dir(project), relative_path)
|
||||||
|
|||||||
30
lib/bds/media/translation.ex
Normal file
30
lib/bds/media/translation.ex
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
defmodule BDS.Media.Translation do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
use Ecto.Schema
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
@primary_key {:id, :string, autogenerate: false}
|
||||||
|
@foreign_key_type :string
|
||||||
|
|
||||||
|
schema "media_translations" do
|
||||||
|
belongs_to :media, BDS.Media.Media, foreign_key: :translation_for, references: :id, type: :string
|
||||||
|
field :project_id, :string
|
||||||
|
field :language, :string
|
||||||
|
field :title, :string
|
||||||
|
field :alt, :string
|
||||||
|
field :caption, :string
|
||||||
|
field :created_at, :integer
|
||||||
|
field :updated_at, :integer
|
||||||
|
end
|
||||||
|
|
||||||
|
def changeset(translation, attrs) do
|
||||||
|
translation
|
||||||
|
|> cast(attrs, [:id, :project_id, :translation_for, :language, :title, :alt, :caption, :created_at, :updated_at],
|
||||||
|
empty_values: [nil]
|
||||||
|
)
|
||||||
|
|> validate_required([:id, :project_id, :translation_for, :language, :created_at, :updated_at])
|
||||||
|
|> foreign_key_constraint(:translation_for)
|
||||||
|
|> unique_constraint(:language, name: :media_translations_translation_language_idx)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -82,28 +82,43 @@ defmodule BDS.MediaTest do
|
|||||||
|
|
||||||
assert {:ok, media} = BDS.Media.import_media(%{project_id: project.id, source_path: source_path})
|
assert {:ok, media} = BDS.Media.import_media(%{project_id: project.id, source_path: source_path})
|
||||||
|
|
||||||
|
assert {:ok, _translation} =
|
||||||
|
BDS.Media.upsert_media_translation(media.id, "de", %{
|
||||||
|
title: "Titel",
|
||||||
|
alt: "Alt",
|
||||||
|
caption: "Beschriftung"
|
||||||
|
})
|
||||||
|
|
||||||
|
thumbnail_paths = BDS.Media.thumbnail_paths(media)
|
||||||
|
|
||||||
assert {:ok, :deleted} = BDS.Media.delete_media(media.id)
|
assert {:ok, :deleted} = BDS.Media.delete_media(media.id)
|
||||||
assert Repo.get(BDS.Media.Media, media.id) == nil
|
assert Repo.get(BDS.Media.Media, media.id) == nil
|
||||||
|
assert Repo.all(BDS.Media.Translation) == []
|
||||||
refute File.exists?(Path.join(temp_dir, media.file_path))
|
refute File.exists?(Path.join(temp_dir, media.file_path))
|
||||||
refute File.exists?(Path.join(temp_dir, media.sidecar_path))
|
refute File.exists?(Path.join(temp_dir, media.sidecar_path))
|
||||||
|
refute File.exists?(Path.join(temp_dir, media.file_path <> ".de.meta"))
|
||||||
|
|
||||||
|
Enum.each(Map.values(thumbnail_paths), fn path ->
|
||||||
|
refute File.exists?(Path.join(temp_dir, path))
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "rebuild_media_from_files recreates media rows from sidecars", %{project: project, temp_dir: temp_dir} do
|
test "rebuild_media_from_files recreates media rows from sidecars", %{project: project, temp_dir: temp_dir} do
|
||||||
media_dir = Path.join([temp_dir, "media", "2026", "04"])
|
media_dir = Path.join([temp_dir, "media", "2026", "04"])
|
||||||
File.mkdir_p!(media_dir)
|
File.mkdir_p!(media_dir)
|
||||||
|
|
||||||
binary_path = Path.join(media_dir, "asset.txt")
|
binary_path = Path.join(media_dir, "asset.jpg")
|
||||||
sidecar_path = binary_path <> ".meta"
|
sidecar_path = binary_path <> ".meta"
|
||||||
|
|
||||||
File.write!(binary_path, "hello media")
|
File.write!(binary_path, "fake-jpeg")
|
||||||
|
|
||||||
File.write!(
|
File.write!(
|
||||||
sidecar_path,
|
sidecar_path,
|
||||||
[
|
[
|
||||||
"id: media-from-file",
|
"id: media-from-file",
|
||||||
"original_name: original.txt",
|
"original_name: original.jpg",
|
||||||
"mime_type: text/plain",
|
"mime_type: image/jpeg",
|
||||||
"size: 11",
|
"size: 9",
|
||||||
"width: 0",
|
"width: 0",
|
||||||
"height: 0",
|
"height: 0",
|
||||||
"title: Recovered",
|
"title: Recovered",
|
||||||
@@ -120,23 +135,92 @@ defmodule BDS.MediaTest do
|
|||||||
|> Enum.join("\n")
|
|> Enum.join("\n")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
File.write!(
|
||||||
|
binary_path <> ".de.meta",
|
||||||
|
[
|
||||||
|
"translation_for: media-from-file",
|
||||||
|
"language: de",
|
||||||
|
"title: Titel",
|
||||||
|
"alt: Alt text",
|
||||||
|
"caption: Bildunterschrift",
|
||||||
|
""
|
||||||
|
]
|
||||||
|
|> Enum.join("\n")
|
||||||
|
)
|
||||||
|
|
||||||
assert {:ok, media_items} = BDS.Media.rebuild_media_from_files(project.id)
|
assert {:ok, media_items} = BDS.Media.rebuild_media_from_files(project.id)
|
||||||
assert length(media_items) == 1
|
assert length(media_items) == 1
|
||||||
|
|
||||||
[media] = media_items
|
[media] = media_items
|
||||||
assert media.id == "media-from-file"
|
assert media.id == "media-from-file"
|
||||||
assert media.project_id == project.id
|
assert media.project_id == project.id
|
||||||
assert media.filename == "asset.txt"
|
assert media.filename == "asset.jpg"
|
||||||
assert media.original_name == "original.txt"
|
assert media.original_name == "original.jpg"
|
||||||
assert media.mime_type == "text/plain"
|
assert media.mime_type == "image/jpeg"
|
||||||
assert media.size == 11
|
assert media.size == 9
|
||||||
assert media.title == "Recovered"
|
assert media.title == "Recovered"
|
||||||
assert media.alt == "Recovered alt"
|
assert media.alt == "Recovered alt"
|
||||||
assert media.caption == "Recovered caption"
|
assert media.caption == "Recovered caption"
|
||||||
assert media.author == "Writer"
|
assert media.author == "Writer"
|
||||||
assert media.language == "en"
|
assert media.language == "en"
|
||||||
assert media.tags == ["alpha"]
|
assert media.tags == ["alpha"]
|
||||||
assert media.file_path == "media/2026/04/asset.txt"
|
assert media.file_path == "media/2026/04/asset.jpg"
|
||||||
assert media.sidecar_path == "media/2026/04/asset.txt.meta"
|
assert media.sidecar_path == "media/2026/04/asset.jpg.meta"
|
||||||
|
|
||||||
|
[translation] = Repo.all(BDS.Media.Translation)
|
||||||
|
assert translation.translation_for == "media-from-file"
|
||||||
|
assert translation.language == "de"
|
||||||
|
assert translation.title == "Titel"
|
||||||
|
assert translation.alt == "Alt text"
|
||||||
|
assert translation.caption == "Bildunterschrift"
|
||||||
|
|
||||||
|
thumbnail_paths = BDS.Media.thumbnail_paths(media)
|
||||||
|
|
||||||
|
Enum.each(Map.values(thumbnail_paths), fn path ->
|
||||||
|
assert File.exists?(Path.join(temp_dir, path))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "import_media generates the four thumbnail files in bucketed thumbnail paths", %{project: project, temp_dir: temp_dir} do
|
||||||
|
source_path = Path.join(temp_dir, "sample.jpg")
|
||||||
|
File.write!(source_path, "fake-jpeg")
|
||||||
|
|
||||||
|
assert {:ok, media} = BDS.Media.import_media(%{project_id: project.id, source_path: source_path})
|
||||||
|
|
||||||
|
thumbnail_paths = BDS.Media.thumbnail_paths(media)
|
||||||
|
assert thumbnail_paths.small == "thumbnails/#{String.slice(media.id, 0, 2)}/#{media.id}-small.webp"
|
||||||
|
assert thumbnail_paths.medium == "thumbnails/#{String.slice(media.id, 0, 2)}/#{media.id}-medium.webp"
|
||||||
|
assert thumbnail_paths.large == "thumbnails/#{String.slice(media.id, 0, 2)}/#{media.id}-large.webp"
|
||||||
|
assert thumbnail_paths.ai == "thumbnails/#{String.slice(media.id, 0, 2)}/#{media.id}-ai.jpg"
|
||||||
|
|
||||||
|
Enum.each(Map.values(thumbnail_paths), fn path ->
|
||||||
|
assert File.exists?(Path.join(temp_dir, path))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "upsert_media_translation persists the row and writes a translated sidecar next to the binary", %{project: project, temp_dir: temp_dir} do
|
||||||
|
source_path = Path.join(temp_dir, "sample.txt")
|
||||||
|
File.write!(source_path, "hello media")
|
||||||
|
|
||||||
|
assert {:ok, media} = BDS.Media.import_media(%{project_id: project.id, source_path: source_path})
|
||||||
|
|
||||||
|
assert {:ok, translation} =
|
||||||
|
BDS.Media.upsert_media_translation(media.id, "de", %{
|
||||||
|
title: "Titel",
|
||||||
|
alt: "Alt text",
|
||||||
|
caption: "Bildunterschrift"
|
||||||
|
})
|
||||||
|
|
||||||
|
assert translation.translation_for == media.id
|
||||||
|
assert translation.language == "de"
|
||||||
|
assert translation.title == "Titel"
|
||||||
|
|
||||||
|
translated_sidecar_path = Path.join(temp_dir, media.file_path <> ".de.meta")
|
||||||
|
contents = File.read!(translated_sidecar_path)
|
||||||
|
assert contents =~ "translation_for: #{media.id}\n"
|
||||||
|
assert contents =~ "language: de\n"
|
||||||
|
assert contents =~ "title: Titel\n"
|
||||||
|
assert contents =~ "alt: Alt text\n"
|
||||||
|
assert contents =~ "caption: Bildunterschrift\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user