123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System.Collections.Generic;
- using GxPress.Request.Media;
- using GxPress.Result.Category;
- using GxPress.Result.Media;
- using GxPress.Service.Interface.Epub;
- using GxPress.Repository.Interface.Media;
- using VersOne.Epub;
- using System;
- using System.Threading.Tasks;
- namespace GxPress.Service.Implement.Epub
- {
- public class EpubService : IEpubService
- {
- private readonly IMediaRepository mediaRepository;
- public EpubService(IMediaRepository mediaRepository)
- {
- this.mediaRepository = mediaRepository;
- }
- /// <summary>
- /// 获取书籍目录
- /// </summary>
- public List<BookCatalogResult> GetCatalog(string path)
- {
- var result = new List<BookCatalogResult>();
- EpubBook epubBook = EpubReader.ReadBook(path);
- int i = 1;
- // 目录
- //列举章节
- foreach (EpubNavigationItem chapter in epubBook.Navigation)
- {
- //本章标题
- var bookCatalog = new BookCatalogResult();
- bookCatalog.Id = i;
- bookCatalog.CatalogNameg = chapter.Title;
- bookCatalog.CatalogId = chapter.HtmlContentFile.FileName;
- result.Add(bookCatalog);
- //嵌套章节
- List<EpubNavigationItem> subChapters = chapter.NestedItems;
- foreach (var item in subChapters)
- {
- bookCatalog = new BookCatalogResult();
- bookCatalog.CatalogNameg = item.Title;
- bookCatalog.CatalogId = item.HtmlContentFile.FileName;
- bookCatalog.ParentId = i;
- result.Add(bookCatalog);
- }
- i++;
- }
- return result;
- }
- /// <summary>
- /// 获取章节内容
- /// </summary>
- /// <returns></returns>
- public async Task<string> GetBookCatalogContent(BookCatalogRequest request)
- {
- if (string.IsNullOrEmpty(request.Path))
- return string.Empty;
- //获取media
- var media = await mediaRepository.GetAsync(request.MediaId);
- var sectionValue = 0;
- if (media.FreeProportion > 0)
- {
- var freeProportion = media.FreeProportion / 100;
- var catalogs = GetCatalog(request.Path);
- sectionValue = Convert.ToInt32(catalogs.Count * freeProportion);
- }
- EpubBook epubBook = EpubReader.ReadBook(request.Path);
- string htmlContent = string.Empty;
- int i = 0;
- foreach (EpubTextContentFile textContentFile in epubBook.ReadingOrder)
- {
- if (i >= sectionValue && sectionValue > 0)
- return "";
- if (textContentFile.FileName.Equals(request.CatalogId) || textContentFile.FileName.Equals(request.CatalogNameg))
- {
- //当前文本内容文件的HTML
- htmlContent = textContentFile.Content;
- break;
- }
- i++;
- }
- return htmlContent;
- }
- /// <summary>
- /// C#去除HTML标签
- /// </summary>
- /// <param name="html">带有html标签的文本</param>
- /// <param name="length">截取长度</param>
- /// <returns></returns>
- public string ReplaceHtmlTag(string html, int length = 0)
- {
- string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
- strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");
- if (length > 0 && strText.Length > length)
- return strText.Substring(0, length);
- return strText;
- }
- }
- }
|