File size: 13,610 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using VersOne.Epub.Environment;
using VersOne.Epub.Options;
using VersOne.Epub.Utils;

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

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

        public EpubBook ReadBook(string filePath)
        {
            return ReadBookAsync(filePath).ExecuteAndUnwrapAggregateException();
        }

        public EpubBook ReadBook(Stream stream)
        {
            return ReadBookAsync(stream).ExecuteAndUnwrapAggregateException();
        }

        public async Task<EpubBook> ReadBookAsync(string filePath)
        {
            BookRefReader bookRefReader = new(environmentDependencies, epubReaderOptions);
            EpubBookRef epubBookRef = await bookRefReader.OpenBookAsync(filePath).ConfigureAwait(false);
            return await ReadBookAsync(epubBookRef).ConfigureAwait(false);
        }

        public async Task<EpubBook> ReadBookAsync(Stream stream)
        {
            BookRefReader bookRefReader = new(environmentDependencies, epubReaderOptions);
            EpubBookRef epubBookRef = await bookRefReader.OpenBookAsync(stream).ConfigureAwait(false);
            return await ReadBookAsync(epubBookRef).ConfigureAwait(false);
        }

        private static List<EpubLocalTextContentFile> ReadReadingOrder(EpubContent epubContent, List<EpubLocalTextContentFileRef> htmlContentFileRefs)
        {
            return htmlContentFileRefs.Select(htmlContentFileRef => epubContent.Html.GetLocalFileByKey(htmlContentFileRef.Key)).ToList();
        }

        private static List<EpubNavigationItem> ReadNavigation(EpubContent epubContent, List<EpubNavigationItemRef> navigationItemRefs)
        {
            List<EpubNavigationItem> result = new();
            foreach (EpubNavigationItemRef navigationItemRef in navigationItemRefs)
            {
                EpubNavigationItemType type = navigationItemRef.Type;
                string title = navigationItemRef.Title;
                EpubNavigationItemLink? link = navigationItemRef.Link;
                EpubLocalTextContentFile? htmlContentFile = null;

                if (navigationItemRef.HtmlContentFileRef != null)
                {
                    htmlContentFile = epubContent.Html.GetLocalFileByKey(navigationItemRef.HtmlContentFileRef.Key);
                }
                List<EpubNavigationItem> nestedItems = ReadNavigation(epubContent, navigationItemRef.NestedItems);
                result.Add(new EpubNavigationItem(type, title, link, htmlContentFile, nestedItems));
            }
            return result;
        }

        private static async Task<EpubLocalTextContentFile> ReadLocalTextContentFile(EpubLocalContentFileRef localContentFileRef)
        {
            string key = localContentFileRef.Key;
            EpubContentType contentType = localContentFileRef.ContentType;
            string contentMimeType = localContentFileRef.ContentMimeType;
            string filePath = localContentFileRef.FilePath;
            string content = await localContentFileRef.ReadContentAsTextAsync().ConfigureAwait(false);
            return new(key, contentType, contentMimeType, filePath, content);
        }

        private static async Task<EpubLocalByteContentFile> ReadLocalByteContentFile(EpubLocalContentFileRef localContentFileRef)
        {
            string key = localContentFileRef.Key;
            EpubContentType contentType = localContentFileRef.ContentType;
            string contentMimeType = localContentFileRef.ContentMimeType;
            string filePath = localContentFileRef.FilePath;
            byte[] content = await localContentFileRef.ReadContentAsBytesAsync().ConfigureAwait(false);
            return new(key, contentType, contentMimeType, filePath, content);
        }

        private async Task<EpubBook> ReadBookAsync(EpubBookRef epubBookRef)
        {
            using (epubBookRef)
            {
                string? filePath = epubBookRef.FilePath;
                EpubSchema schema = epubBookRef.Schema;
                string title = epubBookRef.Title;
                List<string> authorList = epubBookRef.AuthorList;
                string author = epubBookRef.Author;
                EpubContent content = await ReadContent(epubBookRef.Content).ConfigureAwait(false);
                byte[]? coverImage = await epubBookRef.ReadCoverAsync().ConfigureAwait(false);
                string? description = epubBookRef.Description;
                List<EpubLocalTextContentFileRef> htmlContentFileRefs = await epubBookRef.GetReadingOrderAsync().ConfigureAwait(false);
                List<EpubLocalTextContentFile> readingOrder = ReadReadingOrder(content, htmlContentFileRefs);
                List<EpubNavigationItemRef>? navigationItemRefs = await epubBookRef.GetNavigationAsync().ConfigureAwait(false);
                List<EpubNavigationItem>? navigation = navigationItemRefs != null ? ReadNavigation(content, navigationItemRefs) : null;
                return new(filePath, title, author, authorList, description, coverImage, readingOrder, navigation, schema, content);
            }
        }

        private async Task<EpubContent> ReadContent(EpubContentRef contentRef)
        {
            EpubLocalByteContentFile? cover = null;
            EpubLocalTextContentFile? navigationHtmlFile = null;
            EpubContentCollection<EpubLocalTextContentFile, EpubRemoteTextContentFile> html = await ReadTextContentFiles(contentRef.Html).ConfigureAwait(false);
            EpubContentCollection<EpubLocalTextContentFile, EpubRemoteTextContentFile> css = await ReadTextContentFiles(contentRef.Css).ConfigureAwait(false);
            EpubContentCollection<EpubLocalByteContentFile, EpubRemoteByteContentFile> images = await ReadByteContentFiles(contentRef.Images).ConfigureAwait(false);
            EpubContentCollection<EpubLocalByteContentFile, EpubRemoteByteContentFile> fonts = await ReadByteContentFiles(contentRef.Fonts).ConfigureAwait(false);
            EpubContentCollection<EpubLocalByteContentFile, EpubRemoteByteContentFile> audio = await ReadByteContentFiles(contentRef.Audio).ConfigureAwait(false);
            List<EpubLocalContentFile> allFilesLocal = new();
            HashSet<string> allFilesLocalKeys = new();
            List<EpubRemoteContentFile> allFilesRemote = new();
            HashSet<string> allFilesRemoteKeys = new();
            foreach (EpubLocalTextContentFile localTextContentFile in html.Local.Concat(css.Local))
            {
                allFilesLocal.Add(localTextContentFile);
                allFilesLocalKeys.Add(localTextContentFile.Key);
            }
            foreach (EpubRemoteTextContentFile remoteTextContentFile in html.Remote.Concat(css.Remote))
            {
                allFilesRemote.Add(remoteTextContentFile);
                allFilesRemoteKeys.Add(remoteTextContentFile.Key);
            }
            foreach (EpubLocalByteContentFile localByteContentFile in images.Local.Concat(fonts.Local).Concat(audio.Local))
            {
                allFilesLocal.Add(localByteContentFile);
                allFilesLocalKeys.Add(localByteContentFile.Key);
            }
            foreach (EpubRemoteByteContentFile remoteByteContentFile in images.Remote.Concat(fonts.Remote).Concat(audio.Remote))
            {
                allFilesRemote.Add(remoteByteContentFile);
                allFilesRemoteKeys.Add(remoteByteContentFile.Key);
            }
            foreach (EpubLocalContentFileRef localContentFileRef in contentRef.AllFiles.Local)
            {
                if (!allFilesLocalKeys.Contains(localContentFileRef.Key))
                {
                    if (localContentFileRef is EpubLocalTextContentFileRef)
                    {
                        allFilesLocal.Add(await ReadLocalTextContentFile(localContentFileRef).ConfigureAwait(false));
                    }
                    else
                    {
                        allFilesLocal.Add(await ReadLocalByteContentFile(localContentFileRef).ConfigureAwait(false));
                    }
                    allFilesLocalKeys.Add(localContentFileRef.Key);
                }
            }
            foreach (EpubRemoteContentFileRef remoteContentFileRef in contentRef.AllFiles.Remote)
            {
                if (!allFilesRemoteKeys.Contains(remoteContentFileRef.Key))
                {
                    if (remoteContentFileRef is EpubRemoteTextContentFileRef)
                    {
                        allFilesRemote.Add(await DownloadRemoteTextContentFile(remoteContentFileRef).ConfigureAwait(false));
                    }
                    else
                    {
                        allFilesRemote.Add(await DownloadRemoteByteContentFile(remoteContentFileRef).ConfigureAwait(false));
                    }
                    allFilesRemoteKeys.Add(remoteContentFileRef.Key);
                }
            }
            EpubContentCollection<EpubLocalContentFile, EpubRemoteContentFile> allFiles = new(allFilesLocal.AsReadOnly(), allFilesRemote.AsReadOnly());
            if (contentRef.Cover != null)
            {
                cover = images.GetLocalFileByKey(contentRef.Cover.Key);
            }
            if (contentRef.NavigationHtmlFile != null)
            {
                navigationHtmlFile = html.GetLocalFileByKey(contentRef.NavigationHtmlFile.Key);
            }
            return new(cover, navigationHtmlFile, html, css, images, fonts, audio, allFiles);
        }

        private async Task<EpubContentCollection<EpubLocalTextContentFile, EpubRemoteTextContentFile>> ReadTextContentFiles(
            EpubContentCollectionRef<EpubLocalTextContentFileRef, EpubRemoteTextContentFileRef> textContentFileCollectionRef)
        {
            List<EpubLocalTextContentFile> local = new();
            List<EpubRemoteTextContentFile> remote = new();
            foreach (EpubLocalTextContentFileRef localTextContentFileRef in textContentFileCollectionRef.Local)
            {
                local.Add(await ReadLocalTextContentFile(localTextContentFileRef).ConfigureAwait(false));
            }
            foreach (EpubRemoteTextContentFileRef remoteTextContentFileRef in textContentFileCollectionRef.Remote)
            {
                remote.Add(await DownloadRemoteTextContentFile(remoteTextContentFileRef).ConfigureAwait(false));
            }
            EpubContentCollection<EpubLocalTextContentFile, EpubRemoteTextContentFile> result = new(local.AsReadOnly(), remote.AsReadOnly());
            return result;
        }

        private async Task<EpubContentCollection<EpubLocalByteContentFile, EpubRemoteByteContentFile>> ReadByteContentFiles(
            EpubContentCollectionRef<EpubLocalByteContentFileRef, EpubRemoteByteContentFileRef> byteContentFileCollectionRef)
        {
            List<EpubLocalByteContentFile> local = new();
            List<EpubRemoteByteContentFile> remote = new();
            foreach (EpubLocalByteContentFileRef localByteContentFileRef in byteContentFileCollectionRef.Local)
            {
                local.Add(await ReadLocalByteContentFile(localByteContentFileRef).ConfigureAwait(false));
            }
            foreach (EpubRemoteByteContentFileRef remoteByteContentFileRef in byteContentFileCollectionRef.Remote)
            {
                remote.Add(await DownloadRemoteByteContentFile(remoteByteContentFileRef).ConfigureAwait(false));
            }
            EpubContentCollection<EpubLocalByteContentFile, EpubRemoteByteContentFile> result = new(local.AsReadOnly(), remote.AsReadOnly());
            return result;
        }

        private async Task<EpubRemoteTextContentFile> DownloadRemoteTextContentFile(EpubRemoteContentFileRef remoteContentFileRef)
        {
            string key = remoteContentFileRef.Key;
            EpubContentType contentType = remoteContentFileRef.ContentType;
            string contentMimeType = remoteContentFileRef.ContentMimeType;
            string? content = null;
            if (epubReaderOptions.ContentDownloaderOptions != null && epubReaderOptions.ContentDownloaderOptions.DownloadContent)
            {
                content = await remoteContentFileRef.DownloadContentAsTextAsync().ConfigureAwait(false);
            }
            return new(key, contentType, contentMimeType, content);
        }

        private async Task<EpubRemoteByteContentFile> DownloadRemoteByteContentFile(EpubRemoteContentFileRef remoteContentFileRef)
        {
            string key = remoteContentFileRef.Key;
            EpubContentType contentType = remoteContentFileRef.ContentType;
            string contentMimeType = remoteContentFileRef.ContentMimeType;
            byte[]? content = null;
            if (epubReaderOptions.ContentDownloaderOptions != null && epubReaderOptions.ContentDownloaderOptions.DownloadContent)
            {
                content = await remoteContentFileRef.DownloadContentAsBytesAsync().ConfigureAwait(false);
            }
            return new(key, contentType, contentMimeType, content);
        }
    }
}