mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 12:01:10 +00:00
Fixes library cover image handling on save
Validates the cover image only when a new one is chosen, so an unchanged stored cover does not block saving unrelated changes, and keeps the cover already copied to the internal media directory instead of re-copying the stored relative path, which was treated as an absolute file system path.
This commit is contained in:
1 parent
00dcf3d471
commit
ccefb65c83
4 files changed
+101
-12
No files matched your search
+10
-9
@@ -93,8 +93,16 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
// make sure the file is an actual supported image
|
||||
if (command.CoverImage is not null)
|
||||
// get a library repository and retrieve the library to update
|
||||
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (getLibraryResult.IsFailure)
|
||||
return getLibraryResult.Errors;
|
||||
else if (getLibraryResult.Value is null)
|
||||
return DomainErrors.Library.LibraryNotFound;
|
||||
|
||||
// make sure the file is an actual supported image, but only when a new cover image was chosen; the stored cover image is
|
||||
// relative to the application base directory and is already validated, so it must not block saving unrelated changes
|
||||
if (command.CoverImage is not null && command.CoverImage != getLibraryResult.Value.CoverImage)
|
||||
{
|
||||
Result<FileSystemPathId> fileSystemPathIdResult = FileSystemPathId.Create(command.CoverImage);
|
||||
if (fileSystemPathIdResult.IsFailure)
|
||||
@@ -107,13 +115,6 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
|
||||
return DomainErrors.Library.CoverFileMustBeAnImage;
|
||||
}
|
||||
|
||||
// get a library repository and retrieve the library to update
|
||||
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (getLibraryResult.IsFailure)
|
||||
return getLibraryResult.Errors;
|
||||
else if (getLibraryResult.Value is null)
|
||||
return DomainErrors.Library.LibraryNotFound;
|
||||
|
||||
// admins or users with the permission to manage media libraries can update any library; other users can only update the libraries they own
|
||||
bool hasManagePermission = await _authorizationService.HasPermissionAsync(userId, AuthorizationPermission.CanCreateLibraries, cancellationToken).ConfigureAwait(false);
|
||||
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
|
||||
|
||||
+29
@@ -107,6 +107,22 @@ public class LibrarySavedDomainEventHandler : IDomainEventHandler<LibrarySavedDo
|
||||
return libraryPathResult.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves a media file path stored relative to the application base directory into an absolute file system path.
|
||||
/// The stored path was created by removing the base directory from the copied file path, so it is resolved by prepending the
|
||||
/// base directory back. The path is built directly instead of using <see cref="IPathService.CombinePath"/>, because that method
|
||||
/// treats the combined name as a directory segment and appends a trailing separator, which would make the file path invalid.
|
||||
/// </summary>
|
||||
/// <param name="relativePath">The path relative to the application base directory, with a leading path separator.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing the absolute path, or an error.</returns>
|
||||
private Result<FileSystemPathId> ResolveInternalMediaPathId(string relativePath)
|
||||
{
|
||||
string absolutePath = AppContext.BaseDirectory.TrimEnd(_pathService.PathSeparator)
|
||||
+ _pathService.PathSeparator
|
||||
+ relativePath.TrimStart(_pathService.PathSeparator);
|
||||
return FileSystemPathId.Create(absolutePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the cover image of a media library to the internal media directory.
|
||||
/// </summary>
|
||||
@@ -125,8 +141,21 @@ public class LibrarySavedDomainEventHandler : IDomainEventHandler<LibrarySavedDo
|
||||
Result<bool> fileExistsResult = _environmentContext.FileProviderService.FileExists(fileSystemPathIdResult.Value);
|
||||
if (fileExistsResult.IsFailure)
|
||||
return fileExistsResult.Errors;
|
||||
|
||||
// when the library is saved without changing its cover, the stored cover image path is already the internal media directory
|
||||
// path, relative to the application base directory; resolve it against the base directory and keep it when the file is found
|
||||
if (!fileExistsResult.Value)
|
||||
{
|
||||
Result<FileSystemPathId> internalImagePathIdResult = ResolveInternalMediaPathId(imagePath);
|
||||
if (internalImagePathIdResult.IsFailure)
|
||||
return internalImagePathIdResult.Errors;
|
||||
Result<bool> internalFileExistsResult = _environmentContext.FileProviderService.FileExists(internalImagePathIdResult.Value);
|
||||
if (internalFileExistsResult.IsFailure)
|
||||
return internalFileExistsResult.Errors;
|
||||
if (internalFileExistsResult.Value)
|
||||
return imagePath;
|
||||
return Errors.FileSystemManagement.FileNotFound;
|
||||
}
|
||||
|
||||
// make sure the file is an actual supported image
|
||||
Result<ImageType> imageCheckResult = await _environmentContext.FileTypeService.GetImageTypeAsync(fileSystemPathIdResult.Value, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
Reference in new issue
Block a user