| | | 1 | | using System; |
| | | 2 | | using System.IO.Compression; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using System.Xml.Linq; |
| | | 6 | | using MediaBrowser.Controller.Entities; |
| | | 7 | | using MediaBrowser.Controller.Providers; |
| | | 8 | | using MediaBrowser.Model.IO; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | |
| | | 11 | | namespace MediaBrowser.Providers.Books.ComicInfo; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Handles metadata for comics which is saved as an XML document inside the comic itself. |
| | | 15 | | /// </summary> |
| | | 16 | | public class InternalComicInfoProvider : IComicProvider |
| | | 17 | | { |
| | | 18 | | private readonly IFileSystem _fileSystem; |
| | | 19 | | private readonly ILogger<InternalComicInfoProvider> _logger; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of the <see cref="InternalComicInfoProvider"/> class. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | | 25 | | /// <param name="logger">Instance of the <see cref="ILogger{InternalComicInfoProvider}"/> interface.</param> |
| | | 26 | | public InternalComicInfoProvider(IFileSystem fileSystem, ILogger<InternalComicInfoProvider> logger) |
| | | 27 | | { |
| | 0 | 28 | | _logger = logger; |
| | 0 | 29 | | _fileSystem = fileSystem; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public async ValueTask<MetadataResult<Book>> ReadMetadata(ItemInfo info, IDirectoryService directoryService, Cancell |
| | | 34 | | { |
| | 0 | 35 | | var comicInfoXml = await LoadXml(info, cancellationToken).ConfigureAwait(false); |
| | | 36 | | |
| | 0 | 37 | | if (comicInfoXml is null) |
| | | 38 | | { |
| | 0 | 39 | | _logger.LogInformation("Could not load ComicInfo metadata for {Path} from XML file. No internal XML in comic |
| | 0 | 40 | | return new MetadataResult<Book> { HasMetadata = false }; |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | var book = ComicInfoReader.ReadComicBookMetadata(comicInfoXml); |
| | | 44 | | |
| | 0 | 45 | | if (book is null) |
| | | 46 | | { |
| | 0 | 47 | | return new MetadataResult<Book> { HasMetadata = false }; |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | var metadataResult = new MetadataResult<Book> { Item = book, HasMetadata = true }; |
| | | 51 | | |
| | 0 | 52 | | ComicInfoReader.ReadPeopleMetadata(comicInfoXml, metadataResult); |
| | 0 | 53 | | ComicInfoReader.ReadCultureInfoInto(comicInfoXml, "ComicInfo/LanguageISO", cultureInfo => metadataResult.ResultL |
| | | 54 | | |
| | 0 | 55 | | return metadataResult; |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | public bool HasItemChanged(BaseItem item) |
| | | 60 | | { |
| | 0 | 61 | | var file = GetComicBookFile(item.Path); |
| | | 62 | | |
| | 0 | 63 | | if (file is null) |
| | | 64 | | { |
| | 0 | 65 | | return false; |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | return file.Exists && _fileSystem.GetLastWriteTimeUtc(file) > item.DateLastSaved; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | private async Task<XDocument?> LoadXml(ItemInfo info, CancellationToken cancellationToken) |
| | | 72 | | { |
| | 0 | 73 | | var path = GetComicBookFile(info.Path)?.FullName; |
| | | 74 | | |
| | 0 | 75 | | if (path is null) |
| | | 76 | | { |
| | 0 | 77 | | return null; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | try |
| | | 81 | | { |
| | | 82 | | // open the comic archive and try to get the ComicInfo.xml entry |
| | 0 | 83 | | using var comicBookFile = await ZipFile.OpenReadAsync(path, cancellationToken).ConfigureAwait(false); |
| | 0 | 84 | | var container = comicBookFile.GetEntry(ComicInfoReader.ComicRackMetaFile); |
| | | 85 | | |
| | 0 | 86 | | if (container is null) |
| | | 87 | | { |
| | 0 | 88 | | return null; |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | using var containerStream = await container.OpenAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 92 | | var comicInfoXml = XDocument.LoadAsync(containerStream, LoadOptions.None, cancellationToken); |
| | | 93 | | |
| | 0 | 94 | | return await comicInfoXml.ConfigureAwait(false); |
| | | 95 | | } |
| | 0 | 96 | | catch (Exception e) |
| | | 97 | | { |
| | 0 | 98 | | _logger.LogError(e, "could not load internal XML from {Path}", path); |
| | 0 | 99 | | return null; |
| | | 100 | | } |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | private FileSystemMetadata? GetComicBookFile(string path) |
| | | 104 | | { |
| | 0 | 105 | | var fileInfo = _fileSystem.GetFileSystemInfo(path); |
| | | 106 | | |
| | 0 | 107 | | if (fileInfo.IsDirectory) |
| | | 108 | | { |
| | 0 | 109 | | return null; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | // only parse files that are known to have internal metadata |
| | 0 | 113 | | if (!string.Equals(fileInfo.Extension, ".cbz", StringComparison.OrdinalIgnoreCase)) |
| | | 114 | | { |
| | 0 | 115 | | return null; |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | return fileInfo; |
| | | 119 | | } |
| | | 120 | | } |