BulletinApplication.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.Configurations;
  3. using Hotline.Settings;
  4. using Microsoft.Extensions.Options;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using XF.Domain.Dependency;
  8. namespace Hotline.Application.Bulletin
  9. {
  10. public class BulletinApplication : IBulletinApplication, IScopeDependency
  11. {
  12. private readonly IOptionsSnapshot<AppConfiguration> _appOptions;
  13. private readonly ISystemSettingCacheManager _systemSettingCacheManager;
  14. public BulletinApplication(IOptionsSnapshot<AppConfiguration> appOptions,
  15. ISystemSettingCacheManager systemSettingCacheManager)
  16. {
  17. _appOptions = appOptions;
  18. _systemSettingCacheManager= systemSettingCacheManager;
  19. }
  20. /// <summary>
  21. /// 处理通知公告图片附件路径
  22. /// </summary>
  23. /// <param name="sHtmlText"></param>
  24. /// <returns></returns>
  25. public string GetSiteUrls(string sHtmlText)
  26. {
  27. sHtmlText = sHtmlText.Replace("&lt;", "<").Replace("&gt;", ">");
  28. //临时内容
  29. System.Text.StringBuilder sb = new StringBuilder();
  30. sb.Append(sHtmlText);
  31. // 定义正则表达式用来匹配 img 标签
  32. Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
  33. // 搜索匹配的字符串
  34. MatchCollection matches = regImg.Matches(sHtmlText);
  35. // 定义正则表达式用来匹配 video 标签
  36. Regex regvideo = new Regex(@"<video\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
  37. // 搜索匹配的字符串
  38. MatchCollection matchesvideo = regvideo.Matches(sHtmlText);
  39. var strSiteUrl = _systemSettingCacheManager.GetSetting(SettingConstants.OldFilesUrls)?.SettingValue[0];
  40. // 取得匹配项列表 视频
  41. foreach (Match match in matchesvideo)
  42. {
  43. if (-1 == match.Groups["imgUrl"].Value.IndexOf("http"))
  44. {
  45. sb.Replace(match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
  46. sb.Replace(strSiteUrl + strSiteUrl + match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
  47. }
  48. }
  49. // 取得匹配项列表
  50. foreach (Match match in matches)
  51. {
  52. if (-1 == match.Groups["imgUrl"].Value.IndexOf("http"))
  53. {
  54. sb.Replace(match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
  55. sb.Replace(strSiteUrl + strSiteUrl + match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
  56. }
  57. }
  58. return sb.ToString();
  59. }
  60. }
  61. }