示例#1
0
        private static string BuildUrl(string hash, int?size, GravatarRating rating, GravatarDefaultImage defaultImage)
        {
            var url = new StringBuilder("http://www.gravatar.com/avatar/", 90);

            url.Append(hash);

            bool isFirst = true;
            Action <string, string> addParam = (p, v) => {
                url.AppendFormat("{0}{1}={2}", (isFirst ? '?' : '&'), p, v);
                isFirst = false;
            };

            if (size != null)
            {
                if (size < 1 || size > 512)
                {
                    throw new ArgumentOutOfRangeException(nameof(size), size, "Must be null or between 1 and 512, inclusive");
                }
                addParam("s", size.Value.ToString());
            }

            if (rating != GravatarRating.Default)
            {
                addParam("r", rating.ToString().ToLower());
            }

            if (defaultImage != GravatarDefaultImage.Default)
            {
                string code = GenerateDefaultImageCode(defaultImage);
                addParam("d", code);
            }
            return(url.ToString());
        }
示例#2
0
        private static string BuildUrl(string hash, int? size, GravatarRating rating, GravatarDefaultImage defaultImage) {
            var url = new StringBuilder("http://www.gravatar.com/avatar/", 90);
            url.Append(hash);

            bool isFirst = true;
            Action<string, string> addParam = (p, v) => {
                url.AppendFormat("{0}{1}={2}", (isFirst ? '?' : '&'), p, v);
                isFirst = false;
            };

            if (size != null) {
                if (size < 1 || size > 512) {
                    throw new ArgumentOutOfRangeException("size", size, "Must be null or between 1 and 512, inclusive");
                }
                addParam("s", size.Value.ToString());
            }

            if (rating != GravatarRating.Default) {
                addParam("r", rating.ToString().ToLower());
            }

            if (defaultImage != GravatarDefaultImage.Default) {
                string code = GenerateDefaultImageCode(defaultImage);
                addParam("d", code);
            }
            return url.ToString();
        }
示例#3
0
    /// <summary>
    /// Creates HTML for an <c>img</c> element that presents a Gravatar icon.
    /// </summary>
    /// <param name="html">The <see cref="HtmlHelper"/> upon which this extension method is provided.</param>
    /// <param name="email">The email address used to identify the icon.</param>
    /// <param name="size">An optional parameter that specifies the size of the square image in pixels.</param>
    /// <param name="rating">An optional parameter that specifies the safety level of allowed images.</param>
    /// <param name="defaultImage">An optional parameter that controls what image is displayed for email addresses that don't have associated Gravatar icons.</param>
    /// <param name="htmlAttributes">An optional parameter holding additional attributes to be included on the <c>img</c> element.</param>
    /// <returns>An HTML string of the <c>img</c> element that presents a Gravatar icon.</returns>
    public static string Gravatar(this HtmlHelper html,
                                  string email,
                                  int? size = null,
                                  GravatarRating rating = GravatarRating.Default,
                                  GravatarDefaultImage defaultImage = GravatarDefaultImage.MysteryMan,
                                  object htmlAttributes = null)
    {
      var url = new StringBuilder("http://www.gravatar.com/avatar/", 90);
      url.Append(email.ToMd5Hash());

      var isFirst = true;
      Action<string, string> addParam = (p, v) =>
      {
        url.Append(isFirst ? '?' : '&');
        isFirst = false;
        url.Append(p);
        url.Append('=');
        url.Append(v);
      };

      if (size != null)
      {
        if (size < 1 || size > 512)
          throw new ArgumentOutOfRangeException("size", size, "Must be null or between 1 and 512, inclusive.");
        addParam("s", size.Value.ToString(CultureInfo.InvariantCulture));
      }

      if (rating != GravatarRating.Default)
        addParam("r", rating.ToString().ToLower());

      switch (defaultImage)
      {
        case GravatarDefaultImage.Http404:
          addParam("d", "404");
          break;
        case GravatarDefaultImage.Identicon:
          addParam("d", "identicon");
          break;
        case GravatarDefaultImage.MonsterId:
          addParam("d", "monsterid");
          break;
        case GravatarDefaultImage.MysteryMan:
          addParam("d", "mm");
          break;
        case GravatarDefaultImage.Wavatar:
          addParam("d", "wavatar");
          break;
      }

      var tag = new TagBuilder("img");
      tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
      tag.Attributes.Add("src", url.ToString());

      if (size != null)
      {
        tag.Attributes.Add("width", size.ToString());
        tag.Attributes.Add("height", size.ToString());
      }
      return tag.ToString(TagRenderMode.SelfClosing);
    }
