Adds media library advanced options

This commit is contained in:
BinaryAlley committed 2025-01-25 12:27:50 +02:00
1 parent 1c28355197
commit f89ba4960a
60 files changed
+1877 -51

No files matched your search

@@ -15,10 +15,18 @@ namespace Lumina.Application.Core.MediaLibrary.Management.Commands.AddLibrary;
/// <param name="LibraryType">The type of the media library.</param>
/// <param name="ContentLocations">The file system paths of the directories where the media library elements are located.</param>
/// <param name="CoverImage">The path of the image file used as the cover for the library.</param>
/// <param name="IsEnabled">Whether this media library is enabled or not. A disabled media library is never shown or changed.</param>
/// <param name="IsLocked">Whether this media library is locked or not. A locked media library is displayed, but is never changed or updated.</param>
/// <param name="DownloadMedatadaFromWeb">Whether this media library should update the metadata of its elements from the web, or not.</param>
/// <param name="SaveMetadataInMediaDirectories">Whether this media library should copy the downloaded metadata into the media library content locations, or not.</param>
[DebuggerDisplay("Title: {Title}")]
public record AddLibraryCommand(
string? Title,
string? LibraryType,
string[]? ContentLocations,
string? CoverImage
string? CoverImage,
bool IsEnabled,
bool IsLocked,
bool DownloadMedatadaFromWeb,
bool SaveMetadataInMediaDirectories
) : IRequest<ErrorOr<LibraryResponse>>;
@@ -68,6 +68,12 @@ public class AddLibraryCommandHandler : IRequestHandler<AddLibraryCommand, Error
/// </returns>
public async ValueTask<ErrorOr<LibraryResponse>> Handle(AddLibraryCommand request, CancellationToken cancellationToken)
{
// if the user that made the request is not an Admin or they don't have the permission to manage media libraries, they do not have the right to create it
if (!await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false) &&
!await _authorizationService.HasPermissionAsync(_currentUserService.UserId!.Value, AuthorizationPermission.CanCreateLibraries, cancellationToken).ConfigureAwait(false))
return ApplicationErrors.Authorization.NotAuthorized;
// make sure the file is an actual supported image
if (request.CoverImage is not null)
{
@@ -88,14 +94,13 @@ public class AddLibraryCommandHandler : IRequestHandler<AddLibraryCommand, Error
request.Title!,
Enum.Parse<LibraryType>(request.LibraryType!),
request.ContentLocations!,
request.CoverImage
request.CoverImage,
request.IsEnabled,
request.IsLocked,
request.DownloadMedatadaFromWeb,
request.SaveMetadataInMediaDirectories
);
// if the user that made the request is not an Admin or they don't have the permission to manage media libraries, they do not have the right to create it
if (!await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false) &&
!await _authorizationService.HasPermissionAsync(_currentUserService.UserId!.Value, AuthorizationPermission.CanCreateLibraries, cancellationToken).ConfigureAwait(false))
return ApplicationErrors.Authorization.NotAuthorized;
if (createLibraryResult.IsError)
return createLibraryResult.Errors;
@@ -58,9 +58,9 @@ public class ScanLibrariesCommandHandler : IRequestHandler<ScanLibrariesCommand,
if (!await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false))
return Errors.Authorization.NotAuthorized;
// get all media libraries from the persistence medium
// get all media libraries that are enabled and unlocked from the persistence medium
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
ErrorOr<IEnumerable<LibraryEntity>> getLibrariesResult = await libraryRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
ErrorOr<IEnumerable<LibraryEntity>> getLibrariesResult = await libraryRepository.GetAllEnabledAndUnlockedAsync(cancellationToken).ConfigureAwait(false);
if (getLibrariesResult.IsError)
return getLibrariesResult.Errors;
@@ -17,6 +17,10 @@ namespace Lumina.Application.Core.MediaLibrary.Management.Commands.UpdateLibrary
/// <param name="LibraryType">The type of the media library.</param>
/// <param name="ContentLocations">The file system paths of the directories where the media library elements are located.</param>
/// <param name="CoverImage">The path of the image file used as the cover for the library.</param>
/// <param name="IsEnabled">Whether this media library is enabled or not. A disabled media library is never shown or changed.</param>
/// <param name="IsLocked">Whether this media library is locked or not. A locked media library is displayed, but is never changed or updated.</param>
/// <param name="DownloadMedatadaFromWeb">Whether this media library should update the metadata of its elements from the web, or not.</param>
/// <param name="SaveMetadataInMediaDirectories">Whether this media library should copy the downloaded metadata into the media library content locations, or not.</param>
[DebuggerDisplay("Title: {Title}")]
public record UpdateLibraryCommand(
Guid Id,
@@ -24,5 +28,9 @@ public record UpdateLibraryCommand(
string? Title,
string? LibraryType,
string[]? ContentLocations,
string? CoverImage
string? CoverImage,
bool IsEnabled,
bool IsLocked,
bool DownloadMedatadaFromWeb,
bool SaveMetadataInMediaDirectories
) : IRequest<ErrorOr<LibraryResponse>>;
@@ -103,7 +103,11 @@ public class UpdateLibraryCommandHandler : IRequestHandler<UpdateLibraryCommand,
request.Title!,
Enum.Parse<LibraryType>(request.LibraryType!),
request.ContentLocations!,
request.CoverImage
request.CoverImage,
request.IsEnabled,
request.IsLocked,
request.DownloadMedatadaFromWeb,
request.SaveMetadataInMediaDirectories
);
if (createLibraryResult.IsError)
return createLibraryResult.Errors;
@@ -0,0 +1,12 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.MediaLibrary.Management;
using Mediator;
#endregion
namespace Lumina.Application.Core.MediaLibrary.Management.Queries.GetEnabledLibraries;
/// <summary>
/// Query for getting the list of enabled media libraries.
/// </summary>
public record GetEnabledLibrariesQuery : IRequest<ErrorOr<LibraryResponse[]>>;
@@ -0,0 +1,73 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
using Lumina.Contracts.Responses.MediaLibrary.Management;
using Mediator;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.MediaLibrary.Management.Queries.GetEnabledLibraries;
/// <summary>
/// Handler for the query to get the enabled media libraries.
/// </summary>
public class GetEnabledLibrariesQueryHandler : IRequestHandler<GetEnabledLibrariesQuery, ErrorOr<LibraryResponse[]>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly ICurrentUserService _currentUserService;
private readonly IAuthorizationService _authorizationService;
/// <summary>
/// Initializes a new instance of the <see cref="GetEnabledLibrariesQueryHandler"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public GetEnabledLibrariesQueryHandler(
IAuthorizationService authorizationService,
ICurrentUserService currentUserService,
IUnitOfWork unitOfWork)
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_unitOfWork = unitOfWork;
}
/// <summary>
/// Handles the query to get the enabled media libraries.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a successfuly created <see cref="LibraryResponse"/>, or an error message.
/// </returns>
public async ValueTask<ErrorOr<LibraryResponse[]>> Handle(GetEnabledLibrariesQuery request, CancellationToken cancellationToken)
{
// get the enabled libraries from the repository
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
ErrorOr<IEnumerable<LibraryEntity>> getEnabledLibrariesResult = await libraryRepository.GetAllEnabledAsync(cancellationToken).ConfigureAwait(false);
if (getEnabledLibrariesResult.IsError)
return getEnabledLibrariesResult.Errors;
// admins can see all libraries
if (await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false))
return ErrorOrFactory.From(getEnabledLibrariesResult.Value.Select(library => library.ToResponse()).ToArray());
else
{
// for regular users, only take the libraries that belong to them
LibraryResponse[] userEnabledLibraries = getEnabledLibrariesResult.Value
.Where(library => library.UserId == _currentUserService.UserId)
.Select(library => library.ToResponse()).ToArray();
return ErrorOrFactory.From(userEnabledLibraries);
}
}
}
@@ -17,7 +17,7 @@ using System.Threading.Tasks;
namespace Lumina.Application.Core.MediaLibrary.Management.Queries.GetLibraries;
/// <summary>
/// Handler for the query to authenticate an account.
/// Handler for the query to get the media libraries.
/// </summary>
public class GetLibrariesQueryHandler : IRequestHandler<GetLibrariesQuery, ErrorOr<LibraryResponse[]>>
{
@@ -42,7 +42,7 @@ public class GetLibrariesQueryHandler : IRequestHandler<GetLibrariesQuery, Error
}
/// <summary>
/// Handles the query to authenticate an account.
/// Handles the query to get the media libraries.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
@@ -51,7 +51,7 @@ public class GetLibrariesQueryHandler : IRequestHandler<GetLibrariesQuery, Error
/// </returns>
public async ValueTask<ErrorOr<LibraryResponse[]>> Handle(GetLibrariesQuery request, CancellationToken cancellationToken)
{
// get the library with the specified id from the repository
// get the libraries from the repository
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
ErrorOr<IEnumerable<LibraryEntity>> getLibrariesResult = await libraryRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
if (getLibrariesResult.IsError)
@@ -42,9 +42,12 @@ internal class BookLibraryTypeScanner : IBookLibraryTypeScanner
hashComparerJob.AddParent(fileSystemDiscoveryJob);
hashComparerJob.AddParent(repositoryMetadataDiscoveryJob);
hashComparerJob.AddChild(goodReadsMetadataScrapJob);
goodReadsMetadataScrapJob.AddParent(hashComparerJob);
if (library.DownloadMedatadaFromWeb)
{
hashComparerJob.AddChild(goodReadsMetadataScrapJob);
goodReadsMetadataScrapJob.AddParent(hashComparerJob);
}
// return the top level jobs that will be triggered when the scan will be started
yield return fileSystemDiscoveryJob;