EpubService.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections.Generic;
  2. using GxPress.Request.Media;
  3. using GxPress.Result.Category;
  4. using GxPress.Result.Media;
  5. using GxPress.Service.Interface.Epub;
  6. using VersOne.Epub;
  7. namespace GxPress.Service.Implement.Epub
  8. {
  9. public class EpubService : IEpubService
  10. {
  11. /// <summary>
  12. /// 获取书籍目录
  13. /// </summary>
  14. public List<BookCatalogResult> GetCatalog(string path)
  15. {
  16. var result = new List<BookCatalogResult>();
  17. EpubBook epubBook = EpubReader.ReadBook(path);
  18. int i = 1;
  19. // 目录
  20. //列举章节
  21. foreach (EpubNavigationItem chapter in epubBook.Navigation)
  22. {
  23. //本章标题
  24. var bookCatalog = new BookCatalogResult();
  25. bookCatalog.Id = i;
  26. bookCatalog.CatalogNameg = chapter.Title;
  27. bookCatalog.CatalogId = chapter.HtmlContentFile.FileName;
  28. result.Add(bookCatalog);
  29. //嵌套章节
  30. List<EpubNavigationItem> subChapters = chapter.NestedItems;
  31. foreach (var item in subChapters)
  32. {
  33. bookCatalog = new BookCatalogResult();
  34. bookCatalog.CatalogNameg = item.Title;
  35. bookCatalog.CatalogId = item.HtmlContentFile.FileName;
  36. bookCatalog.ParentId = i;
  37. result.Add(bookCatalog);
  38. }
  39. i++;
  40. }
  41. return result;
  42. }
  43. /// <summary>
  44. /// 获取章节内容
  45. /// </summary>
  46. /// <returns></returns>
  47. public string GetBookCatalogContext(BookCatalogRequest request)
  48. {
  49. if (string.IsNullOrEmpty(request.Path))
  50. return string.Empty;
  51. EpubBook epubBook = EpubReader.ReadBook(request.Path);
  52. string htmlContent = string.Empty;
  53. foreach (EpubTextContentFile textContentFile in epubBook.ReadingOrder)
  54. {
  55. if (textContentFile.FileName.Equals(request.CatalogId) || textContentFile.FileName.Equals(request.CatalogNameg))
  56. {
  57. //当前文本内容文件的HTML
  58. htmlContent = textContentFile.Content;
  59. break;
  60. }
  61. }
  62. return htmlContent;
  63. }
  64. }
  65. }