EpubService.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 GxPress.Repository.Interface.Media;
  7. using VersOne.Epub;
  8. using System;
  9. using System.Threading.Tasks;
  10. using GxPress.Common.Tools;
  11. namespace GxPress.Service.Implement.Epub
  12. {
  13. public class EpubService : IEpubService
  14. {
  15. private readonly IMediaRepository mediaRepository;
  16. public EpubService(IMediaRepository mediaRepository)
  17. {
  18. this.mediaRepository = mediaRepository;
  19. }
  20. /// <summary>
  21. /// 获取书籍目录
  22. /// </summary>
  23. public List<BookCatalogResult> GetCatalog(string path)
  24. {
  25. var result = new List<BookCatalogResult>();
  26. EpubBook epubBook = EpubReader.ReadBook(path);
  27. int i = 1;
  28. // 目录
  29. //列举章节
  30. foreach (EpubNavigationItem chapter in epubBook.Navigation)
  31. {
  32. //本章标题
  33. var bookCatalog = new BookCatalogResult();
  34. bookCatalog.Id = i;
  35. bookCatalog.CatalogNameg = chapter.Title;
  36. bookCatalog.CatalogId = chapter.HtmlContentFile.FileName;
  37. result.Add(bookCatalog);
  38. //嵌套章节
  39. List<EpubNavigationItem> subChapters = chapter.NestedItems;
  40. foreach (var item in subChapters)
  41. {
  42. bookCatalog = new BookCatalogResult();
  43. bookCatalog.CatalogNameg = item.Title;
  44. bookCatalog.CatalogId = item.HtmlContentFile.FileName;
  45. bookCatalog.ParentId = i;
  46. result.Add(bookCatalog);
  47. }
  48. i++;
  49. }
  50. return result;
  51. }
  52. /// <summary>
  53. /// 获取章节内容
  54. /// </summary>
  55. /// <returns></returns>
  56. public async Task<string> GetBookCatalogContent(BookCatalogRequest request)
  57. {
  58. if (string.IsNullOrEmpty(request.Path))
  59. return string.Empty;
  60. //获取media
  61. var media = await mediaRepository.GetAsync(request.MediaId);
  62. var sectionValue = 0;
  63. if (media.FreeProportion > 0)
  64. {
  65. var freeProportion = media.FreeProportion / 100;
  66. var catalogs = GetCatalog(request.Path);
  67. sectionValue = Convert.ToInt32(catalogs.Count * freeProportion);
  68. }
  69. EpubBook epubBook = EpubReader.ReadBook(request.Path);
  70. string htmlContent = string.Empty;
  71. int i = 0;
  72. foreach (EpubTextContentFile textContentFile in epubBook.ReadingOrder)
  73. {
  74. if (i >= sectionValue && sectionValue > 0)
  75. {
  76. htmlContent = HtmlAgilityPackHelper.GetHmtlContent(textContentFile.Content);
  77. break;
  78. }
  79. if (textContentFile.FileName.Equals(request.CatalogId) || textContentFile.FileName.Equals(request.CatalogNameg))
  80. {
  81. //当前文本内容文件的HTML
  82. htmlContent = HtmlAgilityPackHelper.GetHmtl(textContentFile.Content);
  83. break;
  84. }
  85. i++;
  86. }
  87. return htmlContent;
  88. }
  89. /// <summary>
  90. /// C#去除HTML标签
  91. /// </summary>
  92. /// <param name="html">带有html标签的文本</param>
  93. /// <param name="length">截取长度</param>
  94. /// <returns></returns>
  95. public string ReplaceHtmlTag(string html, int length = 0)
  96. {
  97. string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
  98. strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");
  99. if (length > 0 && strText.Length > length)
  100. return strText.Substring(0, length);
  101. return strText;
  102. }
  103. }
  104. }