示例#4
0
        /// <summary>
        ///    Gets image url that depends on the given e-mail address.
        /// </summary>
        /// <param name="email"></param>
        /// <param name="size"></param>
        /// <param name="rating"></param>
        /// <param name="defaultImage"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static string GetImageAddress(string email, int?size            = null,
                                             GravatarRating rating             = GravatarRating.Default,
                                             GravatarDefaultImage defaultImage = GravatarDefaultImage.MysteryMan)
        {
            var url = new StringBuilder("//www.gravatar.com/avatar/", 90);

            url.Append(GetEmailHash(email));

            var isFirst = true;
            Action <string, string> addParam = (p, v) =>
            {
                url.Append(isFirst ? '?' : '&');
                isFirst = false;
                url.Append(p);
                url.Append('=');
                url.Append(v);
            };

            if (size != null)
            {
                if (size < 1 || size > 512)
                {
                    throw new ArgumentOutOfRangeException("size", size,
                                                          "Must be null or between 1 and 512, inclusive.");
                }
                addParam("s", size.Value.ToString());
            }

            if (rating != GravatarRating.Default)
            {
                addParam("r", rating.ToString().ToLower());
            }

            if (defaultImage != GravatarDefaultImage.Default)
            {
                if (defaultImage == GravatarDefaultImage.Http404)
                {
                    addParam("d", "404");
                }
                else if (defaultImage == GravatarDefaultImage.Identicon)
                {
                    addParam("d", "identicon");
                }
                else if (defaultImage == GravatarDefaultImage.MonsterId)
                {
                    addParam("d", "monsterid");
                }
                else if (defaultImage == GravatarDefaultImage.MysteryMan)
                {
                    addParam("d", "mm");
                }
                else if (defaultImage == GravatarDefaultImage.Wavatar)
                {
                    addParam("d", "wavatar");
                }
            }

            return(url.ToString());
        }
示例#5
0
        public static string GetUrl(
            string email,
            int imageSize         = 80,
            string defaultImage   = null,
            GravatarRating rating = GravatarRating.Default,
            string imageExtension = null
            )
        {
            if (String.IsNullOrEmpty(email))
            {
                throw new ArgumentException(
                          CommonResources.Argument_Cannot_Be_Null_Or_Empty,
                          "email"
                          );
            }
            if ((imageSize <= 0) || (imageSize > 512))
            {
                throw new ArgumentException(
                          HelpersToolkitResources.Gravatar_InvalidImageSize,
                          "imageSize"
                          );
            }

            StringBuilder url = new StringBuilder(GravatarUrl);

            email = email.Trim().ToLowerInvariant();
            url.Append(Crypto.Hash(email, algorithm: "md5").ToLowerInvariant());

            if (!String.IsNullOrEmpty(imageExtension))
            {
                if (!imageExtension.StartsWith(".", StringComparison.Ordinal))
                {
                    url.Append('.');
                }
                url.Append(imageExtension);
            }

            url.Append("?s=");
            url.Append(imageSize);

            if (rating != GravatarRating.Default)
            {
                url.Append("&r=");
                url.Append(rating.ToString().ToLowerInvariant());
            }

            if (!String.IsNullOrEmpty(defaultImage))
            {
                url.Append("&d=");
                url.Append(HttpUtility.UrlEncode(defaultImage));
            }

            return(HttpUtility.HtmlAttributeEncode(url.ToString()));
        }
示例#6
0
        public static string GetUrl(string email, int imageSize = 80, string defaultImage = null,
                                    GravatarRating rating = GravatarRating.Default, string imageExtension = null)
        {
            if (String.IsNullOrEmpty(email))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "email");
            }
            if ((imageSize <= 0) || (imageSize > 512))
            {
                throw new ArgumentException(HelpersToolkitResources.Gravatar_InvalidImageSize, "imageSize");
            }

            StringBuilder url = new StringBuilder(GravatarUrl);
            email = email.Trim().ToLowerInvariant();
            url.Append(Crypto.Hash(email, algorithm: "md5").ToLowerInvariant());

            if (!String.IsNullOrEmpty(imageExtension))
            {
                if (!imageExtension.StartsWith(".", StringComparison.Ordinal))
                {
                    url.Append('.');
                }
                url.Append(imageExtension);
            }

            url.Append("?s=");
            url.Append(imageSize);

            if (rating != GravatarRating.Default)
            {
                url.Append("&r=");
                url.Append(rating.ToString().ToLowerInvariant());
            }

            if (!String.IsNullOrEmpty(defaultImage))
            {
                url.Append("&d=");
                url.Append(HttpUtility.UrlEncode(defaultImage));
            }

            return HttpUtility.HtmlAttributeEncode(url.ToString());
        }
