EpubService.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. foreach (var item in subChapters)
  47. {
  48. i++;
  49. bookCatalog = new BookCatalogResult();
  50. bookCatalog.Id = i;
  51. bookCatalog.CatalogNameg = item.Title;
  52. bookCatalog.CatalogId = item.HtmlContentFile.FileName;
  53. bookCatalog.ParentId = i;
  54. result.Add(bookCatalog);
  55. }
  56. i++;
  57. }
  58. return result;
  59. }
  60. /// <summary>
  61. /// 获取章节内容
  62. /// </summary>
  63. /// <returns></returns>
  64. public async Task<string> GetBookCatalogContent(BookCatalogRequest request)
  65. {
  66. if (string.IsNullOrEmpty(request.Path))
  67. return string.Empty;
  68. //获取media
  69. var media = await mediaRepository.GetAsync(request.MediaId);
  70. if (request.UserId > 0)
  71. {
  72. var user = await userRepository.GetAsync(request.UserId);
  73. if (user.IsVip && user.EndTime > DateTime.Now)
  74. media.FreeProportion = 0;
  75. else
  76. {
  77. if (await orderRepository.GetExistsAsync(request.UserId, request.MediaId))
  78. media.FreeProportion = 0;
  79. }
  80. }
  81. var sectionValue = 0;
  82. if (media.FreeProportion > 0)
  83. {
  84. var freeProportion = media.FreeProportion / 100;
  85. var catalogs = GetCatalog(request.Path);
  86. sectionValue = Convert.ToInt32(catalogs.Count * freeProportion);
  87. }
  88. EpubBook epubBook = EpubReader.ReadBook(request.Path);
  89. string htmlContent = string.Empty;
  90. int i = 1;
  91. foreach (EpubNavigationItem chapter in epubBook.Navigation)
  92. {
  93. if (chapter.HtmlContentFile.FileName.Equals(request.CatalogId) || chapter.Title.Equals(request.CatalogNameg))
  94. {
  95. if (i > sectionValue && sectionValue > 0)
  96. {
  97. htmlContent = HtmlAgilityPackHelper.GetHmtlContent(chapter.HtmlContentFile.Content);
  98. break;
  99. }
  100. //当前文本内容文件的HTML
  101. htmlContent = HtmlAgilityPackHelper.GetHmtl(chapter.HtmlContentFile.Content);
  102. break;
  103. }
  104. foreach (var item in chapter.NestedItems)
  105. {
  106. if (item.HtmlContentFile.FileName.Equals(request.CatalogId) || item.Title.Equals(request.CatalogNameg))
  107. {
  108. if (i > sectionValue && sectionValue > 0)
  109. {
  110. htmlContent = HtmlAgilityPackHelper.GetHmtlContent(item.HtmlContentFile.Content);
  111. break;
  112. }
  113. //当前文本内容文件的HTML
  114. htmlContent = HtmlAgilityPackHelper.GetHmtl(item.HtmlContentFile.Content);
  115. break;
  116. }
  117. i++;
  118. }
  119. i++;
  120. }
  121. return htmlContent;
  122. }
  123. /// <summary>
  124. /// C#去除HTML标签
  125. /// </summary>
  126. /// <param name="html">带有html标签的文本</param>
  127. /// <param name="length">截取长度</param>
  128. /// <returns></returns>
  129. public string ReplaceHtmlTag(string html, int length = 0)
  130. {
  131. string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
  132. strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");
  133. if (length > 0 && strText.Length > length)
  134. return strText.Substring(0, length);
  135. return strText;
  136. }
  137. }
  138. }