/// <summary> /// 获取短网址 /// </summary> ///<param name="alias">短网址别名</param> ///<param name="entity">要输出的实体</param> ///<param name="urlDomain">自定义短网址域名</param> public string GetShortUrl(string alias, out ShortUrlEntity entity, string urlDomain = "") { IShortUrlSettingsManager settingsManager = DIContainer.Resolve <IShortUrlSettingsManager>(); ShortUrlSettings settings = settingsManager.Get(); urlDomain = !string.IsNullOrEmpty(urlDomain) ? urlDomain : GetUrlDomain(); entity = null; string shortUrl = string.Empty; if (!string.IsNullOrEmpty(alias)) { entity = Get(alias); if (entity != null && !string.IsNullOrEmpty(entity.Alias)) { if (!settings.IsEnableOtherShortner || string.IsNullOrEmpty(entity.OtherShortUrl)) { shortUrl = urlDomain + "/" + alias; } else { shortUrl = entity.OtherShortUrl; } } } return(shortUrl); }
/// <summary> /// 解析内容中的多媒体内容 /// </summary> /// <param name="body">待解析的内容</param> /// <param name="TagGenerate">url生成对应标签的方法</param> public string ResolveBodyForHtmlDetail(string body, Func <string, ParsedMedia, string> TagGenerate) { if (string.IsNullOrEmpty(body)) { return(body); } Regex rg = new Regex(@"(\<img(.[^\<]*)?(alt=""(?<alias>[a-zA-Z0-9]*)"")(.[^\<]*)?\>)", RegexOptions.Multiline | RegexOptions.Singleline); MatchCollection matches = rg.Matches(body); if (matches != null) { string alias = string.Empty, preAlias = string.Empty, imgHtml = string.Empty, shortUrl = string.Empty; ParsedMedia parsedMedia = null; ShortUrlService shortUrlService = new ShortUrlService(); string urlDomain = ShortUrlService.GetUrlDomain(); foreach (Match m in matches) { if (m.Groups["alias"] == null || string.IsNullOrEmpty(m.Groups["alias"].Value)) { continue; } alias = m.Groups["alias"].Value; if (!string.IsNullOrEmpty(alias) && !alias.Equals(preAlias, StringComparison.CurrentCultureIgnoreCase)) { preAlias = alias; } else { continue; } imgHtml = m.Value; if (alias.Length == 6) { ShortUrlEntity entity = null; shortUrl = shortUrlService.GetShortUrl(alias, out entity, urlDomain); if (entity != null) { parsedMedia = Get(alias); if (parsedMedia == null) { continue; } } else { continue; } } body = body.Replace(imgHtml, TagGenerate(shortUrl, parsedMedia)); } } return(body); }
/// <summary> /// 更新短网址 /// </summary> /// <param name="entity">需要更新的短网址信息</param> public void Update(ShortUrlEntity entity) { EventBus <ShortUrlEntity> .Instance().OnBefore(entity, new CommonEventArgs(EventOperationType.Instance().Update())); shortUrlRepository.Update(entity); EventBus <ShortUrlEntity> .Instance().OnAfter(entity, new CommonEventArgs(EventOperationType.Instance().Update())); }
/// <summary> /// 创建示例 /// </summary> /// <param name="alias">Url别名</param> /// <returns></returns> public static ShortUrlEntity New(string alias = "") { ShortUrlEntity shortUrl = new ShortUrlEntity() { Alias = alias, Url = string.Empty, OtherShortUrl = string.Empty, DateCreated = DateTime.UtcNow }; return(shortUrl); }
/// <summary> /// 删除短网址 /// </summary> /// <param name="alias">短网址别名</param> public void Delete(string alias) { if (string.IsNullOrEmpty(alias)) { return; } ShortUrlEntity entity = shortUrlRepository.Get(alias); if (entity != null) { EventBus <ShortUrlEntity> .Instance().OnBefore(entity, new CommonEventArgs(EventOperationType.Instance().Delete())); shortUrlRepository.Delete(entity); EventBus <ShortUrlEntity> .Instance().OnAfter(entity, new CommonEventArgs(EventOperationType.Instance().Delete())); } }
/// <summary> /// 创建短网址 /// </summary> /// <param name="url">需要处理的Url</param> /// <returns>Url别名</returns> public string Create(string url) { bool urlExists; IUrlShortner urlShortner = null; ShortUrlSettings settings = shortUrlSettingsManager.Get(); if (settings.IsEnableOtherShortner) { urlShortner = DIContainer.Resolve <IUrlShortner>(); } ShortUrlEntity entity = ShortUrlEntity.New(GetUrlAlias(url, out urlExists)); entity.Url = url; //判断Url是否存在 if (urlExists) { return(entity.Alias); } string primaryKey = string.Empty; EventBus <ShortUrlEntity> .Instance().OnBefore(entity, new CommonEventArgs(EventOperationType.Instance().Create())); if (settings.IsEnableOtherShortner && urlShortner != null) { entity.OtherShortUrl = urlShortner.Shortner(url); } primaryKey = shortUrlRepository.Insert(entity).ToString(); EventBus <ShortUrlEntity> .Instance().OnAfter(entity, new CommonEventArgs(EventOperationType.Instance().Create())); return(primaryKey); }
/// <summary> /// 创建示例 /// </summary> /// <param name="alias">Url别名</param> /// <returns></returns> public static ShortUrlEntity New(string alias = "") { ShortUrlEntity shortUrl = new ShortUrlEntity() { Alias = alias, Url = string.Empty, OtherShortUrl = string.Empty, DateCreated = DateTime.UtcNow }; return shortUrl; }
/// <summary> /// 解析内容中的AtUser用户展示展示 /// </summary> /// <param name="body">待解析的内容</param> /// <param name="associateId">关联项Id</param> /// <param name="userId">关联项作者Id</param> /// <param name="UrlGenerate">url生成对应标签的方法</param> public string ResolveBodyForDetail(string body, long associateId, long userId, Func <string, long, long, ParsedMedia, string> UrlGenerate) { if (string.IsNullOrEmpty(body) || !body.Contains("http://")) { return(body); } Regex rg = new Regex(regexRule, RegexOptions.Multiline | RegexOptions.Singleline); MatchCollection matches = rg.Matches(body); if (matches != null) { string strUrl = string.Empty, preUrl = string.Empty, shortUrl = string.Empty; string urlDomain = ShortUrlService.GetUrlDomain().Substring(7); ParsedMedia parsedMedia = null; ShortUrlService shortUrlService = new ShortUrlService(); foreach (Match m in matches) { if (string.IsNullOrEmpty(m.Value)) { continue; } strUrl = m.Value; if (string.IsNullOrEmpty(strUrl) || strUrl.Equals(preUrl, StringComparison.CurrentCultureIgnoreCase)) { continue; } preUrl = strUrl; if (strUrl.StartsWith(urlDomain)) { string temalias = strUrl.Substring(strUrl.LastIndexOf('/') + 1); if (temalias.Length != 6) { continue; } parsedMedia = Get(temalias); if (parsedMedia == null) { continue; } shortUrl = shortUrlService.GetShortUrl(temalias, "http://" + urlDomain); } else if (strUrl.Length == 6) { ShortUrlEntity entity = null; shortUrl = shortUrlService.GetShortUrl(strUrl, out entity, "http://" + urlDomain); if (entity == null) { continue; } parsedMedia = Get(strUrl); if (parsedMedia == null) { parsedMedia = ParsedMedia.New(); parsedMedia.MediaType = MediaType.Other; parsedMedia.Alias = entity.Alias; parsedMedia.Url = entity.Url; } } else { continue; } body = body.Replace("http://" + strUrl, UrlGenerate(shortUrl, associateId, userId, parsedMedia)); } } return(body); }
/// <summary> /// 获取短网址 /// </summary> ///<param name="alias">短网址别名</param> ///<param name="urlDomain">自定义短网址域名</param> public string GetShortUrl(string alias, string urlDomain = "") { ShortUrlEntity entity = null; return(GetShortUrl(alias, out entity, urlDomain)); }
/// <summary> /// 更新短网址 /// </summary> /// <param name="entity">需要更新的短网址信息</param> public void Update(ShortUrlEntity entity) { EventBus<ShortUrlEntity>.Instance().OnBefore(entity, new CommonEventArgs(EventOperationType.Instance().Update())); shortUrlRepository.Update(entity); EventBus<ShortUrlEntity>.Instance().OnAfter(entity, new CommonEventArgs(EventOperationType.Instance().Update())); }
/// <summary> /// 获取短网址 /// </summary> ///<param name="alias">短网址别名</param> ///<param name="entity">要输出的实体</param> ///<param name="urlDomain">自定义短网址域名</param> public string GetShortUrl(string alias, out ShortUrlEntity entity, string urlDomain = "") { ShortUrlSettings settings = shortUrlSettingsManager.Get(); urlDomain = !string.IsNullOrEmpty(urlDomain) ? urlDomain : GetUrlDomain(); entity = null; string shortUrl = string.Empty; if (!string.IsNullOrEmpty(alias)) { entity = Get(alias); if (entity != null && !string.IsNullOrEmpty(entity.Alias)) { if (!settings.IsEnableOtherShortner || string.IsNullOrEmpty(entity.OtherShortUrl)) { shortUrl = urlDomain + "/" + alias; } else { shortUrl = entity.OtherShortUrl; } } } return shortUrl; }