File size: 12,452 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using VersOne.Epub.Environment;
using VersOne.Epub.Options;
using VersOne.Epub.Schema;

namespace VersOne.Epub.Internal
{
    internal class ContentReader
    {
        private readonly IEnvironmentDependencies environmentDependencies;
        private readonly EpubReaderOptions epubReaderOptions;

        public ContentReader(IEnvironmentDependencies environmentDependencies, EpubReaderOptions? epubReaderOptions = null)
        {
            this.environmentDependencies = environmentDependencies ?? throw new ArgumentNullException(nameof(environmentDependencies));
            this.epubReaderOptions = epubReaderOptions ?? new EpubReaderOptions();
        }

        public EpubContentRef ParseContentMap(EpubSchema epubSchema, IZipFile epubFile)
        {
            EpubLocalByteContentFileRef? cover;
            EpubLocalTextContentFileRef? navigationHtmlFile = null;
            List<EpubLocalTextContentFileRef> htmlLocal = new();
            List<EpubRemoteTextContentFileRef> htmlRemote = new();
            List<EpubLocalTextContentFileRef> cssLocal = new();
            List<EpubRemoteTextContentFileRef> cssRemote = new();
            List<EpubLocalByteContentFileRef> imagesLocal = new();
            List<EpubRemoteByteContentFileRef> imagesRemote = new();
            List<EpubLocalByteContentFileRef> fontsLocal = new();
            List<EpubRemoteByteContentFileRef> fontsRemote = new();
            List<EpubLocalByteContentFileRef> audioLocal = new();
            List<EpubRemoteByteContentFileRef> audioRemote = new();
            List<EpubLocalContentFileRef> allFilesLocal = new();
            List<EpubRemoteContentFileRef> allFilesRemote = new();
            EpubLocalContentLoader localContentLoader = new(environmentDependencies, epubFile, epubSchema.ContentDirectoryPath, epubReaderOptions.ContentReaderOptions);
            EpubRemoteContentLoader? remoteContentLoader = null;
            foreach (EpubManifestItem manifestItem in epubSchema.Package.Manifest.Items)
            {
                string href = manifestItem.Href;
                EpubContentLocation contentLocation = ContentPathUtils.IsLocalPath(href) ? EpubContentLocation.LOCAL : EpubContentLocation.REMOTE;
                string contentMimeType = manifestItem.MediaType;
                EpubContentType contentType = GetContentTypeByContentMimeType(contentMimeType);
                string contentDirectoryPath = epubSchema.ContentDirectoryPath;
                EpubContentFileRefMetadata contentFileRefMetadata = new(href, contentType, contentMimeType);
                switch (contentType)
                {
                    case EpubContentType.XHTML_1_1:
                    case EpubContentType.CSS:
                    case EpubContentType.OEB1_DOCUMENT:
                    case EpubContentType.OEB1_CSS:
                    case EpubContentType.XML:
                    case EpubContentType.DTBOOK:
                    case EpubContentType.DTBOOK_NCX:
                    case EpubContentType.SMIL:
                    case EpubContentType.SCRIPT:
                        if (contentLocation == EpubContentLocation.LOCAL)
                        {
                            string contentFilePath = ContentPathUtils.Combine(contentDirectoryPath, href);
                            EpubLocalTextContentFileRef localTextContentFile = new(contentFileRefMetadata, contentFilePath, localContentLoader);
                            switch (contentType)
                            {
                                case EpubContentType.XHTML_1_1:
                                    htmlLocal.Add(localTextContentFile);
                                    if (navigationHtmlFile == null && manifestItem.Properties != null && manifestItem.Properties.Contains(EpubManifestProperty.NAV))
                                    {
                                        navigationHtmlFile = localTextContentFile;
                                    }
                                    break;
                                case EpubContentType.CSS:
                                    cssLocal.Add(localTextContentFile);
                                    break;
                            }
                            allFilesLocal.Add(localTextContentFile);
                        }
                        else
                        {
                            remoteContentLoader ??= new EpubRemoteContentLoader(environmentDependencies, epubReaderOptions.ContentDownloaderOptions);
                            EpubRemoteTextContentFileRef remoteTextContentFile = new(contentFileRefMetadata, remoteContentLoader);
                            switch (contentType)
                            {
                                case EpubContentType.XHTML_1_1:
                                    if (manifestItem.Properties != null && manifestItem.Properties.Contains(EpubManifestProperty.NAV))
                                    {
                                        throw new EpubPackageException($"Incorrect EPUB manifest: EPUB 3 navigation document \"{href}\" cannot be a remote resource.");
                                    }
                                    htmlRemote.Add(remoteTextContentFile);
                                    break;
                                case EpubContentType.CSS:
                                    cssRemote.Add(remoteTextContentFile);
                                    break;
                            }
                            allFilesRemote.Add(remoteTextContentFile);
                        }
                        break;
                    default:
                        if (contentLocation == EpubContentLocation.LOCAL)
                        {
                            string contentFilePath = ContentPathUtils.Combine(contentDirectoryPath, href);
                            EpubLocalByteContentFileRef localByteContentFile = new(contentFileRefMetadata, contentFilePath, localContentLoader);
                            switch (contentType)
                            {
                                case EpubContentType.IMAGE_GIF:
                                case EpubContentType.IMAGE_JPEG:
                                case EpubContentType.IMAGE_PNG:
                                case EpubContentType.IMAGE_SVG:
                                case EpubContentType.IMAGE_WEBP:
                                case EpubContentType.IMAGE_BMP:
                                    imagesLocal.Add(localByteContentFile);
                                    break;
                                case EpubContentType.FONT_TRUETYPE:
                                case EpubContentType.FONT_OPENTYPE:
                                case EpubContentType.FONT_SFNT:
                                case EpubContentType.FONT_WOFF:
                                case EpubContentType.FONT_WOFF2:
                                    fontsLocal.Add(localByteContentFile);
                                    break;
                                case EpubContentType.AUDIO_MP3:
                                case EpubContentType.AUDIO_MP4:
                                case EpubContentType.AUDIO_OGG:
                                    audioLocal.Add(localByteContentFile);
                                    break;
                            }
                            allFilesLocal.Add(localByteContentFile);
                        }
                        else
                        {
                            remoteContentLoader ??= new EpubRemoteContentLoader(environmentDependencies, epubReaderOptions.ContentDownloaderOptions);
                            EpubRemoteByteContentFileRef remoteByteContentFile = new(contentFileRefMetadata, remoteContentLoader);
                            switch (contentType)
                            {
                                case EpubContentType.IMAGE_GIF:
                                case EpubContentType.IMAGE_JPEG:
                                case EpubContentType.IMAGE_PNG:
                                case EpubContentType.IMAGE_SVG:
                                case EpubContentType.IMAGE_WEBP:
                                case EpubContentType.IMAGE_BMP:
                                    imagesRemote.Add(remoteByteContentFile);
                                    break;
                                case EpubContentType.FONT_TRUETYPE:
                                case EpubContentType.FONT_OPENTYPE:
                                case EpubContentType.FONT_SFNT:
                                case EpubContentType.FONT_WOFF:
                                case EpubContentType.FONT_WOFF2:
                                    fontsRemote.Add(remoteByteContentFile);
                                    break;
                                case EpubContentType.AUDIO_MP3:
                                case EpubContentType.AUDIO_MP4:
                                case EpubContentType.AUDIO_OGG:
                                    audioRemote.Add(remoteByteContentFile);
                                    break;
                            }
                            allFilesRemote.Add(remoteByteContentFile);
                        }
                        break;
                }
            }
            EpubContentCollectionRef<EpubLocalTextContentFileRef, EpubRemoteTextContentFileRef> html = new(htmlLocal.AsReadOnly(), htmlRemote.AsReadOnly());
            EpubContentCollectionRef<EpubLocalTextContentFileRef, EpubRemoteTextContentFileRef> css = new(cssLocal.AsReadOnly(), cssRemote.AsReadOnly());
            EpubContentCollectionRef<EpubLocalByteContentFileRef, EpubRemoteByteContentFileRef> images = new(imagesLocal.AsReadOnly(), imagesRemote.AsReadOnly());
            EpubContentCollectionRef<EpubLocalByteContentFileRef, EpubRemoteByteContentFileRef> fonts = new(fontsLocal.AsReadOnly(), fontsRemote.AsReadOnly());
            EpubContentCollectionRef<EpubLocalByteContentFileRef, EpubRemoteByteContentFileRef> audio = new(audioLocal.AsReadOnly(), audioRemote.AsReadOnly());
            EpubContentCollectionRef<EpubLocalContentFileRef, EpubRemoteContentFileRef> allFiles = new(allFilesLocal.AsReadOnly(), allFilesRemote.AsReadOnly());
            cover = BookCoverReader.ReadBookCover(epubSchema, images, epubReaderOptions.BookCoverReaderOptions);
            return new(cover, navigationHtmlFile, html, css, images, fonts, audio, allFiles);
        }

