Exemple #1
0
        public void Save()
        {
            // Clear data
            if (helper.Request(ClientID + "clear") == "1")
            {
                // delete file
                deleteFile(_text);

                // set filename in db to nothing
                _text       = "";
                _data.Value = _text;


                foreach (string prop in "umbracoExtension,umbracoBytes,umbracoWidth,umbracoHeight".Split(','))
                {
                    try
                    {
                        var bytesControl = FindControlRecursive <noEdit>(Page, "prop_" + prop);
                        if (bytesControl != null)
                        {
                            bytesControl.RefreshLabel(string.Empty);
                        }
                    }
                    catch
                    {
                        //if first one fails we can assume that props don't exist
                        break;
                    }
                }
            }

            if (PostedFile != null && PostedFile.FileName != String.Empty)
            {
                _data.Value = PostedFile;

                // we update additional properties post image upload
                if (_data.Value != DBNull.Value && !string.IsNullOrEmpty(_data.Value.ToString()))
                {
                    Content content = Content.GetContentFromVersion(_data.Version);

                    // update extension in UI
                    try
                    {
                        var extensionControl = FindControlRecursive <noEdit>(Page, "prop_umbracoExtension");
                        if (extensionControl != null)
                        {
                            extensionControl.RefreshLabel(content.getProperty("umbracoExtension").Value.ToString());
                        }
                    }
                    catch
                    {
                    }


                    // update file size in UI
                    try
                    {
                        var bytesControl = FindControlRecursive <noEdit>(Page, "prop_umbracoBytes");
                        if (bytesControl != null)
                        {
                            bytesControl.RefreshLabel(content.getProperty("umbracoBytes").Value.ToString());
                        }
                    }
                    catch
                    {
                    }

                    try
                    {
                        var widthControl = FindControlRecursive <noEdit>(Page, "prop_umbracoWidth");
                        if (widthControl != null)
                        {
                            widthControl.RefreshLabel(content.getProperty("umbracoWidth").Value.ToString());
                        }
                        var heightControl = FindControlRecursive <noEdit>(Page, "prop_umbracoHeight");
                        if (heightControl != null)
                        {
                            heightControl.RefreshLabel(content.getProperty("umbracoHeight").Value.ToString());
                        }
                    }
                    catch
                    {
                    }
                }
                Text = _data.Value.ToString();
            }
        }
