Esempio n. 1
0
        public async Task SetImagesAsync(ICollection <EpubByteFile> images)
        {
            // Is called when displaying a book; is a perfect time to make the activity
            if (!ProjectRomeEnabled)
            {
                return;
            }

            EpubByteFile bestImage    = null;
            string       imageDataUrl = null;

            foreach (var file in images)
            {
                if (bestImage == null)
                {
                    bestImage = file;
                }
            }
            if (bestImage != null)
            {
                var b64 = Convert.ToBase64String(bestImage.Content);
                imageDataUrl = $"data:{bestImage.MimeType};base64,{b64}"; // image/png
            }
            await CreateActivityAsync(CurrBook, CurrLocation, imageDataUrl);
        }
Esempio n. 2
0
        public static string FileName(this EpubByteFile epub)
        {
            var str       = epub.AbsolutePath;
            var lastIndex = str.LastIndexOf('/');

            if (lastIndex < 0)
            {
                return(str);
            }
            var retval = str.Substring(lastIndex + 1);

            return(retval);
        }
Esempio n. 3
0
        private EpubResources LoadResources(EpubFormat format)
        {
            var resources = new EpubResources();

            foreach (var item in format.Opf.Manifest.Items)
            {
                var      path  = item.Href.ToAbsolutePath(format.Paths.OpfAbsolutePath);
                FileInfo entry = _bookFolder.GetFile("." + 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.OpenRead())
                    {
                        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.OpenRead())
                    {
                        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:
                        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);
        }