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 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 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 ReadReadingOrder(EpubContent epubContent, List htmlContentFileRefs) { return htmlContentFileRefs.Select(htmlContentFileRef => epubContent.Html.GetLocalFileByKey(htmlContentFileRef.Key)).ToList(); } private static List ReadNavigation(EpubContent epubContent, List navigationItemRefs) { List 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 nestedItems = ReadNavigation(epubContent, navigationItemRef.NestedItems); result.Add(new EpubNavigationItem(type, title, link, htmlContentFile, nestedItems)); } return result; } private static async Task 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 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 ReadBookAsync(EpubBookRef epubBookRef) { using (epubBookRef) { string? filePath = epubBookRef.FilePath; EpubSchema schema = epubBookRef.Schema; string title = epubBookRef.Title; List 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 htmlContentFileRefs = await epubBookRef.GetReadingOrderAsync().ConfigureAwait(false); List readingOrder = ReadReadingOrder(content, htmlContentFileRefs); List? navigationItemRefs = await epubBookRef.GetNavigationAsync().ConfigureAwait(false); List? navigation = navigationItemRefs != null ? ReadNavigation(content, navigationItemRefs) : null; return new(filePath, title, author, authorList, description, coverImage, readingOrder, navigation, schema, content); } } private async Task ReadContent(EpubContentRef contentRef) { EpubLocalByteContentFile? cover = null; EpubLocalTextContentFile? navigationHtmlFile = null; EpubContentCollection html = await ReadTextContentFiles(contentRef.Html).ConfigureAwait(false); EpubContentCollection css = await ReadTextContentFiles(contentRef.Css).ConfigureAwait(false); EpubContentCollection images = await ReadByteContentFiles(contentRef.Images).ConfigureAwait(false); EpubContentCollection fonts = await ReadByteContentFiles(contentRef.Fonts).ConfigureAwait(false); EpubContentCollection audio = await ReadByteContentFiles(contentRef.Audio).ConfigureAwait(false); List allFilesLocal = new(); HashSet allFilesLocalKeys = new(); List allFilesRemote = new(); HashSet 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 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> ReadTextContentFiles( EpubContentCollectionRef textContentFileCollectionRef) { List local = new(); List 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 result = new(local.AsReadOnly(), remote.AsReadOnly()); return result; } private async Task> ReadByteContentFiles( EpubContentCollectionRef byteContentFileCollectionRef) { List local = new(); List 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 result = new(local.AsReadOnly(), remote.AsReadOnly()); return result; } private async Task 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 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); } } }