Exemple #2
0
        private string formatMedia(string html)
        {
            // Local media path
            string localMediaPath = getLocalMediaPath();

            // Find all media images
            string pattern = "<img [^>]*src=\"(?<mediaString>/media[^\"]*)\" [^>]*>";

            MatchCollection tags =
                Regex.Matches(html, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

            foreach (Match tag in tags)
            {
                if (tag.Groups.Count > 0)
                {
                    // Replace /> to ensure we're in old-school html mode
                    string tempTag = "<img";
                    string orgSrc  = tag.Groups["mediaString"].Value;

                    // gather all attributes
                    // TODO: This should be replaced with a general helper method - but for now we'll wanna leave Umbraco.dll alone for this patch
                    Hashtable       ht = new Hashtable();
                    MatchCollection m  =
                        Regex.Matches(tag.Value.Replace(">", " >"),
                                      "(?<attributeName>\\S*)=\"(?<attributeValue>[^\"]*)\"|(?<attributeName>\\S*)=(?<attributeValue>[^\"|\\s]*)\\s",
                                      RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
                    foreach (Match attributeSet in m)
                    {
                        if (attributeSet.Groups["attributeName"].Value.ToString().ToLower() != "src")
                        {
                            ht.Add(attributeSet.Groups["attributeName"].Value.ToString(),
                                   attributeSet.Groups["attributeValue"].Value.ToString());
                        }
                    }

                    // build the element
                    // Build image tag
                    IDictionaryEnumerator ide = ht.GetEnumerator();
                    while (ide.MoveNext())
                    {
                        tempTag += " " + ide.Key.ToString() + "=\"" + ide.Value.ToString() + "\"";
                    }

                    // Find the original filename, by removing the might added width and height
                    orgSrc =
                        orgSrc.Replace(
                            "_" + helper.FindAttribute(ht, "width") + "x" + helper.FindAttribute(ht, "height"), "").
                        Replace("%20", " ");

                    // Check for either id or guid from media
                    string mediaId = getIdFromSource(orgSrc, localMediaPath);

                    Media imageMedia = null;

                    try
                    {
                        int      mId = int.Parse(mediaId);
                        Property p   = new Property(mId);
                        imageMedia = new Media(Content.GetContentFromVersion(p.VersionId).Id);
                    }
                    catch
                    {
                        try
                        {
                            imageMedia = new Media(Content.GetContentFromVersion(new Guid(mediaId)).Id);
                        }
                        catch
                        {
                        }
                    }

                    // Check with the database if any media matches this url
                    if (imageMedia != null)
                    {
                        try
                        {
                            // Check extention
                            if (imageMedia.getProperty("umbracoExtension").Value.ToString() != orgSrc.Substring(orgSrc.LastIndexOf(".") + 1, orgSrc.Length - orgSrc.LastIndexOf(".") - 1))
                            {
                                orgSrc = orgSrc.Substring(0, orgSrc.LastIndexOf(".") + 1) +
                                         imageMedia.getProperty("umbracoExtension").Value.ToString();
                            }

                            // Format the tag
                            tempTag = tempTag + " rel=\"" +
                                      imageMedia.getProperty("umbracoWidth").Value.ToString() + "," +
                                      imageMedia.getProperty("umbracoHeight").Value.ToString() + "\" src=\"" + orgSrc +
                                      "\"";
                            tempTag += "/>";

                            // Replace the tag
                            html = html.Replace(tag.Value, tempTag);
                        }
                        catch (Exception ee)
                        {
                            Log.Add(LogTypes.Error, User.GetUser(0), -1,
                                    "Error reading size data from media: " + imageMedia.Id.ToString() + ", " +
                                    ee.ToString());
                        }
                    }
                    else
                    {
                        Log.Add(LogTypes.Error, User.GetUser(0), -1,
                                "Error reading size data from media (not found): " + orgSrc);
                    }
                }
            }
            return(html);
        }
Exemple #3
0
        private string formatMedia(string html)
        {
            // root media url
            var rootMediaUrl = _fs.GetUrl("");

            // Find all media images
            var pattern = String.Format("<img [^>]*src=\"(?<mediaString>{0}[^\"]*)\" [^>]*>", rootMediaUrl);

            MatchCollection tags =
                Regex.Matches(html, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

            foreach (Match tag in tags)
            {
                if (tag.Groups.Count > 0)
                {
                    // Replace /> to ensure we're in old-school html mode
                    string tempTag = "<img";
                    string orgSrc  = tag.Groups["mediaString"].Value;

                    // gather all attributes
                    // TODO: This should be replaced with a general helper method - but for now we'll wanna leave umbraco.dll alone for this patch
                    Hashtable       ht = new Hashtable();
                    MatchCollection m  =
                        Regex.Matches(tag.Value.Replace(">", " >"),
                                      "(?<attributeName>\\S*)=\"(?<attributeValue>[^\"]*)\"|(?<attributeName>\\S*)=(?<attributeValue>[^\"|\\s]*)\\s",
                                      RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

                    //GE: Add ContainsKey check and expand the ifs for readability
                    foreach (Match attributeSet in m)
                    {
                        if (attributeSet.Groups["attributeName"].Value.ToString().ToLower() != "src")
                        {
                            if (!ht.ContainsKey(attributeSet.Groups["attributeName"].Value.ToString()))
                            {
                                ht.Add(attributeSet.Groups["attributeName"].Value.ToString(), attributeSet.Groups["attributeValue"].Value.ToString());
                            }
                        }
                    }

                    // build the element
                    // Build image tag
                    IDictionaryEnumerator ide = ht.GetEnumerator();
                    while (ide.MoveNext())
                    {
                        tempTag += " " + ide.Key.ToString() + "=\"" + ide.Value.ToString() + "\"";
                    }

                    // Find the original filename, by removing the might added width and height
                    // NH, 4.8.1 - above replaced by loading the right media file from the db later!
                    orgSrc =
                        global::Umbraco.Core.IO.IOHelper.ResolveUrl(orgSrc.Replace("%20", " "));

                    // Check for either id or guid from media
                    string mediaId = getIdFromSource(orgSrc, rootMediaUrl);

                    Media imageMedia = null;

                    try
                    {
                        int      mId = int.Parse(mediaId);
                        Property p   = new Property(mId);
                        imageMedia = new Media(Content.GetContentFromVersion(p.VersionId).Id);
                    }
                    catch
                    {
                        try
                        {
                            imageMedia = new Media(Content.GetContentFromVersion(new Guid(mediaId)).Id);
                        }
                        catch
                        {
                        }
                    }

                    // Check with the database if any media matches this url
                    if (imageMedia != null)
                    {
                        try
                        {
                            // Format the tag
                            tempTag = tempTag + " rel=\"" +
                                      imageMedia.getProperty("umbracoWidth").Value.ToString() + "," +
                                      imageMedia.getProperty("umbracoHeight").Value.ToString() + "\" src=\"" + global::Umbraco.Core.IO.IOHelper.ResolveUrl(imageMedia.getProperty("umbracoFile").Value.ToString()) +
                                      "\"";
                            tempTag += "/>";

                            // Replace the tag
                            html = html.Replace(tag.Value, tempTag);
                        }
                        catch (Exception ee)
                        {
                            Log.Add(LogTypes.Error, User.GetUser(0), -1,
                                    "Error reading size data from media: " + imageMedia.Id.ToString() + ", " +
                                    ee.ToString());
                        }
                    }
                    else
                    {
                        Log.Add(LogTypes.Error, User.GetUser(0), -1,
                                "Error reading size data from media (not found): " + orgSrc);
                    }
                }
            }
            return(html);
        }