mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
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.
133 lines
7.3 KiB
C#
133 lines
7.3 KiB
C#
#region ========================================================================= USING =====================================================================================
|
|
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
|
using Lumina.Domain.Common.Primitives;
|
|
using Lumina.Domain.Common.ValueObjects.Metadata;
|
|
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
|
|
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.Entities;
|
|
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
|
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
#endregion
|
|
|
|
namespace Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
|
|
|
/// <summary>
|
|
/// Extension methods for applying <see cref="BookMetadataDto"/> to <see cref="Book"/>.
|
|
/// </summary>
|
|
public static class BookMetadataDtoMapping
|
|
{
|
|
/// <summary>
|
|
/// Applies the metadata of <paramref name="metadata"/> to <paramref name="book"/>.
|
|
/// </summary>
|
|
/// <param name="book">The domain book to which the metadata is applied.</param>
|
|
/// <param name="metadata">The metadata to be applied.</param>
|
|
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
|
public static Result<Success> ApplyMetadata(this Book book, BookMetadataDto metadata)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(metadata.Title))
|
|
return Domain.Common.Errors.Errors.Metadata.TitleCannotBeEmpty;
|
|
if (metadata.ReleaseInfo is null)
|
|
return Domain.Common.Errors.Errors.Metadata.ReleaseInfoCannotBeNull;
|
|
|
|
Result<ReleaseInfo> releaseInfoResult = ReleaseInfo.Create(
|
|
Optional<DateOnly>.FromNullable(metadata.ReleaseInfo.OriginalReleaseDate),
|
|
Optional<int>.FromNullable(metadata.ReleaseInfo.OriginalReleaseYear),
|
|
Optional<DateOnly>.FromNullable(metadata.ReleaseInfo.ReReleaseDate),
|
|
Optional<int>.FromNullable(metadata.ReleaseInfo.ReReleaseYear),
|
|
Optional<string>.FromNullable(metadata.ReleaseInfo.ReleaseCountry),
|
|
Optional<string>.FromNullable(metadata.ReleaseInfo.ReleaseVersion)
|
|
);
|
|
if (releaseInfoResult.IsFailure)
|
|
return releaseInfoResult.Errors;
|
|
|
|
List<Result<Genre>> domainGenresResult = metadata.Genres is not null
|
|
? [.. metadata.Genres.Select(genre => Genre.Create(genre.Name))]
|
|
: [];
|
|
List<Error> genresErrors = [.. domainGenresResult.Where(genreResult => genreResult.IsFailure).SelectMany(genreResult => genreResult.Errors)];
|
|
if (genresErrors.Count != 0)
|
|
return genresErrors;
|
|
|
|
List<Result<Tag>> domainTagsResult = metadata.Tags is not null
|
|
? [.. metadata.Tags.Select(tag => Tag.Create(tag.Name))]
|
|
: [];
|
|
List<Error> tagsErrors = [.. domainTagsResult.Where(tagResult => tagResult.IsFailure).SelectMany(tagResult => tagResult.Errors)];
|
|
if (tagsErrors.Count != 0)
|
|
return tagsErrors;
|
|
|
|
Optional<LanguageInfo> languageInfo = Optional<LanguageInfo>.None();
|
|
if (metadata.Language is not null)
|
|
{
|
|
Result<LanguageInfo> languageInfoResult = LanguageInfo.Create(metadata.Language.LanguageCode, metadata.Language.LanguageName, Optional<string>.FromNullable(metadata.Language.NativeName));
|
|
if (languageInfoResult.IsFailure)
|
|
return languageInfoResult.Errors;
|
|
languageInfo = languageInfoResult.Value;
|
|
}
|
|
|
|
Optional<LanguageInfo> originalLanguageInfo = Optional<LanguageInfo>.None();
|
|
if (metadata.OriginalLanguage is not null)
|
|
{
|
|
Result<LanguageInfo> originalLanguageInfoResult = LanguageInfo.Create(metadata.OriginalLanguage.LanguageCode, metadata.OriginalLanguage.LanguageName, Optional<string>.FromNullable(metadata.OriginalLanguage.NativeName));
|
|
if (originalLanguageInfoResult.IsFailure)
|
|
return originalLanguageInfoResult.Errors;
|
|
originalLanguageInfo = originalLanguageInfoResult.Value;
|
|
}
|
|
|
|
Result<WrittenContentMetadata> writtenContentMetadataResult = WrittenContentMetadata.Create(
|
|
metadata.Title,
|
|
Optional<string>.FromNullable(metadata.OriginalTitle),
|
|
Optional<string>.FromNullable(metadata.Description),
|
|
releaseInfoResult.Value,
|
|
[.. domainGenresResult.Select(genreResult => genreResult.Value)],
|
|
[.. domainTagsResult.Select(tagResult => tagResult.Value)],
|
|
languageInfo,
|
|
originalLanguageInfo,
|
|
Optional<string>.FromNullable(metadata.Publisher),
|
|
Optional<int>.FromNullable(metadata.PageCount)
|
|
);
|
|
if (writtenContentMetadataResult.IsFailure)
|
|
return writtenContentMetadataResult.Errors;
|
|
|
|
List<Result<Isbn>> domainIsbnsResult = metadata.Isbns is not null
|
|
? [.. metadata.Isbns.Where(isbn => isbn.Value is not null && isbn.Format is not null).Select(isbn => Isbn.Create(isbn.Value, (IsbnFormat)(int)isbn.Format!.Value))]
|
|
: [];
|
|
List<Error> isbnsErrors = [.. domainIsbnsResult.Where(isbnResult => isbnResult.IsFailure).SelectMany(isbnResult => isbnResult.Errors)];
|
|
if (isbnsErrors.Count != 0)
|
|
return isbnsErrors;
|
|
|
|
List<Result<BookRating>> domainRatingsResult = metadata.Ratings is not null
|
|
? [.. metadata.Ratings.Where(rating => rating.Value is not null && rating.MaxValue is not null).Select(rating => BookRating.Create(
|
|
rating.Value!.Value,
|
|
rating.MaxValue!.Value,
|
|
Optional<BookRatingSource>.FromNullable((BookRatingSource?)(rating.Source is null ? null : (BookRatingSource)(int)rating.Source.Value)),
|
|
Optional<int>.FromNullable(rating.VoteCount)
|
|
))]
|
|
: [];
|
|
List<Error> ratingsErrors = [.. domainRatingsResult.Where(ratingResult => ratingResult.IsFailure).SelectMany(ratingResult => ratingResult.Errors)];
|
|
if (ratingsErrors.Count != 0)
|
|
return ratingsErrors;
|
|
|
|
book.ApplyEnrichedMetadata(
|
|
writtenContentMetadataResult.Value,
|
|
Optional<BookFormat>.FromNullable(metadata.Format.HasValue ? (BookFormat)(int)metadata.Format.Value : (BookFormat?)null),
|
|
Optional<string>.FromNullable(metadata.Edition),
|
|
Optional<float>.FromNullable(metadata.VolumeNumber),
|
|
Optional<BookSeries>.None(),
|
|
Optional<string>.FromNullable(metadata.ASIN),
|
|
Optional<string>.FromNullable(metadata.GoodreadsId),
|
|
Optional<string>.FromNullable(metadata.LCCN),
|
|
Optional<string>.FromNullable(metadata.OCLCNumber),
|
|
Optional<string>.FromNullable(metadata.OpenLibraryId),
|
|
Optional<string>.FromNullable(metadata.LibraryThingId),
|
|
Optional<string>.FromNullable(metadata.GoogleBooksId),
|
|
Optional<string>.FromNullable(metadata.BarnesAndNobleId),
|
|
Optional<string>.FromNullable(metadata.AppleBooksId),
|
|
[.. domainIsbnsResult.Select(isbnResult => isbnResult.Value)],
|
|
[.. domainRatingsResult.Select(ratingResult => ratingResult.Value)]
|
|
);
|
|
|
|
return Result.Success;
|
|
}
|
|
}
|