示例#7
0
文件: Gravatar.cs 项目: syurov/rt.srz
        private string GetUrl(string email, int?size, string defaultImage, GravatarRating rating)
        {
            StringBuilder url = new StringBuilder("http://www.gravatar.com/avatar/");

            url.Append(GetHash(Email));

            if (size == null)
            {
                size = 80;
            }
            url.Append("?s=");
            url.Append(size);

            if (!String.IsNullOrEmpty(defaultImage))
            {
                url.Append("&d=");
                url.Append(defaultImage);
            }
            else if (DefaultImageBehavior != GravatarDefaultImageBehavior.Default)
            {
                var behaviorName = DefaultImageBehavior.ToString().ToLower();
                switch (DefaultImageBehavior)
                {
                case GravatarDefaultImageBehavior.MysteryMan:
                    behaviorName = "mm";
                    break;
                }

                url.Append("&d=" + behaviorName);
            }

            if (rating != GravatarRating.Default)
            {
                url.Append("&r=");
                url.Append(rating.ToString().ToLowerInvariant());
            }

            return(url.ToString());
        }
        /// <summary>
        /// Renders an HTML img tag that references a Gravatar avatar based off a user's email address
        /// </summary>
        /// <param name="html">The <see cref="HtmlHelper"/></param>
        /// <param name="email">The email address</param>
        /// <param name="defaultGravatar">
        /// The avatar you want to use if the email address doesn't have a Gravatar associated with it.
        /// You can either provide a URL to the image you want to use, or pass in one of the constants
        /// defined in <see cref="GravatarDefaults"/>.
        /// </param>
        /// <param name="size">The size in pixels of the image return. Must be between 1 and 512.</param>
        /// <param name="rating">The maximum rating that the Gravatar can be.</param>
        /// <param name="htmlAttributes">Any HTML attributes you want applied to the img tag</param>
        /// <returns>The img tag string</returns>
        /// <exception cref="ArgumentException">
        /// If <paramref name="size"/> is less than 1 or greater than 512
        /// </exception>
        public static MvcHtmlString Gravatar(this HtmlHelper html, string email, string defaultGravatar, int size, GravatarRating rating, IDictionary <string, object> htmlAttributes)
        {
            if (size < 1 || size > 512)
            {
                throw new ArgumentException("size must be between 1 and 512", "size");
            }

            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext);

            defaultGravatar = url.Encode(defaultGravatar);
            if (email == null)
            {
                email = String.Empty;
            }
            email = email.ToLower();

            StringBuilder hashBuilder = new StringBuilder();
            MD5           md5         = MD5.Create();

            foreach (byte aByte in md5.ComputeHash(Encoding.Default.GetBytes(email)))
            {
                hashBuilder.Append(aByte.ToString("x2"));
            }

            TagBuilder img = new TagBuilder("img");

            img.MergeAttributes(htmlAttributes);
            img.Attributes["src"]    = String.Format("http://www.gravatar.com/avatar/{0}?d={1}&s={2}&r={3}", hashBuilder, defaultGravatar, size, rating.ToString().ToLower());
            img.Attributes["width"]  = size.ToString();
            img.Attributes["height"] = size.ToString();
            return(MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing)));
        }
        string GetUrl(string email, int? size, string defaultImage, GravatarRating rating) {
            var url = new StringBuilder("http://www.gravatar.com/avatar/");
            url.Append(GetHash(Email));

            if(size == null) size = 80;
            url.Append("?s=");
            url.Append(size);

            if(!String.IsNullOrEmpty(defaultImage)) {
                url.Append("&d=");
                url.Append(defaultImage);
            }
            else if(DefaultImageBehavior != GravatarDefaultImageBehavior.Default) {
                var behaviorName = DefaultImageBehavior.ToString().ToLower();
                switch(DefaultImageBehavior) {
                    case GravatarDefaultImageBehavior.MysteryMan:
                        behaviorName = "mm";
                        break;
                }

                url.Append("&d=" + behaviorName);
            }

            if(rating != GravatarRating.Default) {
                url.Append("&r=");
                url.Append(rating.ToString().ToLowerInvariant());
            }

            return url.ToString();
        }
