| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.IO.Compression; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using System.Xml; |
| | | 7 | | using MediaBrowser.Controller.Entities; |
| | | 8 | | using MediaBrowser.Controller.Providers; |
| | | 9 | | using MediaBrowser.Model.IO; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace MediaBrowser.Providers.Books.OpenPackagingFormat |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides book metadata from OPF content in an EPUB item. |
| | | 16 | | /// </summary> |
| | | 17 | | public class EpubProvider : ILocalMetadataProvider<Book> |
| | | 18 | | { |
| | | 19 | | private readonly IFileSystem _fileSystem; |
| | | 20 | | private readonly ILogger<EpubProvider> _logger; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="EpubProvider"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | | 26 | | /// <param name="logger">Instance of the <see cref="ILogger{EpubProvider}"/> interface.</param> |
| | | 27 | | public EpubProvider(IFileSystem fileSystem, ILogger<EpubProvider> logger) |
| | | 28 | | { |
| | 21 | 29 | | _fileSystem = fileSystem; |
| | 21 | 30 | | _logger = logger; |
| | 21 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | 0 | 34 | | public string Name => "EPUB Metadata"; |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public Task<MetadataResult<Book>> GetMetadata(ItemInfo info, IDirectoryService directoryService, CancellationTok |
| | | 38 | | { |
| | 0 | 39 | | var path = GetEpubFile(info.Path)?.FullName; |
| | | 40 | | |
| | 0 | 41 | | if (path is null) |
| | | 42 | | { |
| | 0 | 43 | | return Task.FromResult(new MetadataResult<Book> { HasMetadata = false }); |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | var result = ReadEpubAsZip(path, cancellationToken); |
| | | 47 | | |
| | 0 | 48 | | if (result is null) |
| | | 49 | | { |
| | 0 | 50 | | return Task.FromResult(new MetadataResult<Book> { HasMetadata = false }); |
| | | 51 | | } |
| | | 52 | | else |
| | | 53 | | { |
| | 0 | 54 | | return Task.FromResult(result); |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private FileSystemMetadata? GetEpubFile(string path) |
| | | 59 | | { |
| | 0 | 60 | | var fileInfo = _fileSystem.GetFileSystemInfo(path); |
| | | 61 | | |
| | 0 | 62 | | if (fileInfo.IsDirectory) |
| | | 63 | | { |
| | 0 | 64 | | return null; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | if (!string.Equals(Path.GetExtension(fileInfo.FullName), ".epub", StringComparison.OrdinalIgnoreCase)) |
| | | 68 | | { |
| | 0 | 69 | | return null; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | return fileInfo; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | private MetadataResult<Book>? ReadEpubAsZip(string path, CancellationToken cancellationToken) |
| | | 76 | | { |
| | 0 | 77 | | using var epub = ZipFile.OpenRead(path); |
| | | 78 | | |
| | 0 | 79 | | var opfFilePath = EpubUtils.ReadContentFilePath(epub); |
| | 0 | 80 | | if (opfFilePath == null) |
| | | 81 | | { |
| | 0 | 82 | | return null; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | var opf = epub.GetEntry(opfFilePath); |
| | 0 | 86 | | if (opf == null) |
| | | 87 | | { |
| | 0 | 88 | | return null; |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | using var opfStream = opf.Open(); |
| | | 92 | | |
| | 0 | 93 | | var opfDocument = new XmlDocument(); |
| | 0 | 94 | | opfDocument.Load(opfStream); |
| | | 95 | | |
| | 0 | 96 | | var utilities = new OpfReader<EpubProvider>(opfDocument, _logger); |
| | 0 | 97 | | return utilities.ReadOpfData(cancellationToken); |
| | 0 | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |