예제 #1
0
    // ----------------------------------------------------------------------
    /// <summary>
    /// Gets the image to use for a post list item
    /// </summary>
    /// <param name="postId">The Id of the post</param>
    /// <param name="style">Inline css to add</param>
    /// <returns>Html string for the image.</returns>
    public static string GetPostImage(Guid postId, string style)
    {
        if (!ExtensionManager.ExtensionEnabled(_ExtensionName))
        {
            return(String.Empty);
        }
        Post post = Post.GetPost(postId);

        if (post != null)
        {
            int tagIndex = 0;
            List <BlogParser.HtmlTag> imgTags = BlogParser.HtmlDom.GetTagByTagName(
                BlogParser.ParseForDoms(post.Content),
                "img");

            // See if a different image has been specified to be used
            for (int i = 0; i < imgTags.Count; i++)
            {
                if (String.Compare(imgTags[i]["postlist"].Value, "true", true) == 0)
                {
                    tagIndex = i;
                    break;
                }
            }

            if (imgTags.Count > 0) // Images Found
            {
                // Add or replace CssClass specified in settings
                if (!String.IsNullOrEmpty(_ImageCssClass))
                {
                    imgTags[tagIndex].Insert(0, new BlogParser.HtmlAttribute("class", _ImageCssClass));
                }

                // Get or Create Image to display
                ImageInfo image = GetSmallFile(imgTags[tagIndex]["src"].Value);

                if (image != null)
                {
                    if (!String.IsNullOrEmpty(image.File) && image.File[0] == '\0') // See if error from GetSmallFile and send back
                    {
                        if (Security.IsAuthenticated)
                        {
                            string r = image.File.Replace("\0", String.Empty);

                            if (string.IsNullOrWhiteSpace(r))
                            {
                                return(imgTags[tagIndex]["src"].Value);
                            }
                            else
                            {
                                return("\">" + r);
                            }
                        }
                        else
                        {
                            return(string.Empty);
                        }
                    }

                    imgTags[tagIndex]["src"].Value = image.File;

                    // Add root if there is none
                    if (String.IsNullOrEmpty(Path.GetDirectoryName(imgTags[tagIndex]["src"].Value)))
                    {
                        imgTags[tagIndex]["src"].Value = Blog.CurrentInstance.AbsoluteWebRoot + imgTags[tagIndex]["src"].Value;
                    }

                    // Prevent the browser from caching so that changes made by an authenticated user will be displayed
                    if (Security.IsAuthenticated)
                    {
                        if (imgTags[tagIndex]["src"].Value.Contains("?"))
                        {
                            imgTags[tagIndex]["src"].Value += "&nocache=" + Guid.NewGuid().ToString().Replace("-", String.Empty);
                        }
                        else
                        {
                            imgTags[tagIndex]["src"].Value += "?nocache=" + Guid.NewGuid().ToString().Replace("-", String.Empty);
                        }
                    }

                    if (_ForceMaxWidth && _ForceMaxHeight)
                    {
                        if (image.GetWidth() > _MaxWidth)
                        {
                            image.Width = _MaxWidth + "px";
                        }
                        if (image.GetHeight() > _MaxHeight)
                        {
                            image.Height = _MaxHeight + "px";
                        }
                    }
                    if (_ForceMaxWidth)
                    {
                        image.Width = _MaxWidth.ToString() + _Units;
                        imgTags[tagIndex]["width"].Value = image.Width;
                    }
                    else
                    {
                        imgTags[tagIndex]["width"].Value = null;
                    }

                    if (_ForceMaxHeight)
                    {
                        image.Height = _MaxHeight.ToString() + _Units;
                        imgTags[tagIndex]["height"].Value = image.Height;
                    }
                    else
                    {
                        imgTags[tagIndex]["height"].Value = null;
                    }

                    imgTags[tagIndex]["alt"].Value   = post.Title;
                    imgTags[tagIndex]["title"].Value = post.Title;

                    BlogParser.HtmlStyleAttribute styleAttr = new BlogParser.HtmlStyleAttribute();
                    styleAttr.AddRange(style);             // Add single instance specified style
                    styleAttr.AddRange(_ImageInlineStyle); // Add global specified style
                    if (_ForceMaxWidth)
                    {
                        styleAttr.Add("width", image.Width, false);
                    }

                    if (_ForceMaxHeight)
                    {
                        styleAttr.Add("height", image.Height, false);
                    }


                    imgTags[tagIndex]["style"].Value = styleAttr.Value;

                    BlogParser.HtmlTag alink = new BlogParser.HtmlTag("a",
                                                                      new BlogParser.HtmlAttribute("href", HttpUtility.UrlDecode(post.AbsoluteLink.AbsolutePath))
                                                                      );

                    alink.InnerHtml.Add(imgTags[tagIndex].Clone() as BlogParser.HtmlTag);
                    return(alink.ToHtml());
                }
                else
                {
                    // image == null

                    if (_UseDefaultImage)
                    {
                        BlogParser.HtmlTag alink = new BlogParser.HtmlTag("a",
                                                                          new BlogParser.HtmlAttribute("href", HttpUtility.UrlDecode(post.AbsoluteLink.AbsolutePath)));
                        BlogParser.HtmlStyleAttribute styleAttr = new BlogParser.HtmlStyleAttribute();
                        styleAttr.AddRange(style);             // Add single instance specified style
                        styleAttr.AddRange(_ImageInlineStyle); // Add global specified style
                        BlogParser.HtmlTag defaultTag = new BlogParser.HtmlTag("img",
                                                                               new BlogParser.HtmlAttribute("src", _DefaultImagePath),
                                                                               new BlogParser.HtmlAttribute("alt", post.Title),
                                                                               new BlogParser.HtmlAttribute("title", post.Title),
                                                                               new BlogParser.HtmlAttribute("class", _ImageCssClass),
                                                                               styleAttr);
                        alink.InnerHtml.Add(defaultTag);
                        return(alink.ToString());
                    }
                }
            }
            else // No images in post
            {
                if (_UseDefaultImage)
                {
                    BlogParser.HtmlTag alink = new BlogParser.HtmlTag("a",
                                                                      new BlogParser.HtmlAttribute("href", HttpUtility.UrlDecode(post.AbsoluteLink.AbsolutePath)));
                    BlogParser.HtmlStyleAttribute styleAttr = new BlogParser.HtmlStyleAttribute();
                    styleAttr.AddRange(style);             // Add single instance specified style
                    styleAttr.AddRange(_ImageInlineStyle); // Add global specified style
                    BlogParser.HtmlTag defaultTag = new BlogParser.HtmlTag("img",
                                                                           new BlogParser.HtmlAttribute("src", _DefaultImagePath),
                                                                           new BlogParser.HtmlAttribute("alt", post.Title),
                                                                           new BlogParser.HtmlAttribute("title", post.Title),
                                                                           new BlogParser.HtmlAttribute("class", _ImageCssClass),
                                                                           styleAttr);
                    alink.InnerHtml.Add(defaultTag);
                    return(alink.ToString());
                }
            }
        }
        return(String.Empty);
    }
예제 #2
0
    // ----------------------------------------------------------------------
    /// <summary>
    /// Gets the image to use for a post list item
    /// </summary>
    /// <param name="postId">The Id of the post</param>
    /// <param name="style">Inline css to add</param>
    /// <returns>Html string for the image.</returns>
    public static string GetPostImage(Guid postId, string style)
    {
        if (!ExtensionManager.ExtensionEnabled(_ExtensionName))
        {
            return(String.Empty);
        }
        Post post = Post.GetPost(postId);

        if (post != null)
        {
            int tagIndex = 0;
            List <BlogParser.HtmlTag> imgTags = BlogParser.HtmlDom.GetTagByTagName(
                BlogParser.ParseForDoms(post.Content),
                "img");

            // See if a different image has been specified to be used
            for (int i = 0; i < imgTags.Count; i++)
            {
                if (String.Compare(imgTags[i]["postlist"].Value, "true", true) == 0)
                {
                    tagIndex = i;
                    break;
                }
            }

            if (imgTags.Count > 0) // Images Found
            {
                // Add or replace CssClass specified in settings
                if (!String.IsNullOrEmpty(_ImageCssClass))
                {
                    imgTags[tagIndex].Insert(0, new BlogParser.HtmlAttribute("class", _ImageCssClass));
                }

                // Get filename for the new image to be created
                SmallImageUri imageUri = new SmallImageUri(imgTags[tagIndex]["src"].Value, _SmallFileNameAddon, _BlogImageFolder);
                if (!imageUri.FoundSmallFile)
                {
                    return(String.Empty);
                }


                ImageSize newSize;

                // Create Post list image if it does not exist
                if (!_ForceMaxHeight || !_ForceMaxWidth)
                {
                    // Get display size

                    try
                    {
                        using (FileStream imageStream = new FileStream(imageUri.FileHostPath, FileMode.Open, FileAccess.Read, FileShare.Read)) // Open the big file
                        {
                            using (Image image = Image.FromStream(imageStream))
                            {
                                newSize = GetDisplaySize(image.Width, image.Height);
                            }
                        }
                    }
                    catch (DirectoryNotFoundException err)
                    {
                        return(ShowError(err, "DirectoryNotFound: " + imageUri.FileHostPath));
                    }
                    catch (FileNotFoundException err)
                    {
                        return(ShowError(err, "FileNotFound: " + imageUri.FileHostPath));
                    }
                    catch (PathTooLongException err)
                    {
                        return(ShowError(err, "PathTooLong: " + imageUri.FileHostPath));
                    }
                    catch (IOException err)
                    {
                        return(ShowError(err, "IOException: " + imageUri.FileHostPath));
                    }
                }
                else // No need to get a display size
                {
                    newSize = new ImageSize(_MaxWidth, _MaxHeight, "px");
                }


                // Create Image to display
                string smallFile = GetSmallFile(imgTags[tagIndex]["src"].Value, Convert.ToInt32(newSize.Width.Replace(_Units, String.Empty)),
                                                Convert.ToInt32(newSize.Height.Replace(_Units, String.Empty)));

                if (Security.IsAuthenticated && !String.IsNullOrEmpty(smallFile) && smallFile[0] == '\0')     // See if error from GetSmallFile and send back
                {
                    string r = smallFile.Replace("\0", String.Empty);
                    return("\">" + r);
                }


                imgTags[tagIndex]["src"].Value = smallFile;

                // Add root if there is none
                if (String.IsNullOrEmpty(Path.GetDirectoryName(imgTags[tagIndex]["src"].Value)))
                {
                    imgTags[tagIndex]["src"].Value = Blog.CurrentInstance.AbsoluteWebRoot + imgTags[tagIndex]["src"].Value;
                }

                // Prevent the browser from caching so that changes made by an authenticated user will be displayed
                if (Security.IsAuthenticated)
                {
                    if (imgTags[tagIndex]["src"].Value.Contains("?"))
                    {
                        imgTags[tagIndex]["src"].Value += "&nocache=" + Guid.NewGuid().ToString().Replace("-", String.Empty);
                    }
                    else
                    {
                        imgTags[tagIndex]["src"].Value += "?nocache=" + Guid.NewGuid().ToString().Replace("-", String.Empty);
                    }
                }

                if (_ForceMaxWidth && _ForceMaxHeight)
                {
                    if (newSize.GetWidth() > _MaxWidth)
                    {
                        newSize.Width = _MaxWidth + "px";
                    }
                    if (newSize.GetHeight() > _MaxHeight)
                    {
                        newSize.Height = _MaxHeight + "px";
                    }
                }

                imgTags[tagIndex]["width"].Value  = newSize.Width;
                imgTags[tagIndex]["height"].Value = newSize.Height;
                imgTags[tagIndex]["alt"].Value    = post.Title;
                imgTags[tagIndex]["title"].Value  = post.Title;

                BlogParser.HtmlStyleAttribute styleAttr = new BlogParser.HtmlStyleAttribute();
                styleAttr.AddRange(style);             // Add single instance specified style
                styleAttr.AddRange(_ImageInlineStyle); // Add global specified style
                styleAttr.Add("width", newSize.Width, false);
                styleAttr.Add("height", newSize.Height, false);

                imgTags[tagIndex]["style"].Value = styleAttr.Value;

                BlogParser.HtmlTag alink = new BlogParser.HtmlTag("a",
                                                                  new BlogParser.HtmlAttribute("href", HttpUtility.UrlDecode(post.AbsoluteLink.AbsolutePath))
                                                                  );

                alink.InnerHtml.Add(imgTags[tagIndex].Clone() as BlogParser.HtmlTag);
                return(alink.ToHtml());
            }
            else // No images in post
            {
                if (_UseDefaultImage)
                {
                    BlogParser.HtmlTag alink = new BlogParser.HtmlTag("a",
                                                                      new BlogParser.HtmlAttribute("href", HttpUtility.UrlDecode(post.AbsoluteLink.AbsolutePath)));
                    BlogParser.HtmlStyleAttribute styleAttr = new BlogParser.HtmlStyleAttribute();
                    styleAttr.AddRange(style);             // Add single instance specified style
                    styleAttr.AddRange(_ImageInlineStyle); // Add global specified style
                    BlogParser.HtmlTag defaultTag = new BlogParser.HtmlTag("img",
                                                                           new BlogParser.HtmlAttribute("src", _DefaultImagePath),
                                                                           new BlogParser.HtmlAttribute("alt", post.Title),
                                                                           new BlogParser.HtmlAttribute("title", post.Title),
                                                                           new BlogParser.HtmlAttribute("class", _ImageCssClass),
                                                                           styleAttr);
                    alink.InnerHtml.Add(defaultTag);
                    return(alink.ToString());
                }
            }
        }
        return(String.Empty);
    }