示例#10
0
        public static string Gravatar(this HtmlHelper html,
                                      string email,
                                      int?size = null,
                                      GravatarRating rating             = GravatarRating.Default,
                                      GravatarDefaultImage defaultImage = GravatarDefaultImage.MysteryMan,
                                      object htmlAttributes             = null)
        {
            var url = new StringBuilder("http://www.gravatar.com/avatar/", 90);

            url.Append(GetEmailHash(email));

            var isFirst = true;
            Action <string, string> addParam = (p, v) =>
            {
                url.Append(isFirst ? '?' : '&');
                isFirst = false;
                url.Append(p);
                url.Append('=');
                url.Append(v);
            };

            if (size != null)
            {
                if (size < 1 || size > 512)
                {
                    throw new ArgumentOutOfRangeException("size", size, "Must be null or between 1 and 512, inclusive.");
                }
                addParam("s", size.Value.ToString());
            }

            if (rating != GravatarRating.Default)
            {
                addParam("r", rating.ToString().ToLower());
            }

            if (defaultImage != GravatarDefaultImage.Default)
            {
                if (defaultImage == GravatarDefaultImage.Http404)
                {
                    addParam("d", "404");
                }
                else if (defaultImage == GravatarDefaultImage.Identicon)
                {
                    addParam("d", "identicon");
                }
                if (defaultImage == GravatarDefaultImage.MonsterId)
                {
                    addParam("d", "monsterid");
                }
                if (defaultImage == GravatarDefaultImage.MysteryMan)
                {
                    addParam("d", "mm");
                }
                if (defaultImage == GravatarDefaultImage.Wavatar)
                {
                    addParam("d", "wavatar");
                }
            }

            var tag = new TagBuilder("img");

            tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            tag.Attributes.Add("src", url.ToString());

            if (size != null)
            {
                tag.Attributes.Add("width", size.ToString());
                tag.Attributes.Add("height", size.ToString());
            }

            return(tag.ToString());
        }
示例#11
0
 public static string Value(this GravatarRating rating)
 => rating.ToString().ToLower();
示例#12
0
        public static IHtmlString Gravatar(
            HtmlHelper Html,
            dynamic Shape,
            string Email,
            int? Size = null,
            GravatarRating Rating = GravatarRating.Default,
            GravatarDefaultImage DefaultImage = GravatarDefaultImage.MysteryMan) {

            var url = new StringBuilder("//www.gravatar.com/avatar/", 90);
            url.Append(GetEmailHash(Email));

            var isFirst = true;
            Action<string, string> addParam = (p, v) => {
                url.Append(isFirst ? '?' : '&');
                isFirst = false;
                url.Append(p);
                url.Append('=');
                url.Append(v);
            };

            if (Size != null) {
                if (Size < 1 || Size > 512)
                    throw new ArgumentOutOfRangeException("size", Size, "Must be null or between 1 and 512, inclusive.");
                addParam("s", Size.Value.ToString());
            }

            if (Rating != GravatarRating.Default)
                addParam("r", Rating.ToString().ToLower());

            if (DefaultImage != GravatarDefaultImage.Default) {
                if (DefaultImage == GravatarDefaultImage.Http404)
                    addParam("d", "404");
                else if (DefaultImage == GravatarDefaultImage.Identicon)
                    addParam("d", "identicon");
                if (DefaultImage == GravatarDefaultImage.MonsterId)
                    addParam("d", "monsterid");
                if (DefaultImage == GravatarDefaultImage.MysteryMan)
                    addParam("d", "mm");
                if (DefaultImage == GravatarDefaultImage.Wavatar)
                    addParam("d", "wavatar");
            }

            var tagBuilder = new TagBuilder("img");
            IDictionary<string, string> htmlAttributes = Shape.Attributes;
            IEnumerable<string> classes = Shape.Classes;
            string id = Shape.Id;

            tagBuilder.MergeAttributes(htmlAttributes, false);
            tagBuilder.Attributes.Add("src", url.ToString());

            foreach (var cssClass in classes ?? Enumerable.Empty<string>()) {
                tagBuilder.AddCssClass(cssClass);
            }

            if (!string.IsNullOrWhiteSpace(id)) {
                tagBuilder.GenerateId(id);
            }

            if (Size != null) {
                tagBuilder.Attributes.Add("width", Size.ToString());
                tagBuilder.Attributes.Add("height", Size.ToString());
            }

            return MvcHtmlString.Create(tagBuilder.ToString());
        }
