Adds independent metadata and artwork enrichment

Splits the single combined enrichment phase of the media library scan into
independent, modular jobs, so that the metadata and the artwork of a book
are enriched separately and only when their respective triggers fire.

- Adds the MediaContributorRoleCategory enum (Other first, then Author,
  Translator, Illustrator, Publisher and Narrator) and makes the role
  category canonical, so that roles describing the same kind of
  contribution are never treated as distinct.
- Realizes the media contributor bounded context: a MediaContributor is a
  person, unique by display name, without a role. The role moves to the
  per-work participation (BookContributors), so that a single contributor
  can play multiple roles. Contributors are created and linked by the
  metadata enrichment job, and exposed to subsequent jobs.
- Makes the Book aggregate pure: the enrichment tracking state (metadata
  status, providers, timestamps and covers) no longer lives on the domain
  aggregate, but stays in the Books table, managed directly by the jobs.
- Models artwork generically as a BookArtwork child table (type, ordinal,
  content hash, status and provider), so that each piece of artwork is
  tracked independently and a change of one does not require the others to
  be re-fetched.
- Splits the scan DAG into ResultsSave, ProviderConfigurationInvalidation,
  MetadataEnrichment and ArtworkEnrichment jobs. The invalidation job
  fingerprints the provider configuration of a library and resets the
  enrichment state of the affected channel when it changed. The artwork job
  resolves the missing artwork, hashing it to detect identical covers and
  skip re-copying. Content-changed books are reset by the results save job.
- Deletes the book and its stored artwork when the file disappears from the
  library.
- Adds the migration creating the new tables, backfilling the existing
  covers into the artwork table, and adding the configuration fingerprints.
- Localizes the new scan job stages, and fixes the invalidation job wiping
  the library content locations by persisting the fingerprints through the
  change tracker instead of the repository update action.
This commit is contained in:
BinaryAlley committed 2026-08-27 14:41:49 +03:00
1 parent 4ee2976806
commit 565fe68239
98 files changed
+5845 -630

No files matched your search

