Exemple #1
0
        public void SetCover(byte[] data, ImageFormat imageFormat)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            RemoveCover();

            string          filename;
            EpubContentType type;

            switch (imageFormat)
            {
            case ImageFormat.Gif:
                filename = "cover.gif";
                type     = EpubContentType.ImageGif;
                break;

            case ImageFormat.Jpeg:
                filename = "cover.jpeg";
                type     = EpubContentType.ImageJpeg;
                break;

            case ImageFormat.Png:
                filename = "cover.png";
                type     = EpubContentType.ImagePng;
                break;

            case ImageFormat.Svg:
                filename = "cover.svg";
                type     = EpubContentType.ImageSvg;
                break;

            default:
                throw new ArgumentException($"Unsupported cover format: {format}", nameof(format));
            }

            var coverResource = new EpubByteFile
            {
                AbsolutePath = filename,
                Href         = filename,
                ContentType  = type,
                Content      = data
            };

            coverResource.MimeType = ContentType.ContentTypeToMimeType[coverResource.ContentType];
            resources.Images.Add(coverResource);

            var coverItem = new OpfManifestItem
            {
                Id        = OpfManifest.ManifestItemCoverImageProperty,
                Href      = coverResource.Href,
                MediaType = coverResource.MimeType
            };

            coverItem.Properties.Add(OpfManifest.ManifestItemCoverImageProperty);
            format.Opf.Manifest.Items.Add(coverItem);
        }
Exemple #2
0
        public void AddFile(string filename, byte[] content, EpubContentType type)
        {
            if (string.IsNullOrWhiteSpace(filename))
            {
                throw new ArgumentNullException(nameof(filename));
            }
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            var file = new EpubByteFile
            {
                AbsolutePath = filename,
                Href         = filename,
                ContentType  = type,
                Content      = content
            };

            file.MimeType = ContentType.ContentTypeToMimeType[file.ContentType];

            switch (type)
            {
            case EpubContentType.Css:
                resources.Css.Add(file.ToTextFile());
                break;

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

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

            case EpubContentType.Xml:
            case EpubContentType.Xhtml11:
            case EpubContentType.Other:
                resources.Other.Add(file);
                break;

            default:
                throw new InvalidOperationException($"Unsupported file type: {type}");
            }

            format.Opf.Manifest.Items.Add(new OpfManifestItem
            {
                Id        = Guid.NewGuid().ToString("N"),
                Href      = filename,
                MediaType = file.MimeType
            });
        }
Exemple #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);
        }