public static IEnumerable<string> GetAttributeValues(this string html, string tagName, string attributeName) { var reader = new SgmlReader { DocType = "html", WhitespaceHandling = WhitespaceHandling.All, InputStream = new StringReader(string.Format("<html>{0}</html>", html)) }; while (reader.Read() && !reader.EOF) { if (reader.NodeType == XmlNodeType.Element && reader.LocalName == tagName) { yield return reader.GetAttribute(attributeName); } } }
public static AlbumItem AddVideo(UserContainer currContext, Guid albumId, string title, string description, string url, string embedCode) { var album = DataService.PerThread.AlbumSet.SingleOrDefault(x => x.Id == albumId); if (album == null) throw new BusinessLogicException("Указан неверный идентификатор"); if (string.IsNullOrWhiteSpace(title)) throw new BusinessLogicException("Не указано название видео"); if (currContext != null) { if (album.GroupId.HasValue) { var gm = GroupService.UserInGroup(currContext.Id, album.GroupId.Value); if (gm == null) throw new BusinessLogicException("Вы не состоите в группе"); if (album.IsOpen) { if (!(gm.State == (byte)GroupMemberState.Approved || gm.State == (byte)GroupMemberState.Moderator)) throw new BusinessLogicException("Только члены группы могут добавлять видео в альбом"); } else if (gm.State != (byte)GroupMemberState.Moderator) throw new BusinessLogicException("Только модераторы могут добавлять видео в альбом"); } else if (album.UserId.HasValue) { if (album.UserId != currContext.Id) throw new BusinessLogicException("Нельзя добавлять видео в чужой альбом"); } else throw new BusinessLogicException("Альбом ни к чему не привязан"); } string src; if (!string.IsNullOrWhiteSpace(embedCode)) { using (var sgml = new SgmlReader()) { sgml.InputStream = new StringReader(embedCode); sgml.Read(); src = sgml.GetAttribute("src"); } } else if (!string.IsNullOrWhiteSpace(url)) { var uri = new Uri(url); src = url; // TODO: мб лучше regexp для вычленения src switch (uri.Host.Replace("www.", string.Empty)) { case "youtube.com": //src = uri.Scheme + "://" + uri.Host + "/embed/" + HttpUtility.ParseQueryString(uri.Query).GetValues("v").First(); // это для iframe src = uri.Scheme + "://" + uri.Host + "/v/" + HttpUtility.ParseQueryString(uri.Query).GetValues("v").First(); break; case "youtu.be": //src = uri.Scheme + "://youtube.com/embed/" + uri.Segments[1]; // это для iframe src = uri.Scheme + "://youtube.com/v/" + uri.Segments[1]; break; /*case "vimeo.com": src = uri.Scheme + "://" + "player." + uri.Host + "/video" + uri.PathAndQuery; break; case "dailymotion.com": var query = uri.Fragment.Replace("#", string.Empty); src = uri.Scheme + "://" + uri.Host + "/embed/video/" + HttpUtility.ParseQueryString(query).GetValues("videoId").First(); break;*/ case "e2-e4.tv": src = uri.Scheme + "://" + uri.Host + uri.PathAndQuery + "/swf/player2.swf"; break; case "e2e4.tv": src = uri.Scheme + "://" + uri.Host + uri.PathAndQuery + "/swf/player2.swf"; break; } } else throw new BusinessLogicException("Источник видео не указан"); var albumItem = new AlbumItem { AlbumId = albumId, Title = title, Description = description, Type = (byte)AlbumItemType.Video, Src = src, CreationDate = DateTime.Now }; DataService.PerThread.AlbumItemSet.AddObject(albumItem); DataService.PerThread.LoadProperty(albumItem, x => x.Album); albumItem.Album.ChangeDate = DateTime.Now; DataService.PerThread.SaveChanges(); return albumItem; }
public void LoadHtml(string rawHtml) { if (rawHtml.StartsWith("<dsi:html")) { //parse and remove dsi:html tag... string tag = rawHtml.Substring(0, rawHtml.IndexOf('>') + 1) + "</dsi:html>"; rawHtml = rawHtml.Substring(rawHtml.IndexOf('>') + 1); rawHtml = rawHtml.Substring(0, rawHtml.Length - 11); /* <dsi:html formatting = [true | false] // do we convert line-breaks to br tags? container = [true | false] // do we render the html in a container div? ></dsi:html> */ SgmlReader sgml = new SgmlReader(); sgml.InputStream = new StringReader(tag); sgml.DocType = "HTML"; sgml.Read(); if (sgml.GetAttribute("formatting") != null) Formatting = bool.Parse(sgml.GetAttribute("formatting")); if (sgml.GetAttribute("container") != null) Container = bool.Parse(sgml.GetAttribute("container")); } this.rawHtml = rawHtml; }