@@ -19,7 +19,6 @@ public static partial class Errors
public static Error RoleNameCannotBeEmpty => Error.Validation(description: nameof(RoleNameCannotBeEmpty));
public static Error RoleNameMustBeMaximum50CharactersLong => Error.Validation(description: nameof(RoleNameMustBeMaximum50CharactersLong));
public static Error RoleCategoryCannotBeEmpty => Error.Validation(description: nameof(RoleCategoryCannotBeEmpty));
public static Error RoleCategoryMustBeMaximum50CharactersLong => Error.Validation(description: nameof(RoleCategoryMustBeMaximum50CharactersLong));
public static Error ContributorRoleCannotBeNull => Error.Validation(description: nameof(ContributorRoleCannotBeNull));
}
}
@@ -0,0 +1,17 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
#endregion
namespace Lumina.Domain.Common.Errors;
/// <summary>
/// Media contributor error types.
/// </summary>
public static partial class Errors
{
public static class MediaContributors
{
public static Error MediaContributorNameCannotBeEmpty => Error.Validation(description: nameof(MediaContributorNameCannotBeEmpty));
public static Error MediaContributorRoleNameCannotBeEmpty => Error.Validation(description: nameof(MediaContributorRoleNameCannotBeEmpty));
}
}
@@ -0,0 +1,8 @@
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for resolving the artwork of the media library items, using the artwork providers configured for the media library.
/// </summary>
public interface IMediaLibraryScanArtworkEnrichmentJob : IMediaLibraryScanJob
{
}
@@ -0,0 +1,9 @@
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
/// <summary>
/// Interface for the media library scan job for invalidating the enrichment state of the media library items
/// whose metadata or artwork providers changed since the last scan.
/// </summary>
public interface IMediaLibraryScanProviderConfigurationInvalidationJob : IMediaLibraryScanJob
{
}
@@ -6,11 +6,13 @@ using System;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.MediaContributorBoundedContext.MediaContributorAggregate;
/// <summary>
/// Aggregate Root for a media contributor.
/// Aggregate root for a media contributor, the person that contributed to a media item.
/// A media contributor is a person, unique by name, agnostic of the kind of media it contributed to, and of
/// the roles it played in the media items. The roles a contributor plays are tracked per media item, so that a
/// single contributor that is both an actor and a writer is never duplicated.
/// </summary>
[DebuggerDisplay("{Id}: {Name}")]
public class MediaContributor : AggregateRoot<MediaContributorId>
@@ -20,11 +22,6 @@ public class MediaContributor : AggregateRoot<MediaContributorId>
/// </summary>
public MediaContributorName Name { get; private set; }
/// <summary>
/// Gets the role of the contributor.
/// </summary>
public MediaContributorRole Role { get; private set; }
/// <summary>
/// Gets the biography of the contributor.
/// </summary>
@@ -45,19 +42,19 @@ public class MediaContributor : AggregateRoot<MediaContributorId>
/// </summary>
/// <param name="id">The unique identifier of the contributor.</param>
/// <param name="name">The name of the contributor.</param>
/// <param name="role">The role of the contributor.</param>
/// <param name="biography">The optional biography of the contributor.</param>
/// <param name="dateOfBirth">The optional date of birth of the contributor.</param>
/// <param name="dateOfBirth">The optional date of death of the contributor.</param>
/// <param name="dateOfDeath">The optional date of death of the contributor.</param>
private MediaContributor(
MediaContributorId id,
MediaContributorName name,
MediaContributorRole role,
Optional<string> biography,
Optional<DateOnly> dateOfBirth,
Optional<DateOnly> dateOfDeath) : base(id)
{
Id = id;
Name = name;
Role = role;
Biography = biography;
DateOfBirth = dateOfBirth;
DateOfDeath = dateOfDeath;
}
@@ -69,9 +66,12 @@ public class MediaContributor : AggregateRoot<MediaContributorId>
/// <returns>The created <see cref="MediaContributor"/> instance.</returns>
public static Result<MediaContributor> Create(MediaContributorName name)
{
// TODO: enforce invariants
throw new NotImplementedException();
//return new MediaContributor(id, name);
return new MediaContributor(
MediaContributorId.CreateUnique(),
name,
Optional<string>.None(),
Optional<DateOnly>.None(),
Optional<DateOnly>.None());
}
/// <summary>
@@ -79,14 +79,17 @@ public class MediaContributor : AggregateRoot<MediaContributorId>
/// </summary>
/// <param name="id">The unique identifier of the contributor.</param>
/// <param name="name">The name of the contributor.</param>
/// <param name="biography">The optional biography of the contributor.</param>
/// <param name="dateOfBirth">The optional date of birth of the contributor.</param>
/// <param name="dateOfDeath">The optional date of death of the contributor.</param>
/// <returns>The created <see cref="MediaContributor"/> instance.</returns>
public static Result<MediaContributor> Create(
MediaContributorId id,
MediaContributorName name,
MediaContributorRole role,
Optional<string> biography,
Optional<DateOnly> dateOfBirth,
Optional<DateOnly> dateOfDeath)
{
return new MediaContributor(id, name, role, dateOfBirth, dateOfDeath);
return new MediaContributor(id, name, biography, dateOfBirth, dateOfDeath);
}
}
@@ -1,4 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Models.Core;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
@@ -42,7 +43,9 @@ public sealed class MediaContributorName : ValueObject
/// <returns>The created <see cref="MediaContributorName"/> instance.</returns>
public static Result<MediaContributorName> Create(string displayName, Optional<string> legalName)
{
// TODO: enforce invariants
if (string.IsNullOrWhiteSpace(displayName))
return Errors.MediaContributors.MediaContributorNameCannotBeEmpty;
return new MediaContributorName(displayName, legalName);
}
@@ -1,5 +1,8 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Models.Core;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.MediaContributors;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
@@ -7,42 +10,47 @@ using System.Diagnostics;
namespace Lumina.Domain.Core.BoundedContexts.MediaContributorBoundedContext.MediaContributorAggregate.ValueObjects;
/// <summary>
/// Value Object for the role of a media contributor.
/// Value Object for the role of a media contributor in a media item.
/// The role carries both a free-form display name, as returned by the metadata providers, and the canonical
/// category the display name normalizes to, so that roles describing the same kind of contribution are never
/// treated as distinct.
/// </summary>
[DebuggerDisplay("{DisplayName}")]
public sealed class MediaContributorRole : ValueObject
{
/// <summary>
/// Gets the value representing this object.
/// Gets the display name of the role.
/// </summary>
public string Name { get; }
public string DisplayName { get; }
/// <summary>
/// Gets the category of this object.
/// Gets the canonical category of the role.
/// </summary>
public string Category { get; }
public MediaContributorRoleCategory Category { get; }
/// <summary>
/// Initializes a new instance of the <see cref="MediaContributorRole"/> class.
/// </summary>
/// <param name="name">The value representing this object.</param>
/// <param name="category">The category of this object.</param>
private MediaContributorRole(string name, string category)
/// <param name="displayName">The display name of the role.</param>
/// <param name="category">The canonical category of the role.</param>
private MediaContributorRole(string displayName, MediaContributorRoleCategory category)
{
Name = name;
DisplayName = displayName;
Category = category;
}
/// <summary>
/// Creates a new instance of the <see cref="MediaContributorRole"/> class, from a pre-existing <paramref name="value"/>.
/// Creates a new instance of the <see cref="MediaContributorRole"/> class, from a display <paramref name="displayName"/> and its canonical <paramref name="category"/>.
/// </summary>
/// <param name="name">The value representing this object.</param>
/// <param name="category">The category of this object.</param>
/// <param name="displayName">The display name of the role.</param>
/// <param name="category">The canonical category of the role.</param>
/// <returns>The created <see cref="MediaContributorRole"/> instance.</returns>
public static MediaContributorRole Create(string name, string category)
public static Result<MediaContributorRole> Create(string displayName, MediaContributorRoleCategory category)
{
// TODO: enforce invariants
return new MediaContributorRole(name, category);
if (string.IsNullOrWhiteSpace(displayName))
return Errors.MediaContributors.MediaContributorRoleNameCannotBeEmpty;
return new MediaContributorRole(displayName, category);
}
/// <summary>
@@ -51,7 +59,7 @@ public sealed class MediaContributorRole : ValueObject
/// <returns>A list of items defining the equality.</returns>
public override IEnumerable<object> GetEqualityComponents()
{
yield return Name;
yield return DisplayName;
yield return Category;
}
}
@@ -38,21 +38,6 @@ public sealed class Book : AggregateRoot<BookId>
/// </summary>
public string Path { get; private set; }
/// <summary>
/// Gets the status of the metadata enrichment of the book.
/// </summary>
public MetadataStatus MetadataStatus { get; private set; }
/// <summary>
/// Gets the date and time when the metadata of the book was last enriched, if applicable.
/// </summary>
public Optional<DateTime> LastMetadataUpdateUtc { get; private set; }
/// <summary>
/// Gets the name of the plugin that enriched the metadata of the book, if applicable.
/// </summary>
public Optional<string> MetadataProvider { get; private set; }
/// <summary>
/// Gets the format of the book (e.g., Hardcover, Paperback), if applicable.
/// </summary>
@@ -118,11 +103,6 @@ public sealed class Book : AggregateRoot<BookId>
/// </summary>
public Optional<string> AppleBooksId { get; private set; }
/// <summary>
/// Gets the file system path of the cover image of the book, if applicable.
/// </summary>
public Optional<string> CoverImagePath { get; private set; }
/// <summary>
/// Gets the list of ISBN (International Standard Book Number) of the book.
/// </summary>
@@ -191,9 +171,6 @@ public sealed class Book : AggregateRoot<BookId>
LibraryId = libraryId;
Path = path;
Metadata = metadata;
MetadataStatus = MetadataStatus.Pending;
LastMetadataUpdateUtc = Optional<DateTime>.None();
MetadataProvider = Optional<string>.None();
Format = format;
Edition = edition;
VolumeNumber = volumeNumber;
@@ -207,7 +184,6 @@ public sealed class Book : AggregateRoot<BookId>
GoogleBooksId = googleBooksId;
BarnesAndNobleId = barnesAndNobleId;
AppleBooksId = appleBooksId;
CoverImagePath = Optional<string>.None();
CreatedOnUtc = createdOnUtc;
UpdatedOnUtc = updatedOnUtc.HasValue ? updatedOnUtc.Value : null;
_isbns = isbns;
@@ -368,36 +344,18 @@ public sealed class Book : AggregateRoot<BookId>
}
/// <summary>
/// Marks the metadata of the book as enriched by the provided <paramref name="providerName"/>.
/// Replaces the media contributors of the book with the provided <paramref name="contributors"/>, identified by their unique identifiers.
/// </summary>
/// <param name="providerName">The name of the metadata provider that enriched the book.</param>
/// <param name="lastUpdateUtc">The date and time when the metadata of the book was enriched.</param>
public void MarkMetadataAsEnriched(string providerName, DateTime lastUpdateUtc)
/// <param name="contributors">The unique identifiers of the media contributors of the book.</param>
public void UpdateContributors(IReadOnlyCollection<MediaContributorId> contributors)
{
MetadataProvider = providerName;
LastMetadataUpdateUtc = lastUpdateUtc;
MetadataStatus = MetadataStatus.Enriched;
// replace the contents of the collection in place, preserving the readonly reference invariants of the aggregate
_contributors.Clear();
_contributors.AddRange(contributors);
}
/// <summary>
/// Marks the metadata enrichment of the book as failed.
/// </summary>
public void MarkMetadataAsFailed()
{
MetadataStatus = MetadataStatus.Failed;
}
/// <summary>
/// Sets the file system path of the cover image of the book.
/// </summary>
/// <param name="coverImagePath">The file system path of the cover image of the book.</param>
public void SetBookCoverImagePath(string coverImagePath)
{
CoverImagePath = coverImagePath;
}
/// <summary>
/// Applies the enriched <paramref name="metadata"/> and the related fields to the book, marking its metadata as enriched by the provided <paramref name="providerName"/>.
/// Applies the enriched <paramref name="metadata"/> and the related fields to the book.
/// </summary>
/// <param name="metadata">The enriched metadata of the book.</param>
/// <param name="format">The optional format of the book.</param>
@@ -415,8 +373,6 @@ public sealed class Book : AggregateRoot<BookId>
/// <param name="appleBooksId">The optional Apple Books ID of the book.</param>
/// <param name="isbns">The list of ISBNs of the book.</param>
/// <param name="ratings">The list of ratings for the book.</param>
/// <param name="providerName">The name of the metadata provider that enriched the book.</param>
/// <param name="lastUpdateUtc">The date and time when the metadata was enriched.</param>
public void ApplyEnrichedMetadata(
WrittenContentMetadata metadata,
Optional<BookFormat> format,
@@ -433,9 +389,7 @@ public sealed class Book : AggregateRoot<BookId>
Optional<string> barnesAndNobleId,
Optional<string> appleBooksId,
List<Isbn> isbns,
List<BookRating> ratings,
string providerName,
DateTime lastUpdateUtc)
List<BookRating> ratings)
{
Metadata = metadata;
Format = format;
@@ -456,6 +410,5 @@ public sealed class Book : AggregateRoot<BookId>
_isbns.AddRange(isbns);
_ratings.Clear();
_ratings.AddRange(ratings);
MarkMetadataAsEnriched(providerName, lastUpdateUtc);
}
}
@@ -52,12 +52,23 @@ internal class BookLibraryTypeScanner : IBookLibraryTypeScanner
mediaLibraryScanHashJob.AddChild(mediaLibraryScanResultsSaveJob);
mediaLibraryScanResultsSaveJob.AddParent(mediaLibraryScanHashJob);
// the metadata enrichment job is always the last job in the directed acyclic job graph, running after the scan results save job,
// so that the books are materialized before their metadata is enriched. The job itself is the host-side gate that skips the providers
// requiring access to the web when the media library does not permit downloading data from the web, so that the local providers still run offline
// the enrichment of the media library items is split into independent, modular jobs that run sequentially: the provider configuration
// invalidation job invalidates the enrichment state of the items whose metadata or artwork providers changed since the last scan, the
// metadata enrichment job enriches the metadata and links the media contributors, and the artwork enrichment job resolves the artwork.
// The artwork enrichment job is always the last job in the directed acyclic job graph, running after the metadata enrichment job, so that
// the artwork is resolved after the metadata, and thus after the contributors that provide the author names used to build the artwork directories.
IMediaLibraryScanProviderConfigurationInvalidationJob mediaLibraryScanProviderConfigurationInvalidationJob = _mediaScanJobFactory.CreateJob<IMediaLibraryScanProviderConfigurationInvalidationJob>(libraryId);
IMediaLibraryScanMetadataEnrichmentJob mediaLibraryScanMetadataEnrichmentJob = _mediaScanJobFactory.CreateJob<IMediaLibraryScanMetadataEnrichmentJob>(libraryId);
mediaLibraryScanResultsSaveJob.AddChild(mediaLibraryScanMetadataEnrichmentJob);
mediaLibraryScanMetadataEnrichmentJob.AddParent(mediaLibraryScanResultsSaveJob);
IMediaLibraryScanArtworkEnrichmentJob mediaLibraryScanArtworkEnrichmentJob = _mediaScanJobFactory.CreateJob<IMediaLibraryScanArtworkEnrichmentJob>(libraryId);
mediaLibraryScanResultsSaveJob.AddChild(mediaLibraryScanProviderConfigurationInvalidationJob);
mediaLibraryScanProviderConfigurationInvalidationJob.AddParent(mediaLibraryScanResultsSaveJob);
mediaLibraryScanProviderConfigurationInvalidationJob.AddChild(mediaLibraryScanMetadataEnrichmentJob);
mediaLibraryScanMetadataEnrichmentJob.AddParent(mediaLibraryScanProviderConfigurationInvalidationJob);
mediaLibraryScanMetadataEnrichmentJob.AddChild(mediaLibraryScanArtworkEnrichmentJob);
mediaLibraryScanArtworkEnrichmentJob.AddParent(mediaLibraryScanMetadataEnrichmentJob);
// return the top level jobs that will be triggered when the scan will be started
yield return fileSystemDiscoveryJob;