Adds book details and editing with cover uploads across the stack

This commit is contained in:
BinaryAlley committed 2026-09-06 09:37:23 -07:00
1 parent c2715dea2e
commit 1bfa07ba5d
103 files changed
+8782 -387

No files matched your search

@@ -39,7 +39,7 @@ public class ApiHttpClient : IApiHttpClient
{
_httpClient = httpClient;
_httpContext = httpContextAccessor.HttpContext;
// read the API server configuration values from the configuration, and assign them to the injected client
// Read the API server configuration values from the configuration, and assign them to the injected client.
ServerConfigurationDto serverConfiguration = serverConfigurationOptions.Value;
httpClient.BaseAddress = new Uri($"{serverConfiguration.BaseAddress}:{serverConfiguration.Port}/api/v{serverConfiguration.ApiVersion}/");
@@ -82,11 +82,11 @@ public class ApiHttpClient : IApiHttpClient
AuthenticationHeaderValue? authenticationHeader = GetAuthenticationHeader();
if (authenticationHeader is not null)
request.Headers.Authorization = authenticationHeader;
// send the request and expect only headers initially - this prevents the client from buffering the entire response
// Send the request and expect only headers initially - this prevents the client from buffering the entire response.
using HttpResponseMessage response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
await using Stream stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
// Deserialize the JSON content asynchronously as an enumerable of TResponse items
// Deserialize the JSON content asynchronously as an enumerable of TResponse items.
await foreach (TResponse? item in JsonSerializer.DeserializeAsyncEnumerable<TResponse>(stream, _jsonOptions, cancellationToken).ConfigureAwait(false))
{
if (cancellationToken.IsCancellationRequested)
@@ -110,17 +110,17 @@ public class ApiHttpClient : IApiHttpClient
using HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
// read the content as a string, for error messages
// Read the content as a string, for error messages.
string content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
ProblemDetailsDto? problemDetails = null;
try
{
problemDetails = JsonSerializer.Deserialize<ProblemDetailsDto>(content, _jsonOptions);
}
catch { /* if we can't deserialize to ProblemDetails, we'll just use the status code */ }
catch { /* If we can't deserialize to ProblemDetails, we'll just use the status code. */ }
throw new ApiException(problemDetails, response.StatusCode);
}
// if the response is successful, read it as a byte array
// If the response is successful, read it as a byte array.
byte[] responseContent = await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
string contentType = response.Content.Headers.ContentType?.ToString() ?? "application/octet-stream";
return new BlobDataDto { Data = responseContent, ContentType = contentType };
@@ -137,19 +137,18 @@ public class ApiHttpClient : IApiHttpClient
AuthenticationHeaderValue? authenticationHeader = GetAuthenticationHeader();
if (authenticationHeader is not null)
request.Headers.Authorization = authenticationHeader;
//return await SendRequestAsync<TResponse>(request, cancellationToken).ConfigureAwait(false);
// send the HTTP request and read the response
// Send the HTTP request and read the response.
HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
ProblemDetailsDto? problemDetails = null;
string content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
// attempt to deserialize the response content to ProblemDetailsDto if the request fails
// Attempt to deserialize the response content to ProblemDetailsDto if the request fails.
try
{
problemDetails = JsonSerializer.Deserialize<ProblemDetailsDto>(content, _jsonOptions);
}
catch { /* if we can't deserialize to ProblemDetails, we'll just use the status code */ }
catch { /* If we can't deserialize to ProblemDetails, we'll just use the status code. */ }
throw new ApiException(problemDetails, response.StatusCode, request.RequestUri?.AbsolutePath);
}
}
@@ -204,7 +203,7 @@ public class ApiHttpClient : IApiHttpClient
/// <returns>The deserialized response containing the result of the POST request.</returns>
public async Task<TResponse> PostMultipartAsync<TResponse>(string endpoint, Stream fileStream, string fileName, string fieldName, CancellationToken cancellationToken = default)
{
// the file is streamed as a generic binary part, since the exact content type of the uploaded file is not known
// The file is streamed as a generic binary part, since the exact content type of the uploaded file is not known.
using MultipartFormDataContent form = [];
using StreamContent fileContent = new(fileStream);
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
@@ -220,6 +219,34 @@ public class ApiHttpClient : IApiHttpClient
return await SendRequestAsync<TResponse>(request, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Sends a PUT request with a multipart form containing a single file to the specified <paramref name="endpoint"/> as an asynchronous operation and returns the result.
/// </summary>
/// <typeparam name="TResponse">The expected type of the response content.</typeparam>
/// <param name="endpoint">The API endpoint where the request is being sent.</param>
/// <param name="fileStream">The stream of the file to upload.</param>
/// <param name="fileName">The name of the file to upload.</param>
/// <param name="fieldName">The name of the form field carrying the file.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>The deserialized response containing the result of the PUT request.</returns>
public async Task<TResponse> PutMultipartAsync<TResponse>(string endpoint, Stream fileStream, string fileName, string fieldName, CancellationToken cancellationToken = default)
{
// The file is streamed as a generic binary part, since the exact content type of the uploaded file is not known.
using MultipartFormDataContent form = [];
using StreamContent fileContent = new(fileStream);
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
form.Add(fileContent, fieldName, fileName);
using HttpRequestMessage request = new(HttpMethod.Put, endpoint)
{
Content = form
};
AuthenticationHeaderValue? authenticationHeader = GetAuthenticationHeader();
if (authenticationHeader is not null)
request.Headers.Authorization = authenticationHeader;
return await SendRequestAsync<TResponse>(request, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Sends an HTTP request and deserializes the response into the specified type if the request is successful.
/// If the request fails, it attempts to deserialize the response content into a <see cref="ProblemDetailsDto"/> and throws an <see cref="ApiException"/>.
@@ -231,13 +258,13 @@ public class ApiHttpClient : IApiHttpClient
/// <exception cref="ApiException">Thrown if the response is not successful, with the <see cref="ProblemDetailsDto"/> if available.</exception>
private async Task<TResponse> SendRequestAsync<TResponse>(HttpRequestMessage request, CancellationToken cancellationToken)
{
// send the HTTP request and read the response
// Send the HTTP request and read the response.
HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
string content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
// if the response is successful, deserialize the content to TResponse and return it
// If the response is successful, deserialize the content to TResponse and return it.
if (response.IsSuccessStatusCode)
{
// handle empty content case
// Handle empty content case.
if (string.IsNullOrEmpty(content))
return default!;
return JsonSerializer.Deserialize<TResponse>(content, _jsonOptions)!;
@@ -245,12 +272,12 @@ public class ApiHttpClient : IApiHttpClient
else
{
ProblemDetailsDto? problemDetails = null;
// attempt to deserialize the response content to ProblemDetailsDto if the request fails
// Attempt to deserialize the response content to ProblemDetailsDto if the request fails.
try
{
problemDetails = JsonSerializer.Deserialize<ProblemDetailsDto>(content, _jsonOptions);
}
catch { /* if we can't deserialize to ProblemDetails, we'll just use the status code */ }
catch { /* If we can't deserialize to ProblemDetails, we'll just use the status code. */ }
throw new ApiException(problemDetails, response.StatusCode, request.RequestUri?.AbsolutePath);
}
}
@@ -57,6 +57,18 @@ public interface IApiHttpClient
/// <returns>The deserialized response containing the result of the PUT request.</returns>
Task<TResponse> PutAsync<TResponse, TModel>(string endpoint, TModel data, CancellationToken cancellationToken = default);
/// <summary>
/// Sends a PUT request with a multipart form containing a single file to the specified <paramref name="endpoint"/> as an asynchronous operation and returns the result.
/// </summary>
/// <typeparam name="TResponse">The expected type of the response content.</typeparam>
/// <param name="endpoint">The API endpoint where the request is being sent.</param>
/// <param name="fileStream">The stream of the file to upload.</param>
/// <param name="fileName">The name of the file to upload.</param>
/// <param name="fieldName">The name of the form field carrying the file.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>The deserialized response containing the result of the PUT request.</returns>
Task<TResponse> PutMultipartAsync<TResponse>(string endpoint, Stream fileStream, string fileName, string fieldName, CancellationToken cancellationToken = default);
/// <summary>
/// Sends a POST request with a multipart form containing a single file to the specified <paramref name="endpoint"/> as an asynchronous operation and returns the result.
/// </summary>
@@ -0,0 +1,143 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.DTO.MediaContributors;
using Lumina.Presentation.Web.Common.DTO.WrittenContentLibrary;
using Lumina.Presentation.Web.Common.DTO.WrittenContentLibrary.BookLibrary;
using Lumina.Presentation.Web.Common.Enums.BookLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.DTO.WrittenContentLibrary.BookLibrary;
/// <summary>
/// Data transfer object carrying the full details of a book, as stored in the media library.
/// </summary>
[DebuggerDisplay("Title: {Metadata?.Title}")]
public class BookDetailsDto
{
/// <summary>
/// Gets or sets the Id of the book.
/// </summary>
public Guid Id { get; set; }
/// <summary>
/// Gets or sets the Id of the media library this book belongs to.
/// </summary>
public Guid LibraryId { get; set; }
/// <summary>
/// Gets or sets the file system path of the book.
/// </summary>
public string Path { get; set; } = null!;
/// <summary>
/// Gets or sets the written content metadata of the book.
/// </summary>
public WrittenContentMetadataDto? Metadata { get; set; }
/// <summary>
/// Gets or sets the format of the book (e.g., Hardcover, Paperback), if applicable.
/// </summary>
public BookFormat? Format { get; set; }
/// <summary>
/// Gets or sets the edition of the book, if applicable.
/// </summary>
public string? Edition { get; set; }
/// <summary>
/// Gets or sets the volume or book number in the series, if applicable.
/// </summary>
public float? VolumeNumber { get; set; }
/// <summary>
/// Gets or sets the ASIN (Amazon Standard Identification Number) of the book, if applicable.
/// </summary>
public string? ASIN { get; set; }
/// <summary>
/// Gets or sets the Goodreads Id of the book, if applicable.
/// </summary>
public string? GoodreadsId { get; set; }
/// <summary>
/// Gets or sets the Library of Congress Control Number (LCCN) of the book, if applicable.
/// </summary>
public string? LCCN { get; set; }
/// <summary>
/// Gets or sets the OCLC Number (WorldCat identifier) of the book, if applicable.
/// </summary>
public string? OCLCNumber { get; set; }
/// <summary>
/// Gets or sets the Open Library Id of the book, if applicable.
/// </summary>
public string? OpenLibraryId { get; set; }
/// <summary>
/// Gets or sets the LibraryThing Id of the book, if applicable.
/// </summary>
public string? LibraryThingId { get; set; }
/// <summary>
/// Gets or sets the Google Books Id of the book, if applicable.
/// </summary>
public string? GoogleBooksId { get; set; }
/// <summary>
/// Gets or sets the Barnes &amp; Noble Id of the book, if applicable.
/// </summary>
public string? BarnesAndNobleId { get; set; }
/// <summary>
/// Gets or sets the Apple Books Id of the book, if applicable.
/// </summary>
public string? AppleBooksId { get; set; }
/// <summary>
/// Gets or sets the list of ISBN (International Standard Book Number) of the book.
/// </summary>
public List<IsbnDto>? ISBNs { get; set; }
/// <summary>
/// Gets or sets the list of media contributors of the book.
/// </summary>
public List<MediaContributorDto>? Contributors { get; set; }
/// <summary>
/// Gets or sets the list of ratings of the book.
/// </summary>
public List<BookRatingDto>? Ratings { get; set; }
/// <summary>
/// Gets or sets the status of the metadata enrichment of the book.
/// </summary>
public string? MetadataStatus { get; set; }
/// <summary>
/// Gets or sets the date and time when the metadata of the book was last enriched, if applicable.
/// </summary>
public DateTime? LastMetadataUpdateUtc { get; set; }
/// <summary>
/// Gets or sets the name of the plugin that enriched the metadata of the book, if applicable.
/// </summary>
public string? MetadataProvider { get; set; }
/// <summary>
/// Gets or sets the date and time when the book was created.
/// </summary>
public DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the optional date and time when the book was updated.
/// </summary>
public DateTime? UpdatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the path of the cover image of the book, if available.
/// </summary>
public string? CoverPath { get; set; }
}
@@ -1,5 +1,4 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Diagnostics;
#endregion
@@ -8,8 +7,8 @@ namespace Lumina.Presentation.Web.Common.Requests.Library.WrittenContentLibrary.
/// <summary>
/// Represents a request to get a book by the specified Id.
/// </summary>
/// <param name="Id">The unique identifier of the book to retrieve. Required.</param>
/// <param name="Id">The unique identifier of the book to retrieve, taken from the route. Required.</param>
[DebuggerDisplay("Id: {Id}")]
public record GetBookRequest(
Guid Id
string Id
);
@@ -0,0 +1,107 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.DTO.MediaContributors;
using Lumina.Presentation.Web.Common.DTO.WrittenContentLibrary;
using Lumina.Presentation.Web.Common.DTO.WrittenContentLibrary.BookLibrary;
using Lumina.Presentation.Web.Common.Enums.BookLibrary;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.Requests.Library.WrittenContentLibrary.BookLibrary.Books;
/// <summary>
/// Represents a request to update an existing book.
/// </summary>
[DebuggerDisplay("Title: {Metadata?.Title}")]
public class UpdateBookRequest
{
/// <summary>
/// Gets or sets the Id of the book to update, taken from the route.
/// </summary>
public string? Id { get; set; }
/// <summary>
/// Gets or sets the written content metadata of the book.
/// </summary>
public WrittenContentMetadataDto? Metadata { get; set; }
/// <summary>
/// Gets or sets the format of the book (e.g., Hardcover, Paperback), if applicable.
/// </summary>
public BookFormat? Format { get; set; }
/// <summary>
/// Gets or sets the edition of the book, if applicable.
/// </summary>
public string? Edition { get; set; }
/// <summary>
/// Gets or sets the volume or book number in the series, if applicable.
/// </summary>
public float? VolumeNumber { get; set; }
/// <summary>
/// Gets or sets the series name, if the book is part of a series.
/// </summary>
public BookSeriesDto? Series { get; set; }
/// <summary>
/// Gets or sets the ASIN (Amazon Standard Identification Number) of the book, if applicable.
/// </summary>
public string? ASIN { get; set; }
/// <summary>
/// Gets or sets the Goodreads Id of the book, if applicable.
/// </summary>
public string? GoodreadsId { get; set; }
/// <summary>
/// Gets or sets the Library of Congress Control Number (LCCN) of the book, if applicable.
/// </summary>
public string? LCCN { get; set; }
/// <summary>
/// Gets or sets the OCLC Number (WorldCat identifier) of the book, if applicable.
/// </summary>
public string? OCLCNumber { get; set; }
/// <summary>
/// Gets or sets the Open Library Id of the book, if applicable.
/// </summary>
public string? OpenLibraryId { get; set; }
/// <summary>
/// Gets or sets the LibraryThing Id of the book, if applicable.
/// </summary>
public string? LibraryThingId { get; set; }
/// <summary>
/// Gets or sets the Google Books Id of the book, if applicable.
/// </summary>
public string? GoogleBooksId { get; set; }
/// <summary>
/// Gets or sets the Barnes &amp; Noble Id of the book, if applicable.
/// </summary>
public string? BarnesAndNobleId { get; set; }
/// <summary>
/// Gets or sets the Apple Books Id of the book, if applicable.
/// </summary>
public string? AppleBooksId { get; set; }
/// <summary>
/// Gets or sets the list of ISBN (International Standard Book Number) of the book.
/// </summary>
public List<IsbnDto>? ISBNs { get; set; }
/// <summary>
/// Gets or sets the list of media contributors of the book.
/// </summary>
public List<MediaContributorDto>? Contributors { get; set; }
/// <summary>
/// Gets or sets the list of ratings of the book.
/// </summary>
public List<BookRatingDto>? Ratings { get; set; }
}
@@ -11,6 +11,9 @@ public static partial class ApiRoutes
public static class Books
{
public const string GET_BOOKS_LITE = "books/lite";
public const string GET_BOOK = "books/{id}";
public const string UPDATE_BOOK = "books/{id}";
public const string UPDATE_BOOK_COVER = "books/{id}/cover";
public const string GET_BOOK_READING_MANIFEST = "books/{bookId}/reading/manifest";
public const string GET_BOOK_READING_AVAILABILITY = "books/{bookId}/reading/availability";
public const string GET_BOOK_READING_SECTION = "books/{bookId}/reading/sections/{locationRef}";
@@ -13,6 +13,9 @@ public static partial class WebRoutes
public const string INDEX = "{culture}/library/written-content-library/books-library/books";
public const string GET_LIBRARY_ITEMS = "{culture}/library/written-content-library/books-library/books/api-get-library-items";
public const string EDIT_BOOK = "{culture}/library/written-content-library/books-library/books/{id}";
public const string GET_BOOK_DETAILS = "{culture}/library/written-content-library/books-library/books/{id}/api-get-book";
public const string SAVE_BOOK = "{culture}/library/written-content-library/books-library/books/{id}/api-save-book";
public const string UPDATE_BOOK_COVER = "{culture}/library/written-content-library/books-library/books/{id}/api-update-cover";
public const string READ = "{culture}/library/written-content-library/books-library/books/{bookId}/read";
public const string GET_READING_MANIFEST = "{culture}/library/written-content-library/books-library/books/{bookId}/api-get-reading-manifest";
public const string GET_READING_AVAILABILITY = "{culture}/library/written-content-library/books-library/books/{bookId}/api-get-reading-availability";