private static List <ImgTag> GetImgList(HtmlDocument document) { var documentImages = new List <HtmlNode>(); try { documentImages = document.DocumentNode.SelectNodes("//img[@src]").ToList(); } catch (Exception e) { ToScreen($"Error when selecting img nodes. Techical message: {e.Message}"); } var imgList = new List <ImgTag>(); foreach (var item in documentImages) { if (ImgTag.ValidateImg(item.OuterHtml)) { imgList.Add(new ImgTag(item.OuterHtml)); } } return(imgList.Select(r => new ImgTag { Path = r.Path, FileName = r.FileName }) .GroupBy(x => x.Path) .Select(x => x.First()).ToList()); }
/// <summary> /// Remove image width & heigth style /// </summary> /// <param name="html"></param> /// <returns></returns> public static string RemoveSizeStyle(string html) { var parser = new HtmlParser(); var document = parser.Parse(html); foreach (var ImgTag in document.QuerySelectorAll("img")) { ImgTag.RemoveAttribute("width"); ImgTag.RemoveAttribute("height"); List <string> ActualStyle = new List <string>(); if (!ImgTag.GetAttribute("style").IsNullOrEmpty()) { ActualStyle = ImgTag.GetAttribute("style").Split(';').ToList();; } string NewStyle = string.Empty; if (ActualStyle.Count > 0) { foreach (string StyleProperty in ActualStyle) { if (!StyleProperty.ToLower().Contains("width") && !StyleProperty.ToLower().Contains("height")) { NewStyle += StyleProperty.ToLower() + ";"; } } } ImgTag.SetAttribute("style", NewStyle); } return(document.Body.InnerHtml); }
public async Task Img(ulong guild, [Remainder] string tag) { ImgTag imgTag = Program.BotHandler.ImageTags.Find(x => x.GuildID == guild && x.Tag == tag); if (imgTag == null) { await Context.Channel.SendMessageAsync($"This Server doesn't have an img of Tag **{tag}** added."); return; } await Context.Channel.SendFileAsync(imgTag.ImageStream, imgTag.FileName); }
public string UpdateImage(HttpContext context) { var user = RequestSession.GetSessionUser(); string result = ""; try { var tag = context.Request["images"]; if (user != null) { string goodspath = ConfigurationManager.AppSettings["ImgUmasMapSrc"] + user.CompanyId + "\\"; string Webpath = ConfigurationManager.AppSettings["ImgUmasWebSrc"] + user.CompanyId + "/"; if (!string.IsNullOrEmpty(tag)) { string path = Base64ToImage("data:image/jpeg;base64," + tag).Replace("\\", "/"); user = RequestSession.GetSessionUser(); if (user != null) { using (var db = new UdowsYunPublicEntities()) { var id = db.Company.Where(p => p.userId == user.id).ToList(); foreach (var vals in id) { ApiSoapClient a = new ApiSoapClient(); string json = a.UploadLicense(vals.id, ImgaeFoByte(path)); ImgTag it = JsonConvert.DeserializeObject <ImgTag>(json); if (it != null) { if (it.result == 1) { result = "{ \"code\": 0,\"msg\": \"\",\"data\": {\"src\": \"" + Webpath + it.name + "\"}}"; } } } } } } else { result = "{ \"code\": 1,\"msg\": \"\",\"data\": {\"src\": \"\"}}"; } } } catch (Exception ex) { LogHelper lh = new LogHelper(); lh.WriteLog(ex.Message); throw; } return(result); }
public async Task Img([Remainder] string tag) { ImgTag imgTag = Program.BotHandler.ImageTags.Find(x => x.GuildID == Context.Guild.Id && x.Tag == tag); if (imgTag == null) { await Context.Channel.SendMessageAsync( $"This Server doesn't have an img of Tag **{tag}** added." + $"\nTo add an Img, use `{Program.PREFIX}AddImg` [`image_url`] [`Tag`]"); return; } await Context.Channel.SendFileAsync(imgTag.ImageStream, $"CalledBy-{Context.User.Id}{imgTag.Extension}"); }
public async Task RemoveImg([Remainder] string tag) { ImgTag imgTag = Program.BotHandler.ImageTags.Find(x => x.Tag == tag); if (imgTag != null) { Program.BotHandler.ImageTags.Remove(imgTag); File.Delete($"{ImgTag.ContentPath}/{Context.Guild.Id}/{imgTag.FileName}"); await Context.Channel.SendMessageAsync($"Img **{tag}** has been removed from the tag list."); } else { await Context.Channel.SendMessageAsync( $"Img **{tag}** doesn't exist in this server" + $"\nCheck All server Img Tags with {Program.PREFIX}Server."); } }
public async Task AddImg(string url, [Remainder] string tag) { bool pass = true; if (Context.User.Id != Program.ME) { int TagCount = Program.BotHandler.ImageTags.FindAll(x => x.GuildID == Context.Guild.Id).Count; int UserCount = ((SocketGuild)Context.Guild).Users.Count; int UserCountTemp = UserCount; int UserCountNeededNext = 0; int MaxTagCount = 0; while (UserCountTemp >= (MaxTagCount + 1) * 25) { UserCountTemp -= (MaxTagCount + 1) * 25; UserCountNeededNext += (MaxTagCount + 1) * 25; MaxTagCount++; } UserCountNeededNext += (MaxTagCount + 1) * 25; if (TagCount >= MaxTagCount) { await Context.Channel.SendMessageAsync($"You need **{UserCountNeededNext - UserCount}** more users in the server to add more imgs."); pass = false; } } if (pass) { if (Program.BotHandler.ImageTags.Exists(x => x.GuildID == Context.Guild.Id && x.Tag == tag)) { await Context.Channel.SendMessageAsync($"This server already has **{tag}** img."); } else { ImgTag imgTag = new ImgTag(Context, tag, url); Program.BotHandler.ImageTags.Add(imgTag); await Context.Channel.SendMessageAsync("Specified img has been added.\nRemember it is case sensitive and can only be used in this server!"); } } }
public string ImageRequest(HttpContext context) { string strResult = ""; string img = ""; List <object> list = new List <object>(); var user = RequestSession.GetSessionUser(); if (user != null) { string goodspath = ConfigurationManager.AppSettings["ImgUmasMapSrc"] + user.CompanyId; if (!Directory.Exists(goodspath)) { Directory.CreateDirectory(goodspath); } list = JsonConvert.DeserializeObject <List <object> >(context.Request["images"]); for (int i = 0; i < list.Count; i++) { var postedFile = list[i].ToString(); string imgpoth = Uploads(postedFile); ApiSoapClient a = new ApiSoapClient(); string json = a.UploadInfoImg(Convert.ToInt32(user.CompanyId), ImgaeFoByte(imgpoth)); ImgTag it = JsonConvert.DeserializeObject <ImgTag>(json); if (it != null) { if (it.result == 1) { img += it.name + ","; } } } strResult = img.Substring(0, img.Length - 1); } else { strResult = ""; } return(strResult); }
public static string ConvertToUbb(string htmlContent, ContentConvert beforeConvert, ContentConvert afterConvert) { string msg = string.Empty; htmlContent = htmlContent.Replace("\n", string.Empty); htmlContent = htmlContent.Replace("\r", string.Empty); htmlContent = regScript.Replace(htmlContent, string.Empty); htmlContent = regStyle.Replace(htmlContent, string.Empty); // htmlContent = htmlContent.Trim(); if (beforeConvert != null) { htmlContent = beforeConvert(htmlContent); } MatchCollection matchs = regTag.Matches(htmlContent); int tagIndex = 0; List <HtmlTagBase> tags = new List <HtmlTagBase>() , beginTags = new List <HtmlTagBase>() , endTages = new List <HtmlTagBase>() , singleTags = new List <HtmlTagBase>(); foreach (Match m in matchs) { HtmlTagBase t; string tagName = m.Groups[1].Value; //这里也可以根据HTML标签来反射生成Html标签类, 代码可以少很多, 但是这样性能不好。 switch (tagName.ToLower()) { case "img": t = new ImgTag(tagIndex, m.Value, m.Index); break; case "a": t = new ATag(tagIndex, m.Value, m.Index); break; case "font": t = new FontTag(tagIndex, m.Value, m.Index); break; case "span": t = new SpanTag(tagIndex, m.Value, m.Index); break; case "br": t = new BrTag(tagIndex, m.Value, m.Index); break; case "div": t = new DivTag(tagIndex, m.Value, m.Index); break; case "p": t = new PTag(tagIndex, m.Value, m.Index); break; case "table": t = new TableTag(tagIndex, m.Value, m.Index); break; case "ol": case "ul": t = new ListTag(tagIndex, m.Value, m.Index); break; case "td": case "tr": case "s": case "b": case "i": case "sub": case "sup": case "u": t = new PairTag(tagIndex, m.Value, m.Index); break; case "th": t = new PairTag(tagIndex, m.Value, m.Index); ((PairTag)t).UbbTag = "td"; break; case "strong": t = new PairTag(tagIndex, m.Value, m.Index); ((PairTag)t).UbbTag = "b"; break; case "em": t = new PairTag(tagIndex, m.Value, m.Index); ((PairTag)t).UbbTag = "i"; break; case "strike": t = new PairTag(tagIndex, m.Value, m.Index); ((PairTag)t).UbbTag = "s"; break; case "blockquote": t = new PairTag(tagIndex, m.Value, m.Index); ((PairTag)t).UbbTag = "indent"; break; case "h1": case "h2": case "h3": case "h4": case "h5": case "h6": t = new HTag(tagIndex, m.Value, m.Index); break; case "embed": t = new EmbedTag(tagIndex, m.Value, m.Index); break; default: t = new HtmlTagBase(tagIndex, m.Value, m.Index); break; } tagIndex++; t.TagList = tags; tags.Add(t); if (t.IsSingleTag) { singleTags.Add(t); } else { if (!t.IsEndTag) { beginTags.Add(t); } else { int flag = -1; for (int i = beginTags.Count - 1; i >= 0; i--) { if (beginTags[i].TagName == t.TagName)//匹配开始标记和结束标记 { flag = i; beginTags[i].Pair = t; t.Pair = beginTags[i]; break; } } if (flag >= 0) { for (int i = beginTags.Count - 1; i >= flag; i--) { beginTags.RemoveAt(i); } } } } } StringBuilder builder = new StringBuilder(htmlContent); for (int i = 0; i < tags.Count; i++) { if (!tags[i].IsSingleTag) { if (tags[i].Pair == null) { msg += tags[i].TagName + "没有对应的" + (tags[i].IsEndTag ? "开始" : "结束") + "标记\r\n"; } } if (!tags[i].IsEndTag || tags[i].IsSingleTag) { builder = tags[i].ConvertToUBB(builder); } } //builder.Replace(" ", string.Empty); builder.Replace("\t", string.Empty); htmlContent = builder.ToString(); htmlContent = new Regex("<.*?>").Replace(htmlContent, string.Empty); htmlContent = HttpUtility.HtmlDecode(htmlContent); if (afterConvert != null) { htmlContent = afterConvert(htmlContent); } return(htmlContent); }