EpubService.cs 3.8 KB

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