示例#13
0
        public static IHtmlString Gravatar(
            HtmlHelper Html,
            dynamic Shape,
            string Email,
            int?Size = null,
            GravatarRating Rating             = GravatarRating.Default,
            GravatarDefaultImage DefaultImage = GravatarDefaultImage.MysteryMan)
        {
            var url = new StringBuilder("//www.gravatar.com/avatar/", 90);

            url.Append(GetEmailHash(Email));

            var isFirst = true;
            Action <string, string> addParam = (p, v) => {
                url.Append(isFirst ? '?' : '&');
                isFirst = false;
                url.Append(p);
                url.Append('=');
                url.Append(v);
            };

            if (Size != null)
            {
                if (Size < 1 || Size > 512)
                {
                    throw new ArgumentOutOfRangeException("size", Size, "Must be null or between 1 and 512, inclusive.");
                }
                addParam("s", Size.Value.ToString());
            }

            if (Rating != GravatarRating.Default)
            {
                addParam("r", Rating.ToString().ToLower());
            }

            if (DefaultImage != GravatarDefaultImage.Default)
            {
                if (DefaultImage == GravatarDefaultImage.Http404)
                {
                    addParam("d", "404");
                }
                else if (DefaultImage == GravatarDefaultImage.Identicon)
                {
                    addParam("d", "identicon");
                }
                if (DefaultImage == GravatarDefaultImage.MonsterId)
                {
                    addParam("d", "monsterid");
                }
                if (DefaultImage == GravatarDefaultImage.MysteryMan)
                {
                    addParam("d", "mm");
                }
                if (DefaultImage == GravatarDefaultImage.Wavatar)
                {
                    addParam("d", "wavatar");
                }
            }

            var tagBuilder = new TagBuilder("img");
            IDictionary <string, string> htmlAttributes = Shape.Attributes;
            IEnumerable <string>         classes        = Shape.Classes;
            string id = Shape.Id;

            tagBuilder.MergeAttributes(htmlAttributes, false);
            tagBuilder.Attributes.Add("src", url.ToString());

            foreach (var cssClass in classes ?? Enumerable.Empty <string>())
            {
                tagBuilder.AddCssClass(cssClass);
            }

            if (!string.IsNullOrWhiteSpace(id))
            {
                tagBuilder.GenerateId(id);
            }

            if (Size != null)
            {
                tagBuilder.Attributes.Add("width", Size.ToString());
                tagBuilder.Attributes.Add("height", Size.ToString());
            }

            return(MvcHtmlString.Create(tagBuilder.ToString()));
        }
        private static string BuildGravatarUrl(string email, GravatarRating rating, GravatarDefaultImage defaultImage, int? size = null)
        {
            var urlBuilder = new StringBuilder(GRAVATAR_URL, 90);
            urlBuilder.Append(GetEmailHash(email));

            var isFirst = true;
            Action<string, string> addParam = (name, value) =>
            {
                urlBuilder.Append(isFirst ? '?' : '&');
                isFirst = false;
                urlBuilder.Append(name);
                urlBuilder.Append("=");
                urlBuilder.Append(value);
            };

            if (size != null)
            {
                if (size < 1 || size > 512)
                    throw new ArgumentOutOfRangeException("size", size, "Must be null or value between 1 and 512, inclusive.");

                addParam("s", size.Value.ToString());
            }

            if (rating != GravatarRating.Default)
                addParam("r", rating.ToString().ToLower());

            if (defaultImage != GravatarDefaultImage.Default)
            {
                string imageToShow = "";

                switch (defaultImage)
                {
                    case GravatarDefaultImage.Http404:
                        imageToShow = "404";
                        break;
                    case GravatarDefaultImage.MysteryMan:
                        imageToShow = "mm";
                        break;
                    case GravatarDefaultImage.Identicon:
                        imageToShow = "identicon";
                        break;
                    case GravatarDefaultImage.MonsterId:
                        imageToShow = "monsterid";
                        break;
                    case GravatarDefaultImage.Wavatar:
                        imageToShow = "wavatar";
                        break;
                }

                addParam("d", imageToShow);
            }

            return urlBuilder.ToString();
        }