Skip to content

Commit

Permalink
Merge pull request #42 from jellyfin/fix-build
Browse files Browse the repository at this point in the history
  • Loading branch information
crobibero authored Nov 19, 2021
2 parents b7d14d5 + f5f5aa7 commit 20c78b2
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 70 deletions.
4 changes: 1 addition & 3 deletions Jellyfin.Plugin.Bookshelf/Providers/BookProviderFromOpf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public class BookProviderFromOpf : ILocalMetadataProvider<Book>, IHasItemChangeM
private const string StandardOpfFile = "content.opf";
private const string CalibreOpfFile = "metadata.opf";

private const string DcNamespace = @"http://purl.org/dc/elements/1.1/";
private const string OpfNamespace = @"http://www.idpf.org/2007/opf";
private readonly IFileSystem _fileSystem;

private readonly ILogger<BookProviderFromOpf> _logger;
Expand Down Expand Up @@ -94,7 +92,7 @@ private void ReadOpfData(MetadataResult<Book> bookResult, string metaFile, Cance
var doc = new XmlDocument();
doc.Load(metaFile);

OpfReader.ReadOpfData(bookResult, doc, cancellationToken, _logger);
OpfReader.ReadOpfData(bookResult, doc, _logger, cancellationToken);
}
}
}
13 changes: 8 additions & 5 deletions Jellyfin.Plugin.Bookshelf/Providers/ComicBookImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ public ComicBookImageProvider(ILogger<ComicBookImageProvider> logger)
public string Name => "Comic Book Zip Archive Cover Extractor";

/// <inheritdoc />
public async Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
{
// Check if the file is a .cbz file
var extension = Path.GetExtension(item.Path);
if (string.Equals(extension, CbzFileExtension, StringComparison.OrdinalIgnoreCase))
{
return await LoadCover(item);
return LoadCover(item);
}
else
{
return new DynamicImageResponse { HasImage = false };
return Task.FromResult(new DynamicImageResponse { HasImage = false });
}
}

Expand Down Expand Up @@ -82,7 +82,7 @@ private async Task<DynamicImageResponse> LoadCover(BaseItem item)
var (cover, imageFormat) = FindCoverEntryInZip(archive) ?? throw new InvalidOperationException("No supported cover found");

// Copy our cover to memory stream
await cover.Open().CopyToAsync(memoryStream);
await cover.Open().CopyToAsync(memoryStream).ConfigureAwait(false);

// Reset stream position after copying
memoryStream.Position = 0;
Expand Down Expand Up @@ -117,7 +117,10 @@ private async Task<DynamicImageResponse> LoadCover(BaseItem item)
// There are comics with a cover file, but others with varying names for the cover
// e.g. attackontitan_vol1_Page_001 with no indication that this is the cover except
// that it is the first jpeg entry (and page)
var cover = archive.GetEntry("cover" + extension) ?? archive.Entries.OrderBy(x => x.Name).FirstOrDefault(x => x.Name.EndsWith(extension));
var cover = archive.GetEntry("cover" + extension)
?? archive.Entries
.OrderBy(x => x.Name)
.FirstOrDefault(x => x.Name.EndsWith(extension, StringComparison.OrdinalIgnoreCase));

