aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Providers/Books/OpenPackagingFormat/EpubImageProvider.cs
blob: 69cae77628f9ba67c256d1b22ab8c9c89aeed626 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
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.Entities;
using Microsoft.Extensions.Logging;

namespace MediaBrowser.Providers.Books.OpenPackagingFormat
{
    /// <summary>
    /// Provides the primary image for EPUB items that have embedded covers.
    /// </summary>
    public class EpubImageProvider : IDynamicImageProvider
    {
        private readonly ILogger<EpubImageProvider> _logger;

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

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

        /// <inheritdoc />
        public bool Supports(BaseItem item)
        {
            return item is Book;
        }

        /// <inheritdoc />
        public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
        {
            yield return ImageType.Primary;
        }

        /// <inheritdoc />
        public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
        {
            if (string.Equals(Path.GetExtension(item.Path), ".epub", StringComparison.OrdinalIgnoreCase))
            {
                return GetFromZip(item, cancellationToken);
            }

            return Task.FromResult(new DynamicImageResponse { HasImage = false });
        }

        private async Task<DynamicImageResponse> LoadCover(ZipArchive epub, XmlDocument opf, string opfRootDirectory, CancellationToken cancellationToken)
        {
            var utilities = new OpfReader<EpubImageProvider>(opf, _logger);
            var coverReference = utilities.ReadCoverPath(opfRootDirectory);
            if (coverReference == null)
            {
                return new DynamicImageResponse { HasImage = false };
            }

            var cover = coverReference.Value;
            var coverFile = epub.GetEntry(cover.Path);

            if (coverFile == null)
            {
                return new DynamicImageResponse { HasImage = false };
            }

            var memoryStream = new MemoryStream();

            var coverStream = await coverFile.OpenAsync(cancellationToken).ConfigureAwait(false);
            await using (coverStream.ConfigureAwait(false))
            {
                await coverStream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);
            }

            memoryStream.Position = 0;

            var response = new DynamicImageResponse { HasImage = true, Stream = memoryStream };
            response.SetFormatFromMimeType(cover.MimeType);

            return response;
        }

        private async Task<DynamicImageResponse> GetFromZip(BaseItem item, CancellationToken cancellationToken)
        {
            using var epub = await ZipFile.OpenReadAsync(item.Path, cancellationToken).ConfigureAwait(false);

            var opfFilePath = EpubUtils.ReadContentFilePath(epub);
            if (opfFilePath == null)
            {
                return new DynamicImageResponse { HasImage = false };
            }

            var opfRootDirectory = Path.GetDirectoryName(opfFilePath);
            if (opfRootDirectory == null)
            {
                return new DynamicImageResponse { HasImage = false };
            }

            var opfFile = epub.GetEntry(opfFilePath);
            if (opfFile == null)
            {
                return new DynamicImageResponse { HasImage = false };
            }

            using var opfStream = await opfFile.OpenAsync(cancellationToken).ConfigureAwait(false);

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

            return await LoadCover(epub, opfDocument, opfRootDirectory, cancellationToken).ConfigureAwait(false);
        }
    }
}