        private static EpubContentType GetContentTypeByContentMimeType(string contentMimeType)
        {
            return contentMimeType.ToLowerInvariant() switch
            {
                "application/xhtml+xml" => EpubContentType.XHTML_1_1,
                "application/x-dtbook+xml" => EpubContentType.DTBOOK,
                "application/x-dtbncx+xml" => EpubContentType.DTBOOK_NCX,
                "text/x-oeb1-document" => EpubContentType.OEB1_DOCUMENT,
                "application/xml" => EpubContentType.XML,
                "text/css" => EpubContentType.CSS,
                "text/x-oeb1-css" => EpubContentType.OEB1_CSS,
                "application/javascript" or "application/ecmascript" or "text/javascript" => EpubContentType.SCRIPT,
                "image/gif" => EpubContentType.IMAGE_GIF,
                "image/jpeg" => EpubContentType.IMAGE_JPEG,
                "image/png" => EpubContentType.IMAGE_PNG,
                "image/svg+xml" => EpubContentType.IMAGE_SVG,
                "image/webp" => EpubContentType.IMAGE_WEBP,
                "image/bmp" => EpubContentType.IMAGE_BMP,
                "font/truetype" or "font/ttf" or "application/x-font-truetype" => EpubContentType.FONT_TRUETYPE,
                "font/opentype" or "font/otf" or "application/vnd.ms-opentype" => EpubContentType.FONT_OPENTYPE,
                "font/sfnt" or "application/font-sfnt" => EpubContentType.FONT_SFNT,
                "font/woff" or "application/font-woff" => EpubContentType.FONT_WOFF,
                "font/woff2" => EpubContentType.FONT_WOFF2,
                "application/smil+xml" => EpubContentType.SMIL,
                "audio/mpeg" => EpubContentType.AUDIO_MP3,
                "audio/mp4" => EpubContentType.AUDIO_MP4,
                "audio/ogg" or "audio/ogg; codecs=opus" => EpubContentType.AUDIO_OGG,
                _ => EpubContentType.OTHER
            };
        }
    }
}