Beispiel #1
0
        public EpubChapter AddChapter(string title, string html, string fileId = null)
        {
            if (string.IsNullOrWhiteSpace(title))
            {
                throw new ArgumentNullException(nameof(title));
            }
            if (html == null)
            {
                throw new ArgumentNullException(nameof(html));
            }

            fileId = fileId ?? Guid.NewGuid().ToString("N");
            var file = new EpubTextFile
            {
                AbsolutePath = fileId + ".html",
                Href         = fileId + ".html",
                TextContent  = html,
                ContentType  = EpubContentType.Xhtml11
            };

            file.MimeType = ContentType.ContentTypeToMimeType[file.ContentType];
            resources.Html.Add(file);

            var manifestItem = new OpfManifestItem
            {
                Id        = fileId,
                Href      = file.Href,
                MediaType = file.MimeType
            };

            format.Opf.Manifest.Items.Add(manifestItem);

            var spineItem = new OpfSpineItemRef {
                IdRef = manifestItem.Id, Linear = true
            };

            format.Opf.Spine.ItemRefs.Add(spineItem);

            FindNavTocOl()?.Add(new XElement(NavElements.Li, new XElement(NavElements.A, new XAttribute("href", file.Href), title)));

            format.Ncx?.NavMap.NavPoints.Add(new NcxNavPoint
            {
                Id           = Guid.NewGuid().ToString("N"),
                NavLabelText = title,
                ContentSrc   = file.Href,
                PlayOrder    = format.Ncx.NavMap.NavPoints.Any() ? format.Ncx.NavMap.NavPoints.Max(e => e.PlayOrder) : 1
            });

            return(new EpubChapter
            {
                Id = fileId,
                Title = title,
                RelativePath = file.AbsolutePath
            });
        }
        private static EpubResources LoadResources(ZipArchive epubArchive, EpubBook book)
        {
            var resources = new EpubResources();

            foreach (var item in book.Format.Opf.Manifest.Items)
            {
                var path  = item.Href.ToAbsolutePath(book.Format.Paths.OpfAbsolutePath);
                var entry = epubArchive.GetEntryImproved(path);

                if (entry == null)
                {
                    throw new EpubParseException($"file {path} not found in archive.");
                }
                if (entry.Length > int.MaxValue)
                {
                    throw new EpubParseException($"file {path} is bigger than 2 Gb.");
                }

                var href     = item.Href;
                var mimeType = item.MediaType;

                EpubContentType contentType;
                contentType = ContentType.MimeTypeToContentType.TryGetValue(mimeType, out contentType)
                    ? contentType
                    : EpubContentType.Other;

                switch (contentType)
                {
                case EpubContentType.Xhtml11:
                case EpubContentType.Css:
                case EpubContentType.Oeb1Document:
                case EpubContentType.Oeb1Css:
                case EpubContentType.Xml:
                case EpubContentType.Dtbook:
                case EpubContentType.DtbookNcx:
                {
                    var file = new EpubTextFile
                    {
                        AbsolutePath = path,
                        Href         = href,
                        MimeType     = mimeType,
                        ContentType  = contentType
                    };

                    resources.All.Add(file);

                    using (var stream = entry.Open())
                    {
                        file.Content = stream.ReadToEnd();
                    }

                    switch (contentType)
                    {
                    case EpubContentType.Xhtml11:
                        resources.Html.Add(file);
                        break;

                    case EpubContentType.Css:
                        resources.Css.Add(file);
                        break;

                    default:
                        resources.Other.Add(file);
                        break;
                    }
                    break;
                }

                default:
                {
                    var file = new EpubByteFile
                    {
                        AbsolutePath = path,
                        Href         = href,
                        MimeType     = mimeType,
                        ContentType  = contentType
                    };

                    resources.All.Add(file);

                    using (var stream = entry.Open())
                    {
                        if (stream == null)
                        {
                            throw new EpubException($"Incorrect EPUB file: content file \"{href}\" specified in manifest is not found");
                        }

                        using (var memoryStream = new MemoryStream((int)entry.Length))
                        {
                            stream.CopyTo(memoryStream);
                            file.Content = memoryStream.ToArray();
                        }
                    }

                    switch (contentType)
                    {
                    case EpubContentType.ImageGif:
                    case EpubContentType.ImageJpeg:
                    case EpubContentType.ImagePng:
                    case EpubContentType.ImageSvg:
                    case EpubContentType.ImageBmp:
                        resources.Images.Add(file);
                        break;

                    case EpubContentType.FontTruetype:
                    case EpubContentType.FontOpentype:
                        resources.Fonts.Add(file);
                        break;

                    default:
                        resources.Other.Add(file);
                        break;
                    }
                    break;
                }
                }
            }

            return(resources);
        }