// If we have found something, return immediately
if (cover is not null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Jellyfin.Plugin.Bookshelf.Providers.ComicBookInfo
Expand Down Expand Up @@ -90,13 +91,13 @@ public class ComicBookInfoMetadata
/// Gets or sets the list of credits.
/// </summary>
[JsonPropertyName("credits")]
public ComicBookInfoCredit[] Credits { get; set; } = Array.Empty<ComicBookInfoCredit>();
public IReadOnlyList<ComicBookInfoCredit> Credits { get; set; } = Array.Empty<ComicBookInfoCredit>();

/// <summary>
/// Gets or sets the list of tags.
/// </summary>
[JsonPropertyName("tags")]
public string[] Tags { get; set; } = Array.Empty<string>();
public IReadOnlyList<string> Tags { get; set; } = Array.Empty<string>();

/// <summary>
/// Gets or sets the comments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public async ValueTask<MetadataResult<Book>> ReadMetadata(ItemInfo info, IDirect

try
{
#pragma warning disable CA2007
await using Stream stream = File.OpenRead(path);
#pragma warning restore CA2007

// not yet async: https://github.com/adamhathcock/sharpcompress/pull/565
using var archive = ZipArchive.Open(stream);
Expand All @@ -58,8 +60,12 @@ public async ValueTask<MetadataResult<Book>> ReadMetadata(ItemInfo info, IDirect
var volume = archive.Volumes.First();
if (volume.Comment is not null)
{
#pragma warning disable CA2007
await using var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(volume.Comment));
var comicBookMetadata = await JsonSerializer.DeserializeAsync<ComicBookInfoFormat>(jsonStream, JsonDefaults.Options, cancellationToken);
#pragma warning restore CA2007

var comicBookMetadata = await JsonSerializer.DeserializeAsync<ComicBookInfoFormat>(jsonStream, JsonDefaults.Options, cancellationToken)
.ConfigureAwait(false);

if (comicBookMetadata is null)
{
Expand Down Expand Up @@ -120,7 +126,7 @@ private MetadataResult<Book> SaveMetadata(ComicBookInfoFormat comic)
metadataResult.ResultLanguage = ReadCultureInfoAsThreeLetterIsoInto(comic.Metadata.Language);
}

if (comic.Metadata.Credits.Length > 0)
if (comic.Metadata.Credits.Count > 0)
{
foreach (var person in comic.Metadata.Credits)
{
Expand Down Expand Up @@ -151,25 +157,25 @@ private MetadataResult<Book> SaveMetadata(ComicBookInfoFormat comic)
if (comic.PublicationYear is not null)
{
book.ProductionYear = comic.PublicationYear;
hasFoundMetadata |= true;
hasFoundMetadata = true;
}

if (comic.Issue is not null)
{
book.IndexNumber = comic.Issue;
hasFoundMetadata |= true;
hasFoundMetadata = true;
}

if (comic.Tags is not null && comic.Tags.Length > 0)
if (comic.Tags.Count > 0)
{
book.Tags = comic.Tags;
hasFoundMetadata |= true;
book.Tags = comic.Tags.ToArray();
hasFoundMetadata = true;
}

if (comic.PublicationYear is not null && comic.PublicationMonth is not null)
{
book.PremiereDate = ReadTwoPartDateInto(comic.PublicationYear.Value, comic.PublicationMonth.Value);
hasFoundMetadata |= true;
hasFoundMetadata = true;
}

if (hasFoundMetadata)
Expand Down
6 changes: 4 additions & 2 deletions Jellyfin.Plugin.Bookshelf/Providers/ComicFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public async Task<MetadataResult<Book>> GetMetadata(ItemInfo info, IDirectorySer
{
foreach (IComicFileProvider iComicFileProvider in _comicFileProviders)
{
var metadata = await iComicFileProvider.ReadMetadata(info, directoryService, cancellationToken);
var metadata = await iComicFileProvider.ReadMetadata(info, directoryService, cancellationToken)
.ConfigureAwait(false);

if (metadata.HasMetadata)
{
Expand All @@ -41,7 +42,8 @@ public async Task<MetadataResult<Book>> GetMetadata(ItemInfo info, IDirectorySer
return new MetadataResult<Book> { HasMetadata = false };
}

bool IHasItemChangeMonitor.HasChanged(BaseItem item, IDirectoryService directoryService)
/// <inheritdoc />
public bool HasChanged(BaseItem item, IDirectoryService directoryService)
{
foreach (IComicFileProvider iComicFileProvider in _comicFileProviders)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ExternalComicInfoProvider(IFileSystem fileSystem, ILogger<ExternalComicIn
/// <inheritdoc />
public async ValueTask<MetadataResult<Book>> ReadMetadata(ItemInfo info, IDirectoryService directoryService, CancellationToken cancellationToken)
{
var comicInfoXml = await LoadXml(info, directoryService, cancellationToken);
var comicInfoXml = await LoadXml(info, directoryService, cancellationToken).ConfigureAwait(false);

if (comicInfoXml is null)
{
Expand Down Expand Up @@ -88,7 +88,7 @@ public bool HasItemChanged(BaseItem item)
var comicInfoXml = XDocument.LoadAsync(reader, LoadOptions.None, cancellationToken);

// Read data from XML
return await comicInfoXml;
return await comicInfoXml.ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public InternalComicInfoProvider(IFileSystem fileSystem, ILogger<InternalComicIn
/// <inheritdoc />
public async ValueTask<MetadataResult<Book>> ReadMetadata(ItemInfo info, IDirectoryService directoryService, CancellationToken cancellationToken)
{
var comicInfoXml = await LoadXml(info, directoryService, cancellationToken);
var comicInfoXml = await LoadXml(info, directoryService, cancellationToken).ConfigureAwait(false);

if (comicInfoXml is null)
{
Expand Down Expand Up @@ -96,11 +96,14 @@ public bool HasItemChanged(BaseItem item)
}

// Open the xml
#pragma warning disable CA2007
await using var containerStream = container.Open();
#pragma warning restore CA2007

var comicInfoXml = XDocument.LoadAsync(containerStream, LoadOptions.None, cancellationToken);

// Read data from XML
return await comicInfoXml;
return await comicInfoXml.ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,27 @@ private bool IsValidImage(string? mimeType)
return null;
}

private Task<DynamicImageResponse> LoadCover(ZipArchive epub, XmlDocument opf, string opfRootDirectory)
private async Task<DynamicImageResponse> LoadCover(ZipArchive epub, XmlDocument opf, string opfRootDirectory)
{
var coverRef = ReadCoverPath(opf, opfRootDirectory);
if (coverRef == null)
{
return Task.FromResult(new DynamicImageResponse { HasImage = false });
return new DynamicImageResponse { HasImage = false };
}

var cover = coverRef.Value;

var coverFile = epub.GetEntry(cover.Path);
if (coverFile == null)
{
return Task.FromResult(new DynamicImageResponse { HasImage = false });
return new DynamicImageResponse { HasImage = false };
}

var memoryStream = new MemoryStream();
using (var coverStream = coverFile.Open())
{
coverStream.CopyTo(memoryStream);
await coverStream.CopyToAsync(memoryStream)
.ConfigureAwait(false);
}

memoryStream.Position = 0;
Expand All @@ -160,7 +161,7 @@ private Task<DynamicImageResponse> LoadCover(ZipArchive epub, XmlDocument opf, s
};
response.SetFormatFromMimeType(cover.MimeType);

return Task.FromResult(response);
return response;
}

private Task<DynamicImageResponse> GetFromZip(BaseItem item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void ReadEpubAsZip(MetadataResult<Book> result, string path, Cancellatio
var opfDocument = new XmlDocument();
opfDocument.Load(opfStream);

OpfReader.ReadOpfData(result, opfDocument, cancellationToken, _logger);
OpfReader.ReadOpfData(result, opfDocument, _logger, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
Expand Down Expand Up @@ -57,7 +58,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
return list;
}

var bookResult = await FetchBookData(googleBookId, cancellationToken);
var bookResult = await FetchBookData(googleBookId, cancellationToken).ConfigureAwait(false);

if (bookResult == null)
{
Expand All @@ -77,12 +78,16 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
{
cancellationToken.ThrowIfCancellationRequested();

var url = string.Format(GoogleApiUrls.DetailsUrl, googleBookId);
var url = string.Format(CultureInfo.InvariantCulture, GoogleApiUrls.DetailsUrl, googleBookId);

var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);

using var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);

#pragma warning disable CA2007
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
#pragma warning restore CA2007

return await JsonSerializer.DeserializeAsync<BookResult>(stream, JsonDefaults.Options, cancellationToken).ConfigureAwait(false);
}

Expand Down
Loading

0 comments on commit 20c78b2

Please sign in to comment.