aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Providers/Books/OpenPackagingFormat/EpubProvider.cs
blob: bc77e5928d9bf53299aa371fa7361042c75fa697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;

namespace MediaBrowser.Providers.Books.OpenPackagingFormat
{
    /// <summary>
    /// Provides book metadata from OPF content in an EPUB item.
    /// </summary>
    public class EpubProvider : ILocalMetadataProvider<Book>
    {
        private readonly IFileSystem _fileSystem;
        private readonly ILogger<EpubProvider> _logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="EpubProvider"/> class.
        /// </summary>
        /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
        /// <param name="logger">Instance of the <see cref="ILogger{EpubProvider}"/> interface.</param>
        public EpubProvider(IFileSystem fileSystem, ILogger<EpubProvider> logger)
        {
            _fileSystem = fileSystem;
            _logger = logger;
        }

        /// <inheritdoc />
        public string Name => "EPUB Metadata";

        /// <inheritdoc />
        public Task<MetadataResult<Book>> GetMetadata(ItemInfo info, IDirectoryService directoryService, CancellationToken cancellationToken)
        {
            var path = GetEpubFile(info.Path)?.FullName;

            if (path is null)
            {
                return Task.FromResult(new MetadataResult<Book> { HasMetadata = false });
            }

            var result = ReadEpubAsZip(path, cancellationToken);

            if (result is null)
            {
                return Task.FromResult(new MetadataResult<Book> { HasMetadata = false });
            }
            else
            {
                return Task.FromResult(result);
            }
        }

        private FileSystemMetadata? GetEpubFile(string path)
        {
            var fileInfo = _fileSystem.GetFileSystemInfo(path);

            if (fileInfo.IsDirectory)
            {
                return null;
            }

            if (!string.Equals(Path.GetExtension(fileInfo.FullName), ".epub", StringComparison.OrdinalIgnoreCase))
            {
                return null;
            }

            return fileInfo;
        }

        private MetadataResult<Book>? ReadEpubAsZip(string path, CancellationToken cancellationToken)
        {
            using var epub = ZipFile.OpenRead(path);

            var opfFilePath = EpubUtils.ReadContentFilePath(epub);
            if (opfFilePath == null)
            {
                return null;
            }

            var opf = epub.GetEntry(opfFilePath);
            if (opf == null)
            {
                return null;
            }

            using var opfStream = opf.Open();

            var opfDocument = new XmlDocument();
            opfDocument.Load(opfStream);

            var utilities = new OpfReader<EpubProvider>(opfDocument, _logger);
            return utilities.ReadOpfData(cancellationToken);
        }
    }
}