mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 10:11:10 +00:00
Adds media library advanced options
This commit is contained in:
1 parent
1c28355197
commit
f89ba4960a
60 files changed
+1877
-51
No files matched your search
+20
@@ -45,6 +45,26 @@ public class LibraryEntity : IStorageEntity, IAuditableEntity
|
||||
/// </summary>
|
||||
public required ICollection<LibraryContentLocationEntity> ContentLocations { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether this media library is enabled or not. A disabled media library is never shown or changed.
|
||||
/// </summary>
|
||||
public bool IsEnabled { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether this media library is locked or not. A locked media library is displayed, but is never changed or updated.
|
||||
/// </summary>
|
||||
public bool IsLocked { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether this media library should update the metadata of its elements from the web, or not.
|
||||
/// </summary>
|
||||
public bool DownloadMedatadaFromWeb { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether this media library should copy the downloaded metadata into the media library content locations, or not.
|
||||
/// </summary>
|
||||
public bool SaveMetadataInMediaDirectories { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the user that owns the media library.
|
||||
/// </summary>
|
||||
|
||||
+17
@@ -1,8 +1,12 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
@@ -17,4 +21,17 @@ public interface ILibraryRepository : IRepository<LibraryEntity>,
|
||||
IUpdateRepositoryAction<LibraryEntity>,
|
||||
IDeleteByIdRepositoryAction<Guid>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets all media libraries that are marked as enabled and unlocked, from the storage medium.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="LibraryEntity"/>, or an error.</returns>
|
||||
Task<ErrorOr<IEnumerable<LibraryEntity>>> GetAllEnabledAndUnlockedAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all media libraries that are marked as enabled from the storage medium.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="LibraryEntity"/>, or an error.</returns>
|
||||
Task<ErrorOr<IEnumerable<LibraryEntity>>> GetAllEnabledAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
+5
-1
@@ -24,7 +24,11 @@ public static class AddMediaLibraryRequestMapping
|
||||
request.Title,
|
||||
request.LibraryType,
|
||||
request.ContentLocations,
|
||||
request.CoverImage
|
||||
request.CoverImage,
|
||||
request.IsEnabled,
|
||||
request.IsLocked,
|
||||
request.DownloadMedatadaFromWeb,
|
||||
request.SaveMetadataInMediaDirectories
|
||||
);
|
||||
}
|
||||
}
|
||||
+9
-1
@@ -29,6 +29,10 @@ public static class LibraryEntityMapping
|
||||
repositoryEntity.LibraryType,
|
||||
repositoryEntity.ContentLocations.Select(location => location.Path).ToList(),
|
||||
repositoryEntity.CoverImage,
|
||||
repositoryEntity.IsEnabled,
|
||||
repositoryEntity.IsLocked,
|
||||
repositoryEntity.DownloadMedatadaFromWeb,
|
||||
repositoryEntity.SaveMetadataInMediaDirectories,
|
||||
repositoryEntity.CreatedOnUtc,
|
||||
repositoryEntity.UpdatedOnUtc
|
||||
);
|
||||
@@ -47,7 +51,11 @@ public static class LibraryEntityMapping
|
||||
repositoryEntity.Title,
|
||||
repositoryEntity.LibraryType,
|
||||
repositoryEntity.ContentLocations.Select(contentLocation => contentLocation.Path),
|
||||
repositoryEntity.CoverImage
|
||||
repositoryEntity.CoverImage,
|
||||
repositoryEntity.IsEnabled,
|
||||
repositoryEntity.IsLocked,
|
||||
repositoryEntity.DownloadMedatadaFromWeb,
|
||||
repositoryEntity.SaveMetadataInMediaDirectories
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,10 @@ public static class LibraryMapping
|
||||
UserId = domainEntity.UserId.Value,
|
||||
ContentLocations = domainEntity.ContentLocations.Select(path => new LibraryContentLocationEntity() { Path = path.Path }).ToList(),
|
||||
CoverImage = domainEntity.CoverImage,
|
||||
IsEnabled = domainEntity.IsEnabled,
|
||||
IsLocked = domainEntity.IsLocked,
|
||||
DownloadMedatadaFromWeb = domainEntity.DownloadMedatadaFromWeb,
|
||||
SaveMetadataInMediaDirectories = domainEntity.SaveMetadataInMediaDirectories,
|
||||
CreatedOnUtc = domainEntity.CreatedOnUtc,
|
||||
CreatedBy = Guid.Empty,
|
||||
UpdatedOnUtc = domainEntity.UpdatedOnUtc.HasValue ? domainEntity.UpdatedOnUtc : null,
|
||||
|
||||
+5
-1
@@ -24,7 +24,11 @@ public static class UpdateLibraryRequestMapping
|
||||
request.Title,
|
||||
request.LibraryType,
|
||||
request.ContentLocations,
|
||||
request.CoverImage
|
||||
request.CoverImage,
|
||||
request.IsEnabled,
|
||||
request.IsLocked,
|
||||
request.DownloadMedatadaFromWeb,
|
||||
request.SaveMetadataInMediaDirectories
|
||||
);
|
||||
}
|
||||
}
|
||||
+9
-1
@@ -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>>;
|
||||
+11
-6
@@ -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;
|
||||
|
||||
|
||||
+2
-2
@@ -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;
|
||||
|
||||
|
||||
+9
-1
@@ -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>>;
|
||||
+5
-1
@@ -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;
|
||||
|
||||
+12
@@ -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[]>>;
|
||||
+73
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -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)
|
||||
|
||||
+5
-2
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user