EpubService.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. using GxPress.Repository.Interface;
  12. using GxPress.Repository.Interface.Order;
  13. namespace GxPress.Service.Implement.Epub
  14. {
  15. public class EpubService : IEpubService
  16. {
  17. private readonly IMediaRepository mediaRepository;
  18. private readonly IUserRepository userRepository;
  19. private readonly IOrderRepository orderRepository;
  20. public EpubService(IMediaRepository mediaRepository, IUserRepository userRepository, IOrderRepository orderRepository)
  21. {
  22. this.mediaRepository = mediaRepository;
  23. this.userRepository = userRepository;
  24. this.orderRepository = orderRepository;
  25. }
  26. /// <summary>
  27. /// 获取书籍目录
  28. /// </summary>
  29. public List<BookCatalogResult> GetCatalog(string path)
  30. {
  31. var result = new List<BookCatalogResult>();
  32. EpubBook epubBook = EpubReader.ReadBook(path);
  33. int i = 1;
  34. // 目录
  35. //列举章节
  36. foreach (EpubNavigationItem chapter in epubBook.Navigation)
  37. {
  38. //本章标题
  39. var bookCatalog = new BookCatalogResult();
  40. bookCatalog.Id = i;
  41. bookCatalog.CatalogNameg = chapter.Title;
  42. bookCatalog.CatalogId = chapter.HtmlContentFile.FileName;
  43. result.Add(bookCatalog);
  44. //嵌套章节
  45. List<EpubNavigationItem> subChapters = chapter.NestedItems;
  46. bookCatalog.IsChildren = chapter.NestedItems != null && subChapters.Count > 0;
  47. bookCatalog.Children = new List<BookCatalogResult>();
  48. foreach (var item in subChapters)
  49. {
  50. var bookCatalogModel = new BookCatalogResult();
  51. bookCatalogModel.Children = new List<BookCatalogResult>();
  52. i++;
  53. bookCatalogModel.Id = i;
  54. bookCatalogModel.CatalogNameg = item.Title;
  55. bookCatalogModel.CatalogId = item.HtmlContentFile.FileName;
  56. bookCatalog.Children.Add(bookCatalogModel);
  57. }
  58. i++;
  59. }
  60. return result;
  61. }
  62. /// <summary>
  63. /// 获取章节内容
  64. /// </summary>
  65. /// <returns></returns>
  66. public async Task<string> GetBookCatalogContent(BookCatalogRequest request)
  67. {
  68. if (string.IsNullOrEmpty(request.Path))
  69. return string.Empty;
  70. //获取media
  71. var media = await mediaRepository.GetAsync(request.MediaId);
  72. if (request.UserId > 0)
  73. {
  74. var user = await userRepository.GetAsync(request.UserId);
  75. if (user.IsVip && user.EndTime > DateTime.Now)
  76. media.FreeProportion = 0;
  77. else
  78. {
  79. if (await orderRepository.GetExistsAsync(request.UserId, request.MediaId))
  80. media.FreeProportion = 0;
  81. }
  82. }
  83. var sectionValue = 0;
  84. if (media.FreeProportion > 0)
  85. {
  86. var freeProportion = media.FreeProportion / 100;
  87. var catalogs = GetCatalog(request.Path);
  88. sectionValue = Convert.ToInt32(catalogs.Count * freeProportion);
  89. }
  90. EpubBook epubBook = EpubReader.ReadBook(request.Path);
  91. string htmlContent = string.Empty;
  92. int i = 1;
  93. foreach (EpubNavigationItem chapter in epubBook.Navigation)
  94. {
  95. if (chapter.HtmlContentFile.FileName.Equals(request.CatalogId) || chapter.Title.Equals(request.CatalogNameg))
  96. {
  97. if (i > sectionValue && sectionValue > 0)
  98. {
  99. htmlContent = HtmlAgilityPackHelper.GetHmtlContent(chapter.HtmlContentFile.Content);
  100. break;
  101. }
  102. //当前文本内容文件的HTML
  103. htmlContent = HtmlAgilityPackHelper.GetHmtl(chapter.HtmlContentFile.Content);
  104. break;
  105. }
  106. foreach (var item in chapter.NestedItems)
  107. {
  108. if (item.HtmlContentFile.FileName.Equals(request.CatalogId) || item.Title.Equals(request.CatalogNameg))
  109. {
  110. if (i > sectionValue && sectionValue > 0)
  111. {
  112. htmlContent = HtmlAgilityPackHelper.GetHmtlContent(item.HtmlContentFile.Content);
  113. break;
  114. }
  115. //当前文本内容文件的HTML
  116. htmlContent = HtmlAgilityPackHelper.GetHmtl(item.HtmlContentFile.Content);
  117. break;
  118. }
  119. i++;
  120. }
  121. i++;
  122. }
  123. return htmlContent;
  124. }
  125. /// <summary>
  126. /// C#去除HTML标签
  127. /// </summary>
  128. /// <param name="html">带有html标签的文本</param>
  129. /// <param name="length">截取长度</param>
  130. /// <returns></returns>
  131. public string ReplaceHtmlTag(string html, int length = 0)
  132. {
  133. string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
  134. strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");
  135. if (length > 0 && strText.Length > length)
  136. return strText.Substring(0, length);
  137. return strText;
  138. }
  139. }
  140. }