mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +00:00
Adds book reading support with EPUB and PDF reader plugins
This commit is contained in:
1 parent
275fd83ff2
commit
f7ccdf3d1c
271 files changed
+11501
-361
No files matched your search
+1
-1
@@ -1,7 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Commands.AddBook;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.AddBook;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
using FastEndpoints;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Queries.GetBooks;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Queries.GetBooks;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Contracts.Responses.Common;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
using FastEndpoints;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Queries.GetBooksLite;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Queries.GetBooksLite;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Contracts.Responses.Common;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading.Queries.GetReadingAvailability;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.Library.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.Reading.GetReadingAvailability;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/books/{bookId}/reading/availability</c> route.
|
||||
/// </summary>
|
||||
public class GetReadingAvailabilityEndpoint : BaseEndpoint<GetReadingAvailabilityRequest, IResult>
|
||||
{
|
||||
private readonly IQueryHandler<GetReadingAvailabilityQuery, Result<ReadingAvailabilityResponse>> _getReadingAvailabilityQueryHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetReadingAvailabilityEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="getReadingAvailabilityQueryHandler">Injected service for handling get reading availability queries.</param>
|
||||
public GetReadingAvailabilityEndpoint(IQueryHandler<GetReadingAvailabilityQuery, Result<ReadingAvailabilityResponse>> getReadingAvailabilityQueryHandler)
|
||||
{
|
||||
_getReadingAvailabilityQueryHandler = getReadingAvailabilityQueryHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(Http.GET);
|
||||
Routes(ApiRoutes.Books.GET_BOOK_READING_AVAILABILITY);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks the reading availability of the book identified by <paramref name="request"/>.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the Id of the book whose reading availability is checked.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(GetReadingAvailabilityRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<ReadingAvailabilityResponse> result = await _getReadingAvailabilityQueryHandler.HandleAsync(request.ToQuery(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.Reading.GetReadingAvailability;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="GetReadingAvailabilityEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetReadingAvailabilityEndpointSummary : Summary<GetReadingAvailabilityEndpoint, GetReadingAvailabilityRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetReadingAvailabilityEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public GetReadingAvailabilityEndpointSummary()
|
||||
{
|
||||
Summary = "Checks the reading availability of a book.";
|
||||
Description = "Checks whether the book can be opened for reading, resolving the book reader configured for its media library and verifying that the reader is enabled, without extracting the book.";
|
||||
|
||||
ExampleRequest = new GetReadingAvailabilityRequest(
|
||||
BookId: Guid.NewGuid()
|
||||
);
|
||||
|
||||
RequestParam(r => r.BookId, "The Id of the book whose reading availability is checked. Required.");
|
||||
|
||||
ResponseParam<ReadingAvailabilityResponse>(r => r.BookId, "The Id of the book whose reading availability is reported.");
|
||||
ResponseParam<ReadingAvailabilityResponse>(r => r.LibraryId, "The Id of the media library the book belongs to.");
|
||||
ResponseParam<ReadingAvailabilityResponse>(r => r.IsAvailable, "Whether the book can be opened for reading.");
|
||||
ResponseParam<ReadingAvailabilityResponse>(r => r.ErrorCode, "The code of the error preventing the book from being read, when it cannot be read. Can be null when the book is available.");
|
||||
|
||||
Response(200, "The reading availability of the book is returned.",
|
||||
example: new ReadingAvailabilityResponse(
|
||||
BookId: Guid.NewGuid(),
|
||||
LibraryId: Guid.NewGuid(),
|
||||
IsAvailable: false,
|
||||
ErrorCode: "ReaderDisabled"
|
||||
)
|
||||
);
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "You are not authorized",
|
||||
instance = "/api/v1/books/{bookId}/reading/availability"
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an Admin, or the owner of the media library.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/books/{bookId}/reading/availability",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the book does not exist.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "BookNotFound",
|
||||
instance = "/api/v1/books/{bookId}/reading/availability",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/books/{bookId}/reading/availability",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"BookIdCannotBeEmpty"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading.Queries.GetReadingManifest;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.Library.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.Reading.GetReadingManifest;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/books/{bookId}/reading/manifest</c> route.
|
||||
/// </summary>
|
||||
public class GetReadingManifestEndpoint : BaseEndpoint<GetReadingManifestRequest, IResult>
|
||||
{
|
||||
private readonly IQueryHandler<GetReadingManifestQuery, Result<ReadingManifestResponse>> _getReadingManifestQueryHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetReadingManifestEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="getReadingManifestQueryHandler">Injected service for handling get reading manifest queries.</param>
|
||||
public GetReadingManifestEndpoint(IQueryHandler<GetReadingManifestQuery, Result<ReadingManifestResponse>> getReadingManifestQueryHandler)
|
||||
{
|
||||
_getReadingManifestQueryHandler = getReadingManifestQueryHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(Http.GET);
|
||||
Routes(ApiRoutes.Books.GET_BOOK_READING_MANIFEST);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the reading manifest of the book identified by <paramref name="request"/>.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the Id of the book whose reading manifest is retrieved.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(GetReadingManifestRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<ReadingManifestResponse> result = await _getReadingManifestQueryHandler.HandleAsync(request.ToQuery(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.Reading.GetReadingManifest;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="GetReadingManifestEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetReadingManifestEndpointSummary : Summary<GetReadingManifestEndpoint, GetReadingManifestRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetReadingManifestEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public GetReadingManifestEndpointSummary()
|
||||
{
|
||||
Summary = "Retrieves the reading manifest of a book.";
|
||||
Description = "Retrieves the reading manifest of a book, containing the metadata, the table of contents, the spine, and the resources needed to render the reader.";
|
||||
|
||||
ExampleRequest = new GetReadingManifestRequest(
|
||||
BookId: Guid.NewGuid()
|
||||
);
|
||||
|
||||
RequestParam(r => r.BookId, "The Id of the book whose reading manifest is retrieved. Required.");
|
||||
|
||||
ResponseParam<ReadingManifestResponse>(r => r.Title, "The title of the book.");
|
||||
ResponseParam<ReadingManifestResponse>(r => r.Author, "The author of the book, if known.");
|
||||
ResponseParam<ReadingManifestResponse>(r => r.CoverResourceKey, "The resource key of the cover image of the book, if applicable.");
|
||||
ResponseParam<ReadingManifestResponse>(r => r.Spine, "The ordered spine of the reading sections of the book.");
|
||||
ResponseParam<ReadingManifestResponse>(r => r.ResourceKeys, "The resource keys of the resources of the book.");
|
||||
ResponseParam<ReadingManifestResponse>(r => r.HasTextContent, "Whether the book has extractable text content. A scanned book, whose pages are only images, has no text content.");
|
||||
|
||||
Response(200, "The reading manifest of the book is returned.",
|
||||
example: new ReadingManifestResponse(
|
||||
Title: "The Fellowship of the Ring",
|
||||
Author: "J.R.R. Tolkien",
|
||||
CoverResourceKey: "cover-image",
|
||||
TableOfContents:
|
||||
[
|
||||
new ReadingTocEntryResponse(
|
||||
Label: "Chapter 1",
|
||||
LocationRef: "chapter-1",
|
||||
Children: []
|
||||
)
|
||||
],
|
||||
Spine:
|
||||
[
|
||||
new ReadingSpineItemResponse(
|
||||
LocationRef: "chapter-1",
|
||||
Title: "Chapter 1"
|
||||
)
|
||||
],
|
||||
ResourceKeys: ["cover-image"],
|
||||
HasTextContent: true
|
||||
)
|
||||
);
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "You are not authorized",
|
||||
instance = "/api/v1/books/{bookId}/reading/manifest"
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an Admin, or the owner of the media library.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/books/{bookId}/reading/manifest",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the book does not exist, no reader plugin supports its format, or the reader plugin is disabled.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "ReaderDisabled",
|
||||
instance = "/api/v1/books/{bookId}/reading/manifest",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/books/{bookId}/reading/manifest",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"BookIdCannotBeEmpty"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading.Queries.GetReadingResource;
|
||||
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.Library.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.Reading.GetReadingResource;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/books/{bookId}/reading/resources/{resourceKey}</c> route.
|
||||
/// </summary>
|
||||
public class GetReadingResourceEndpoint : BaseEndpoint<GetReadingResourceRequest, IResult>
|
||||
{
|
||||
private readonly IQueryHandler<GetReadingResourceQuery, Result<ReadingResourceDataDto>> _getReadingResourceQueryHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetReadingResourceEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="getReadingResourceQueryHandler">Injected service for handling get reading resource queries.</param>
|
||||
public GetReadingResourceEndpoint(IQueryHandler<GetReadingResourceQuery, Result<ReadingResourceDataDto>> getReadingResourceQueryHandler)
|
||||
{
|
||||
_getReadingResourceQueryHandler = getReadingResourceQueryHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(Http.GET);
|
||||
Routes(ApiRoutes.Books.GET_BOOK_READING_RESOURCE);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the resource of the book identified by <paramref name="request"/>.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the Id of the book and the resource key of the resource.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(GetReadingResourceRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<ReadingResourceDataDto> result = await _getReadingResourceQueryHandler.HandleAsync(request.ToQuery(), cancellationToken).ConfigureAwait(false);
|
||||
if (result.IsFailure)
|
||||
return Problem(result.Errors);
|
||||
// The media type of a resource is declared by the book itself, so it is not trusted: a resource whose declared media type could be
|
||||
// rendered as an active document (for example an SVG) is served as an opaque binary download instead, and every resource is served
|
||||
// with content sniffing disabled, so that the browser never executes book content as anything but what this endpoint declares.
|
||||
HttpContext.Response.Headers["X-Content-Type-Options"] = "nosniff";
|
||||
return TypedResults.Bytes(result.Value.Data, GetSafeContentType(result.Value.MimeType));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the safe content type of a book resource, replacing the media types that could be rendered as active documents with an opaque binary type.
|
||||
/// </summary>
|
||||
/// <param name="mediaType">The media type declared by the book.</param>
|
||||
/// <returns>The safe content type the resource is served with.</returns>
|
||||
private static string GetSafeContentType(string mediaType)
|
||||
{
|
||||
// Images, audio, video, fonts, and stylesheets are inert content the reader can render; everything else (an SVG, an XML or HTML
|
||||
// document, a script) is served as an opaque download, so that opening a resource URL never executes active content.
|
||||
if (mediaType.StartsWith("image/", StringComparison.OrdinalIgnoreCase) && !string.Equals(mediaType, "image/svg+xml", StringComparison.OrdinalIgnoreCase))
|
||||
return mediaType;
|
||||
if (mediaType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase) || mediaType.StartsWith("video/", StringComparison.OrdinalIgnoreCase))
|
||||
return mediaType;
|
||||
if (mediaType.StartsWith("font/", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(mediaType, "application/font-woff", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(mediaType, "application/font-woff2", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(mediaType, "application/vnd.ms-opentype", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(mediaType, "application/x-font-ttf", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(mediaType, "application/x-font-opentype", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(mediaType, "text/css", StringComparison.OrdinalIgnoreCase))
|
||||
return mediaType;
|
||||
return "application/octet-stream";
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.Reading.GetReadingResource;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="GetReadingResourceEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetReadingResourceEndpointSummary : Summary<GetReadingResourceEndpoint, GetReadingResourceRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetReadingResourceEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public GetReadingResourceEndpointSummary()
|
||||
{
|
||||
Summary = "Retrieves a resource of a book, for reading.";
|
||||
Description = "Retrieves the binary content of a resource of a book, such as an image or a font referenced by a reading section.";
|
||||
|
||||
ExampleRequest = new GetReadingResourceRequest(
|
||||
BookId: Guid.NewGuid(),
|
||||
ResourceKey: "cover-image"
|
||||
);
|
||||
|
||||
RequestParam(r => r.BookId, "The Id of the book whose resource is retrieved. Required.");
|
||||
RequestParam(r => r.ResourceKey, "The opaque resource key of the resource. Required.");
|
||||
|
||||
Response(200, "The resource of the book is returned.");
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "You are not authorized",
|
||||
instance = "/api/v1/books/{bookId}/reading/resources/{resourceKey}"
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an Admin, or the owner of the media library.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/books/{bookId}/reading/resources/{resourceKey}",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the book does not exist, no reader plugin supports its format, the reader plugin is disabled, or the resource was not found.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "ResourceNotFound",
|
||||
instance = "/api/v1/books/{bookId}/reading/resources/{resourceKey}",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/books/{bookId}/reading/resources/{resourceKey}",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"BookIdCannotBeEmpty",
|
||||
"ResourceKeyCannotBeEmpty"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading.Queries.GetReadingSection;
|
||||
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.Library.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.Reading.GetReadingSection;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/books/{bookId}/reading/sections/{locationRef}</c> route.
|
||||
/// </summary>
|
||||
public class GetReadingSectionEndpoint : BaseEndpoint<GetReadingSectionRequest, IResult>
|
||||
{
|
||||
private readonly IQueryHandler<GetReadingSectionQuery, Result<ReadingSectionDto>> _getReadingSectionQueryHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetReadingSectionEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="getReadingSectionQueryHandler">Injected service for handling get reading section queries.</param>
|
||||
public GetReadingSectionEndpoint(IQueryHandler<GetReadingSectionQuery, Result<ReadingSectionDto>> getReadingSectionQueryHandler)
|
||||
{
|
||||
_getReadingSectionQueryHandler = getReadingSectionQueryHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(Http.GET);
|
||||
Routes(ApiRoutes.Books.GET_BOOK_READING_SECTION);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the content of the reading section of the book identified by <paramref name="request"/>.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the Id of the book and the location reference of the reading section.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(GetReadingSectionRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<ReadingSectionDto> result = await _getReadingSectionQueryHandler.HandleAsync(request.ToQuery(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.Reading.GetReadingSection;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="GetReadingSectionEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetReadingSectionEndpointSummary : Summary<GetReadingSectionEndpoint, GetReadingSectionRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetReadingSectionEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public GetReadingSectionEndpointSummary()
|
||||
{
|
||||
Summary = "Retrieves the content of a reading section of a book.";
|
||||
Description = "Retrieves the sanitized HTML content of the reading section of a book, ready to be rendered by the client.";
|
||||
|
||||
ExampleRequest = new GetReadingSectionRequest(
|
||||
BookId: Guid.NewGuid(),
|
||||
LocationRef: "chapter-1"
|
||||
);
|
||||
|
||||
RequestParam(r => r.BookId, "The Id of the book whose reading section is retrieved. Required.");
|
||||
RequestParam(r => r.LocationRef, "The opaque location reference of the reading section. Required.");
|
||||
|
||||
ResponseParam<ReadingSectionDto>(r => r.LocationRef, "The opaque location reference of the reading section.");
|
||||
ResponseParam<ReadingSectionDto>(r => r.Title, "The title of the reading section, if known.");
|
||||
ResponseParam<ReadingSectionDto>(r => r.ContentHtml, "The sanitized HTML content of the reading section.");
|
||||
|
||||
Response(200, "The content of the reading section is returned.",
|
||||
example: new ReadingSectionDto(
|
||||
LocationRef: "chapter-1",
|
||||
Title: "Chapter 1",
|
||||
ContentHtml: "<h1>Chapter 1</h1><p>A long journey begins...</p>"
|
||||
)
|
||||
);
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "You are not authorized",
|
||||
instance = "/api/v1/books/{bookId}/reading/sections/{locationRef}"
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an Admin, or the owner of the media library.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/books/{bookId}/reading/sections/{locationRef}",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the book does not exist, no reader plugin supports its format, the reader plugin is disabled, or the reading section was not found.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "SectionNotFound",
|
||||
instance = "/api/v1/books/{bookId}/reading/sections/{locationRef}",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/books/{bookId}/reading/sections/{locationRef}",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"BookIdCannotBeEmpty",
|
||||
"LocationRefCannotBeEmpty"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user