Exemplo n.º 1
0
        /// <summary>
        /// 创建多媒体网址
        /// </summary>
        /// <param name="ParsedMedia">多媒体网址实体</param>
        /// <returns>Url别名</returns>
        public string Create(ParsedMedia ParsedMedia)
        {
            ShortUrlService shortUrlService = new ShortUrlService();
            string          alias           = shortUrlService.Create(ParsedMedia.Url);

            ParsedMedia.Alias = alias;

            EventBus <ParsedMedia> .Instance().OnBefore(ParsedMedia, new CommonEventArgs(EventOperationType.Instance().Create()));

            string description = HtmlUtility.StripHtml(ParsedMedia.Description, true, false).Trim();

            if (description.Length > 500)
            {
                ParsedMedia.Description = description.Substring(0, 500) + "…";
            }
            else
            {
                ParsedMedia.Description = description;
            }
            object result = ParsedMediaRepository.Insert(ParsedMedia);

            if ((bool)result)
            {
                EventBus <ParsedMedia> .Instance().OnAfter(ParsedMedia, new CommonEventArgs(EventOperationType.Instance().Create()));
            }
            return(alias);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 解析内容用于创建AtUser
        /// </summary>
        /// <param name="body">待解析的内容</param>
        /// <param name="videoAlias">视频Url别名</param>
        /// <param name="audioAlias">音乐Url别名</param>
        public string ResolveBodyForEdit(string body, out string videoAlias, out string audioAlias)
        {
            videoAlias = audioAlias = string.Empty;
            if (string.IsNullOrEmpty(body) || !body.Contains("http://"))
            {
                return(body);
            }

            string newBody = body, strUrl = string.Empty, strPreUrl = string.Empty;
            string urlDomain = ShortUrlService.GetUrlDomain();

            urlDomain = urlDomain.Substring(7);

            Dictionary <string, string> urls            = new Dictionary <string, string>();
            ShortUrlService             shortUrlService = new ShortUrlService();

            ParsedMedia     parsedMedia = null;
            Regex           rg          = new Regex(regexRule, RegexOptions.Multiline | RegexOptions.Singleline);
            MatchCollection matches     = rg.Matches(body);

            if (matches != null)
            {
                foreach (Match m in matches)
                {
                    if (string.IsNullOrEmpty(m.Value))
                    {
                        continue;
                    }

                    strUrl = m.Value;

                    if (!string.IsNullOrEmpty(strUrl) && !strUrl.Equals(strPreUrl, StringComparison.CurrentCultureIgnoreCase))
                    {
                        strPreUrl = strUrl;
                    }
                    else
                    {
                        continue;
                    }

                    if (strUrl.StartsWith(urlDomain) || strUrl.Length == 6)
                    {
                        string temalias = strUrl.Length == 6 ? strUrl : strUrl.Substring(urlDomain.Length + 1);
                        if (temalias.Length == 6)
                        {
                            parsedMedia = Get(temalias);
                            if (strUrl.Contains(urlDomain))
                            {
                                urls[temalias] = strUrl;
                            }
                        }
                        else
                        {
                            temalias = shortUrlService.Create("http://" + strUrl);
                            if (string.IsNullOrEmpty(temalias))
                            {
                                continue;
                            }

                            urls[temalias] = strUrl;
                        }
                    }

                    if (parsedMedia == null)
                    {
                        continue;
                    }

                    if (parsedMedia.MediaType == MediaType.Video)
                    {
                        if (string.IsNullOrEmpty(videoAlias))
                        {
                            videoAlias = parsedMedia.Alias;
                        }
                    }
                    else if (parsedMedia.MediaType == MediaType.Audio)
                    {
                        if (string.IsNullOrEmpty(audioAlias))
                        {
                            audioAlias = parsedMedia.Alias;
                        }
                    }
                }

                if (urls.Count() > 0)
                {
                    foreach (var url in urls)
                    {
                        body = body.Replace("http://" + url.Value, "http://" + url.Key + " ");
                    }
                }
            }

            return(body);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 生成多媒体内容标签
        /// </summary>
        /// <param name="shrotUrl">短网址</param>
        /// <param name="parsedMedia">多媒体连接实体</param>
        private string ParsedMediaTagGenerateForMobile(string shrotUrl, ParsedMedia parsedMedia)
        {
            if (parsedMedia == null)
                return string.Empty;

            if (parsedMedia.MediaType == MediaType.Audio)
            {
                string musicHtml = "<p><a href=\"{1}\" ntype='mediaPlay'>点击播放音乐</a></p>";
                ShortUrlEntity shortUrlEntity = new ShortUrlService().Get(parsedMedia.Alias);
                return string.Format(musicHtml, SiteUrls.FullUrl(SiteUrls.Instance()._MusicDetail(parsedMedia.Alias)), shortUrlEntity == null ? null : shortUrlEntity.Url);
            }
            else if (parsedMedia.MediaType == MediaType.Video)
            {
                string videoHtml = "<p><a  href=\"{1}\" ntype=\"mediaPlay\">{1}<img src=\"{2}\"></a></p>";
                ShortUrlEntity shortUrlEntity = new ShortUrlService().Get(parsedMedia.Alias);
                return string.Format(videoHtml, SiteUrls.FullUrl(SiteUrls.Instance()._VideoDetail(parsedMedia.Alias)), shortUrlEntity == null ? null : shortUrlEntity.Url, parsedMedia.ThumbnailUrl);
            }

            return string.Empty;
        }
Exemplo n.º 6
0
        /// <summary>
        /// 网址解析器
        /// </summary>
        /// <param name="url"></param>
        /// <param name="mediaType"></param>
        /// <returns></returns>
        public ActionResult ParseMedia(MediaType mediaType, string url = null)
        {
            if (UserContext.CurrentUser == null)
                return Json(new { errorMessage = "您没有登录" }, JsonRequestBehavior.AllowGet);
            if (string.IsNullOrEmpty(url))
                return Json(new { errorMessage = "url不能为空" }, JsonRequestBehavior.AllowGet);

            if (!Utility.IsAllowableReferrer(Request))
                return Json(new { errorMessage = "站点不支持跨域访问" }, JsonRequestBehavior.AllowGet);
            List<string> keys = GetUrlParserKeys(mediaType);
            string key = keys.FirstOrDefault(n => url.ToLower().Contains(n.ToLower()));
            //若已经在短网址中存在,则直接返回结果
            ShortUrlService shortUrlService = new ShortUrlService();
            bool urlExists = false;
            string alias = shortUrlService.GetUrlAlias(url, out urlExists);
            //string alias = IdGenerator.Next().ToString();
            ParsedMediaService parsedMediaService = new ParsedMediaService();
            ParsedMedia entity = parsedMediaService.Get(alias);
            if (entity != null)
                return Json(entity, JsonRequestBehavior.AllowGet);

            if (string.IsNullOrEmpty(key))
                return View("~/Plugins/MediaParsers/" + mediaType.ToString() + "/Default.cshtml");

            return View("~/Plugins/MediaParsers/" + mediaType.ToString() + "/" + key + ".cshtml");
        }