From 48d620cdb0a85b7012ae00b4d155bdf8ee067338 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Wed, 26 Dec 2012 06:27:30 -0100 Subject: [PATCH] Cleanup RTE ImageHelper a little bit, still bad code but more readable now --- .../tinymce/tinyMCEImageHelper.cs | 138 +++++------------- 1 file changed, 33 insertions(+), 105 deletions(-) diff --git a/src/umbraco.editorControls/tinymce/tinyMCEImageHelper.cs b/src/umbraco.editorControls/tinymce/tinyMCEImageHelper.cs index bb578f43f4..66a3994ad4 100644 --- a/src/umbraco.editorControls/tinymce/tinyMCEImageHelper.cs +++ b/src/umbraco.editorControls/tinymce/tinyMCEImageHelper.cs @@ -1,14 +1,10 @@ using System; using System.Collections; -using System.Drawing; -using System.Drawing.Drawing2D; -using System.Drawing.Imaging; using System.Text.RegularExpressions; using System.Web; using Umbraco.Core.IO; -using umbraco.BusinessLogic; +using Umbraco.Core.Logging; using umbraco.cms.businesslogic.Files; -using umbraco.IO; namespace umbraco.editorControls.tinymce { @@ -16,47 +12,36 @@ namespace umbraco.editorControls.tinymce { public static string cleanImages(string html) { - string[] allowedAttributes = UmbracoSettings.ImageAllowedAttributes.ToLower().Split(','); - string pattern = @"]*>"; - MatchCollection tags = - Regex.Matches(html + " ", pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); + var allowedAttributes = UmbracoSettings.ImageAllowedAttributes.ToLower().Split(','); + const string pattern = @"]*>"; + var tags = Regex.Matches(html + " ", pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); foreach (Match tag in tags) { - if (tag.Value.ToLower().IndexOf("umbraco_macro") == -1) + if (tag.Value.ToLower().IndexOf("umbraco_macro", StringComparison.Ordinal) == -1) { - string cleanTag = "", " >"), - "(?\\S*)=\"(?[^\"]*)\"|(?\\S*)=(?[^\"|\\s]*)\\s", - RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); - foreach (Match attributeSet in m) + var ht = new Hashtable(); + var matches = Regex.Matches(tag.Value.Replace(">", " >"), "(?\\S*)=\"(?[^\"]*)\"|(?\\S*)=(?[^\"|\\s]*)\\s", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); + foreach (Match attributeSet in matches) { - ht.Add(attributeSet.Groups["attributeName"].Value.ToString().ToLower(), - attributeSet.Groups["attributeValue"].Value.ToString()); - if (attributeSet.Groups["attributeName"].Value.ToString().ToLower() == "width") - int.TryParse(attributeSet.Groups["attributeValue"].Value.ToString(), out orgWidth); - else if (attributeSet.Groups["attributeName"].Value.ToString().ToLower() == "height") - int.TryParse(attributeSet.Groups["attributeValue"].Value.ToString(), out orgHeight); + ht.Add(attributeSet.Groups["attributeName"].Value.ToLower(), + attributeSet.Groups["attributeValue"].Value); } // If rel attribute exists and if it's different from current sizing we should resize the image! if (helper.FindAttribute(ht, "rel") != "") { - int newWidth = 0, newHeight = 0; - // if size has changed resize image serverside - string[] newDims = helper.FindAttribute(ht, "rel").Split(",".ToCharArray()); - if (newDims.Length > 0 && - (newDims[0] != helper.FindAttribute(ht, "width") || - newDims[1] != helper.FindAttribute(ht, "height"))) + var newDims = helper.FindAttribute(ht, "rel").Split(",".ToCharArray()); + if (newDims.Length > 0 && (newDims[0] != helper.FindAttribute(ht, "width") || newDims[1] != helper.FindAttribute(ht, "height"))) { try { - cleanTag += doResize(ht, out newWidth, out newHeight); + int newWidth; + int newHeight; + cleanTag += DoResize(ht, out newWidth, out newHeight); } catch (Exception err) { @@ -66,8 +51,8 @@ namespace umbraco.editorControls.tinymce cleanTag += " width=\"" + helper.FindAttribute(ht, "width") + "\""; cleanTag += " height=\"" + helper.FindAttribute(ht, "height") + "\""; } - Log.Add(LogTypes.Error, User.GetUser(0), -1, - "Error resizing image in editor: " + err.ToString()); + + LogHelper.Error("Error resizing image in editor", err); } } else @@ -87,23 +72,21 @@ namespace umbraco.editorControls.tinymce cleanTag += " width=\"" + helper.FindAttribute(ht, "width") + "\""; cleanTag += " height=\"" + helper.FindAttribute(ht, "height") + "\""; } - } - // Build image tag - foreach (string attr in allowedAttributes) + foreach (var attr in allowedAttributes) { if (helper.FindAttribute(ht, attr) != "") { - string attrValue = helper.FindAttribute(ht, attr); + var attrValue = helper.FindAttribute(ht, attr); cleanTag += " " + attr + "=\"" + attrValue + "\""; } } - if (bool.Parse(GlobalSettings.EditXhtmlMode)) cleanTag += "/"; + cleanTag += ">"; html = html.Replace(tag.Value, cleanTag); } @@ -112,97 +95,42 @@ namespace umbraco.editorControls.tinymce return html; } - private static string doResize(Hashtable attributes, out int finalWidth, out int finalHeight) + private static string DoResize(IDictionary attributes, out int finalWidth, out int finalHeight) { var fs = FileSystemProviderManager.Current.GetFileSystemProvider(); + var orgSrc = HttpContext.Current.Server.HtmlDecode(helper.FindAttribute(attributes, "src").Replace("%20", " ")); + var orgDim = helper.FindAttribute(attributes, "rel").Split(",".ToCharArray()); + var orgWidth = float.Parse(orgDim[0]); + var orgHeight = float.Parse(orgDim[1]); + var newWidth = int.Parse(helper.FindAttribute(attributes, "width")); + var newHeight = int.Parse(helper.FindAttribute(attributes, "height")); - string resizeDim = helper.FindAttribute(attributes, "width") + "," + - helper.FindAttribute(attributes, "height"); - string[] resizeDimSplit = resizeDim.Split(','); - string orgSrc = HttpContext.Current.Server.HtmlDecode(helper.FindAttribute(attributes, "src").Replace("%20", " ")); - string[] orgDim = helper.FindAttribute(attributes, "rel").Split(",".ToCharArray()); - float orgWidth = float.Parse(orgDim[0]); - float orgHeight = float.Parse(orgDim[1]); - string newSrc = ""; - int newWidth = int.Parse(resizeDimSplit[0]); - int newHeight = int.Parse(resizeDimSplit[1]); + var newSrc = ""; - if (orgHeight > 0 && orgWidth > 0 && resizeDim != "" && orgSrc != "") + if (orgHeight > 0 && orgWidth > 0 && orgSrc != "") { // Check dimensions if (Math.Abs(orgWidth / newWidth) > Math.Abs(orgHeight / newHeight)) { - newHeight = (int)Math.Round((float)newWidth * (orgHeight / orgWidth)); + newHeight = (int)Math.Round(newWidth * (orgHeight / orgWidth)); } else { - newWidth = (int)Math.Round((float)newHeight * (orgWidth / orgHeight)); + newWidth = (int)Math.Round(newHeight * (orgWidth / orgHeight)); } - // update orgSrc to remove umbraco reference - //string resolvedMedia = IOHelper.ResolveUrl(SystemDirectories.Media); - //if (IOHelper.ResolveUrl(orgSrc).IndexOf(resolvedMedia) > -1) - //{ - // orgSrc = SystemDirectories.Media + orgSrc.Substring(orgSrc.IndexOf(resolvedMedia) + resolvedMedia.Length); //, orgSrc.Length - orgSrc.IndexOf(String.Format("/media/", SystemDirectories.Media))); - - //} - - //string fullSrc = IOHelper.MapPath(orgSrc); - var orgPath = fs.GetRelativePath(orgSrc); if (fs.FileExists(orgPath)) { var uf = new UmbracoFile(orgPath); newSrc = uf.Resize(newWidth, newHeight); } - - /* - // Load original image - Image image = Image.FromFile(fullSrc); - - - // Create new image with best quality settings - Bitmap bp = new Bitmap(newWidth, newHeight); - Graphics g = Graphics.FromImage(bp); - g.SmoothingMode = SmoothingMode.HighQuality; - g.InterpolationMode = InterpolationMode.HighQualityBicubic; - g.PixelOffsetMode = PixelOffsetMode.HighQuality; - - // Copy the old image to the new and resized - Rectangle rect = new Rectangle(0, 0, newWidth, newHeight); - g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); - - // Copy metadata - ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); - ImageCodecInfo codec = null; - for (int i = 0; i < codecs.Length; i++) - { - if (codecs[i].MimeType.Equals("image/jpeg")) - codec = codecs[i]; } - // Set compresion ratio to 90% - EncoderParameters ep = new EncoderParameters(); - ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L); - - // Save the new image - bp.Save(fullSrcNew, codec, ep); - * */ - } - - // return the new width and height finalWidth = newWidth; finalHeight = newHeight; - //GE: When the SystemDirectories.Media contains a ~, newSrc will also contain this and hasn't been resolved. - //This causes the editor to save content which contains a virtual url, and thus the image doesn't serve - //the admin editor successfully displays the image because the HTML is rewritten, but when you get the RTE content in the template - //it hasn't been replaced - //2011-08-12 added a IOHelper.ResolveUrl call around newSrc - //2012-08-20 IFileSystem now takes care of URL resolution, so should be safe to assume newSrc is a full/absolute URL - return - " src=\"" + newSrc + "\" width=\"" + newWidth.ToString() + "\" height=\"" + newHeight.ToString() + - "\""; + return " src=\"" + newSrc + "\" width=\"" + newWidth + "\" height=\"" + newHeight + "\""; } } } \ No newline at end of file