Esempio n. 1
0
        public EpubWriter(EpubBook book)
        {
            if (book == null)
            {
                throw new ArgumentNullException(nameof(book));
            }
            if (book.Format?.Opf == null)
            {
                throw new ArgumentException("book opf instance == null", nameof(book));
            }

            format    = book.Format;
            resources = book.Resources;

            opfPath = format.Ocf.RootFilePath;
            ncxPath = format.Opf.FindNcxPath();

            if (ncxPath != null)
            {
                // Remove NCX file from the resources - Write() will format a new one.
                resources.Other = resources.Other.Where(e => e.FileName != ncxPath).ToList();

                ncxPath = PathExt.Combine(PathExt.GetDirectoryPath(opfPath), ncxPath);
            }
        }
Esempio n. 2
0
        public EpubWriter()
        {
            var opf = new OpfDocument {
                EpubVersion = EpubVersion.Epub3
            };

            opf.Metadata.Dates.Add(new OpfMetadataDate {
                Text = DateTimeOffset.UtcNow.ToString("o")
            });
            opf.Manifest.Items.Add(new OpfManifestItem {
                Id = "ncx", Href = "toc.ncx", MediaType = ContentType.ContentTypeToMimeType[EpubContentType.DtbookNcx]
            });
            opf.Spine.Toc = "ncx";

            format = new EpubFormat
            {
                Opf = opf,
                Nav = new NavDocument(),
                Ncx = new NcxDocument()
            };

            format.Nav.Head.Dom = new XElement(NavElements.Head);
            format.Nav.Body.Dom =
                new XElement(
                    NavElements.Body,
                    new XElement(NavElements.Nav, new XAttribute(NavNav.Attributes.Type, NavNav.Attributes.TypeValues.Toc),
                                 new XElement(NavElements.Ol)));

            resources = new EpubResources();
        }
Esempio n. 3
0
        private static EpubResources LoadResources(ZipArchive epubArchive, EpubBook book)
        {
            var resources = new EpubResources();

            foreach (var item in book.Format.Opf.Manifest.Items)
            {
                var path  = PathExt.Combine(Path.GetDirectoryName(book.Format.Ocf.RootFilePath), item.Href);
                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 fileName = 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
                    {
                        FileName    = fileName,
                        MimeType    = mimeType,
                        ContentType = contentType
                    };

                    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
                    {
                        FileName    = fileName,
                        MimeType    = mimeType,
                        ContentType = contentType
                    };

                    using (var stream = entry.Open())
                    {
                        if (stream == null)
                        {
                            throw new EpubException($"Incorrect EPUB file: content file \"{fileName}\" 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:
                        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);
        }