public static EpubByteContentFileRef ReadBookCover(EpubSchema epubSchema, Dictionary <string, EpubByteContentFileRef> imageContentRefs) { List <EpubMetadataMeta> metaItems = epubSchema.Package.Metadata.MetaItems; if (metaItems == null || !metaItems.Any()) { return(null); } EpubMetadataMeta coverMetaItem = metaItems.FirstOrDefault(metaItem => metaItem.Name.CompareOrdinalIgnoreCase("cover")); if (coverMetaItem == null) { return(null); } if (String.IsNullOrEmpty(coverMetaItem.Content)) { throw new Exception("Incorrect EPUB metadata: cover item content is missing."); } EpubManifestItem coverManifestItem = epubSchema.Package.Manifest.FirstOrDefault(manifestItem => manifestItem.Id.CompareOrdinalIgnoreCase(coverMetaItem.Content)); if (coverManifestItem == null) { throw new Exception($"Incorrect EPUB manifest: item with ID = \"{coverMetaItem.Content}\" is missing."); } if (!imageContentRefs.TryGetValue(coverManifestItem.Href, out EpubByteContentFileRef coverImageContentFileRef)) { throw new Exception($"Incorrect EPUB manifest: item with href = \"{coverManifestItem.Href}\" is missing."); } return(coverImageContentFileRef); }
public static EpubByteContentFileRef ReadBookCover(EpubSchema epubSchema, Dictionary <string, EpubByteContentFileRef> imageContentRefs) { List <EpubMetadataMeta> metaItems = epubSchema.Package.Metadata.MetaItems; if (metaItems == null || !metaItems.Any()) { return(null); } EpubMetadataMeta coverMetaItem = metaItems.FirstOrDefault(metaItem => metaItem.Name.CompareOrdinalIgnoreCase("cover")); if (coverMetaItem == null) { return(null); } if (String.IsNullOrEmpty(coverMetaItem.Content)) { throw new Exception("Incorrect EPUB metadata: cover item content is missing."); } EpubManifestItem coverManifestItem = epubSchema.Package.Manifest.FirstOrDefault(manifestItem => manifestItem.Id.CompareOrdinalIgnoreCase(coverMetaItem.Content)); if (coverManifestItem == null) { // 2019-08-20 Hotfix: if coverManifestItem is not found by its Id, then try it with its Href - some ebooks refer to the image directly! coverManifestItem = epubSchema.Package.Manifest.FirstOrDefault(manifestItem => manifestItem.Href.CompareOrdinalIgnoreCase(coverMetaItem.Content)); if (null == coverManifestItem) { throw new Exception($"Incorrect EPUB manifest: item with ID = \"{coverMetaItem.Content}\" is missing."); } } if (imageContentRefs.TryGetValue(coverManifestItem.Href, out EpubByteContentFileRef coverImageContentFileRef)) { return(coverImageContentFileRef); } // 2019-08-21 Fix some ebooks seem to contain more than one item with Id="cover" // thus we test if there is a second item.... coverManifestItem = epubSchema.Package.Manifest.Where(manifestItem => manifestItem.Id.CompareOrdinalIgnoreCase(coverMetaItem.Content)).Skip(1).FirstOrDefault();; if (coverManifestItem == null) { // 2019-08-20 Hotfix: if coverManifestItem is not found by its Id, then try it with its Href - some ebooks refer to the image directly! coverManifestItem = epubSchema.Package.Manifest.FirstOrDefault(manifestItem => manifestItem.Href.CompareOrdinalIgnoreCase(coverMetaItem.Content)); if (null == coverManifestItem) { throw new Exception($"Incorrect EPUB manifest: item with ID = \"{coverMetaItem.Content}\" is missing."); } } if (imageContentRefs.TryGetValue(coverManifestItem.Href, out EpubByteContentFileRef coverImageContentFileRef2)) { return(coverImageContentFileRef2); } throw new Exception($"Incorrect EPUB manifest: item with href = \"{coverManifestItem.Href}\" is missing."); }
public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive) { var result = new EpubSchema(); var rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive).ConfigureAwait(false); var contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath); result.ContentDirectoryPath = contentDirectoryPath; EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath).ConfigureAwait(false); result.Package = package; return(result); }
public static EpubSchema ReadSchema(ZipArchive epubArchive) { EpubSchema result = new EpubSchema(); string rootFilePath = RootFilePathReader.GetRootFilePath(epubArchive); string contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath); result.ContentDirectoryPath = contentDirectoryPath; EpubPackage package = PackageReader.ReadPackage(epubArchive, rootFilePath); result.Package = package; EpubNavigation navigation = NavigationReader.ReadNavigation(epubArchive, contentDirectoryPath, package); result.Navigation = navigation; return(result); }
public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive) { EpubSchema result = new EpubSchema(); string rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive).ConfigureAwait(false); string contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath); result.ContentDirectoryPath = contentDirectoryPath; EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath).ConfigureAwait(false); result.Package = package; EpubNavigation navigation = await NavigationReader.ReadNavigationAsync(epubArchive, contentDirectoryPath, package).ConfigureAwait(false); result.Navigation = navigation; return(result); }
public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive) { EpubSchema result = new EpubSchema(); string rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive).ConfigureAwait(false); string contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath); result.ContentDirectoryPath = contentDirectoryPath; EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath).ConfigureAwait(false); result.Package = package; result.Epub2Ncx = await Epub2NcxReader.ReadEpub2NcxAsync(epubArchive, contentDirectoryPath, package).ConfigureAwait(false); result.Epub3NavDocument = await Epub3NavDocumentReader.ReadEpub3NavDocumentAsync(epubArchive, contentDirectoryPath, package).ConfigureAwait(false); return(result); }
public static EpubByteContentFileRef ReadBookCoverVersion3(EpubSchema epubSchema, Dictionary <string, EpubByteContentFileRef> imageContentRefs) { // https://ebookflightdeck.com/handbook/coverimage var manifestItem = epubSchema.Package.Manifest.FirstOrDefault(m => m.Properties != null && m.Properties.Contains(ManifestProperty.COVER_IMAGE)); if (manifestItem == null) { return(null); } EpubByteContentFileRef coverImageContentFileRef; if (manifestItem.Href != null && imageContentRefs.TryGetValue(manifestItem.Href, out coverImageContentFileRef)) { return(coverImageContentFileRef); } return(null); }
public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive) { EpubSchema result = new EpubSchema(); // Reading META-INF/container.xml string rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive); // Getting directory path - usually it's: META-INF/ string contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath); result.ContentDirectoryPath = contentDirectoryPath; //Reading the file content.opf EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath); result.Package = package; EpubNavigation navigation = await NavigationReader.ReadNavigationAsync(epubArchive, contentDirectoryPath, package); result.Navigation = navigation; return(result); }
public static EpubByteContentFileRef ReadBookCover(EpubSchema epubSchema, Dictionary <string, EpubByteContentFileRef> imageContentRefs) { List <EpubMetadataMeta> metaItems = epubSchema.Package.Metadata.MetaItems; if (metaItems == null || !metaItems.Any()) { return(null); } EpubMetadataMeta coverMetaItem = metaItems.FirstOrDefault(metaItem => metaItem.Name.CompareOrdinalIgnoreCase("cover")); if (coverMetaItem == null) { return(null); } if (String.IsNullOrEmpty(coverMetaItem.Content)) { throw new Exception("Incorrect EPUB metadata: cover item content is missing."); } EpubByteContentFileRef coverImageContentFileRef; EpubManifestItem coverManifestItem = epubSchema.Package.Manifest.FirstOrDefault(manifestItem => manifestItem.Id.CompareOrdinalIgnoreCase(coverMetaItem.Content)); if (null != coverManifestItem?.Href && imageContentRefs.TryGetValue(coverManifestItem.Href, out coverImageContentFileRef)) { return(coverImageContentFileRef); } // For non-standard ebooks, we try several other ways... if (null != coverManifestItem) // we have found the item but there was no corresponding image ... { // some ebooks seem to contain more than one item with Id="cover" // thus we test if there is a second item, and whether that is an image.... coverManifestItem = epubSchema.Package.Manifest.Where(manifestItem => manifestItem.Id.CompareOrdinalIgnoreCase(coverMetaItem.Content)).Skip(1).FirstOrDefault();; if (null != coverManifestItem?.Href && imageContentRefs.TryGetValue(coverManifestItem.Href, out coverImageContentFileRef)) { return(coverImageContentFileRef); } } // we have still not found the item // 2019-08-20 Hotfix: if coverManifestItem is not found by its Id, then try it with its Href - some ebooks refer to the image directly! coverManifestItem = epubSchema.Package.Manifest.FirstOrDefault(manifestItem => manifestItem.Href.CompareOrdinalIgnoreCase(coverMetaItem.Content)); if (null != coverManifestItem?.Href && imageContentRefs.TryGetValue(coverManifestItem.Href, out coverImageContentFileRef)) { return(coverImageContentFileRef); } // 2019-08-24 if it is still not found, then try to find an Id named cover coverManifestItem = epubSchema.Package.Manifest.FirstOrDefault(manifestItem => manifestItem.Id.CompareOrdinalIgnoreCase(coverMetaItem.Name)); if (null != coverManifestItem?.Href && imageContentRefs.TryGetValue(coverManifestItem.Href, out coverImageContentFileRef)) { return(coverImageContentFileRef); } // 2019-08-24 if it is still not found, then try to find it in the guide var guideItem = epubSchema.Package.Guide.FirstOrDefault(reference => reference.Title.CompareOrdinalIgnoreCase(coverMetaItem.Name)); if (null != guideItem?.Href && imageContentRefs.TryGetValue(guideItem.Href, out coverImageContentFileRef)) { return(coverImageContentFileRef); } throw new Exception($"Incorrect EPUB manifest: item with ID = \"{coverMetaItem.Content}\" is missing or no corresponding image was found."); }