public static string GetImageTag(string imagePathOrMediaId, int maxWidth, int maxHeight, bool constrain) { if (string.IsNullOrEmpty(imagePathOrMediaId)) { return(string.Empty); } int mediaId = CommonUtil.ConvertToIntSafe(imagePathOrMediaId, -1); if (mediaId == -1) { return(GetImageTag(imagePathOrMediaId, maxWidth, maxHeight)); } Media media = UmbracoUtil.GetMedia(mediaId); if (media != null) { string file = media.GetPropertyValue("umbracoFile", string.Empty); if (!string.IsNullOrEmpty(file)) { string alt = HttpUtility.HtmlEncode(media.Text); string thumbnailUrl = ImageUtil.ThumbnailUrl(file, maxWidth, maxHeight, constrain); return(GetImageTag(thumbnailUrl, alt)); } } return(string.Empty); }
/// <summary> /// Gets the value from name value collection. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key">The key.</param> /// <param name="defaultValue">The default value.</param> /// <param name="nameValueCollection">The name value collection.</param> /// <returns></returns> public static T GetValueFromNameValueCollection <T>(string key, T defaultValue, NameValueCollection nameValueCollection) { if (nameValueCollection == null) { return(defaultValue); } string value = nameValueCollection[key]; if (string.IsNullOrEmpty(value)) { return(defaultValue); } Type type = typeof(T); if (type == typeof(Boolean)) { if (value == "1" || value.ToLower() == "true") { return((T)(object)(true)); } if (value == "0" || value.ToLower() == "false") { return((T)(object)(false)); } return(defaultValue); } else if (type == typeof(Int32)) { int defaultInt = (Int32)Convert.ChangeType(defaultValue, type); return((T)Convert.ChangeType(CommonUtil.ConvertToIntSafe(value, defaultInt), type)); } return((T)Convert.ChangeType(value, type)); }
public static string GetThumbnailUrl(string imagePathOrMediaId, int width, int height, bool constrain) { int mediaId = CommonUtil.ConvertToIntSafe(imagePathOrMediaId, -1); if (mediaId > -1) { return(ImageUtil.ThumbnailUrl(mediaId, width, height, constrain)); } return(ImageUtil.ThumbnailUrl(imagePathOrMediaId, width, height, constrain)); }
/// <summary> /// Gets the readable file size via bytes. /// </summary> /// <param name="bytes">The bytes.</param> /// <returns>A readable file size</returns> /// <example> /// <code lang="xsl" title=""> /// <![CDATA[ /// <xsl:value-of select="upac:GetReadableFileSizeViaBytes(1073741824)" /> /// Would give: 1 GB /// /// <xsl:value-of select="upac:GetReadableFileSizeViaBytes(1048576)" /> /// Would give: 1 MB /// /// <xsl:value-of select="upac:GetReadableFileSizeViaBytes(1024)" /> /// Would give: 1 KB /// /// <xsl:value-of select="upac:GetReadableFileSizeViaBytes(600)" /> /// Would give: 600 Bytes /// ]]> /// </code> /// </example> public static string GetReadableFileSizeViaBytes(string bytes) { int bytesAsInt = CommonUtil.ConvertToIntSafe(bytes, -1); if (bytesAsInt > -1) { return(CommonUtil.GetReadableFileSizeViaBytes(bytesAsInt)); } return(string.Empty); }
public static Node GetNode(string nodeId) { int id = CommonUtil.ConvertToIntSafe(nodeId, -1); if (id > -1) { return(GetNode(id)); } return(null); }
public static Media GetMedia(string mediaId) { int id = CommonUtil.ConvertToIntSafe(mediaId, -1); if (id > -1) { return(GetMedia(id)); } return(null); }
/// <summary> /// Gets the O embed HTML. /// </summary> /// <param name="url">The OEmbed URL/Source.</param> /// <param name="maxWidth">Width of the max.</param> /// <param name="maxHeight">Height of the max.</param> /// <returns>The html or an error message</returns> public static string GetOEmbedHtml(string url, string maxWidth, string maxHeight) { StringBuilder output = new StringBuilder(); if (!string.IsNullOrEmpty(url)) { string jsonResponse = string.Empty; StringBuilder oohembedUrl = new StringBuilder(); oohembedUrl.Append("http://oohembed.com/oohembed/?"); if (!string.IsNullOrEmpty(maxWidth)) { int width = CommonUtil.ConvertToIntSafe(maxWidth, -1); if (width > -1) { oohembedUrl.Append("maxwidth="); oohembedUrl.Append(width); oohembedUrl.Append("&"); } } if (!string.IsNullOrEmpty(maxHeight)) { int height = CommonUtil.ConvertToIntSafe(maxHeight, -1); if (height > -1) { oohembedUrl.Append("maxheight="); oohembedUrl.Append(height); oohembedUrl.Append("&"); } } oohembedUrl.Append("url="); oohembedUrl.Append(HttpUtility.UrlEncode(url)); string cacheKey = string.Format("Upac.Core.Utilities.OEmbed::{0}", oohembedUrl); Log.DebugFormat("Try to look in cache via cachekey: {0}", cacheKey); if (CacheUtil.Exist(cacheKey)) { Log.Debug("Cache gave a hit. Return html from cache"); return(CacheUtil.Get <string>(cacheKey)); } Log.Debug("No content in cache"); System.Net.WebClient webClient = new System.Net.WebClient(); try { jsonResponse = webClient.DownloadString(oohembedUrl.ToString()); } catch (System.Net.WebException exception) { if (exception.Status != System.Net.WebExceptionStatus.ProtocolError) { Log.Error("Exception smidt", exception); throw; } // If it's a ProtocolError. // oembed throws a 404 status, if the resource could not be looked up. output.Append("<a href=\"" + url + "\">" + url + "</a>"); output.Append("<br /><em>Error with embedding the source<br />oohembed source: " + oohembedUrl + "</em>"); } if (!string.IsNullOrEmpty(jsonResponse)) { JavaScriptSerializer scriptSerializer = new JavaScriptSerializer(); OEmbedResponse businessObject = scriptSerializer.Deserialize <OEmbedResponse>(jsonResponse); string html = businessObject.GetHtml(); CacheUtil.Insert(cacheKey, html, 10, false); output.Append(html); } } return(output.ToString()); }
/// <summary> /// Get the url to a node via node id and appending the provided alternate template and can include the domain incl. http:// /// </summary> /// <param name="nodeId">The node id.</param> /// <param name="alternateTemplate">The alternate template. May be empty.</param> /// <param name="includeDomain">if set to <c>true</c> include domain and http.</param> /// <returns>An url</returns> /// <example> /// <code lang="xsl" title=""> /// <![CDATA[ /// <xsl:value-of select="upac:UrlViaNodeId(1541, 'RSS', true())" /> /// Would give: http://www.domain.dk/Subjectpage/HelloWorld/RSS /// ]]> /// </code> /// <code lang="xsl" title="How to use on A"> /// <![CDATA[ /// <a href="{upac:UrlViaNodeId(1541, 'RSS', true())}">Hello World</a> /// Would give: <a href="http://www.domain.dk/Subjectpage/HelloWorld/RSS">Hello World</a> /// ]]> /// </code> /// <code lang="xsl" title="How to use on A"> /// <![CDATA[ /// <a href="{upac:UrlViaNodeId(1541, '', true())}">Hello World</a> /// Would give: <a href="http://www.domain.dk/Subjectpage/HelloWorld/">Hello World</a> /// ]]> /// </code> /// </example> public static string UrlViaNodeId(string nodeId, string alternateTemplate, bool includeDomain) { int id = CommonUtil.ConvertToIntSafe(nodeId, -1); return((id == -1) ? string.Empty : LinkManager.GetNodeUrl(id, alternateTemplate, includeDomain)); }
/// <summary> /// Converts a string to a number. If the input string is empty or can not be converted, the defaultNumber is returned instead. /// </summary> /// <param name="input">The input to be converted</param> /// <param name="defaultNumber">The default number to return if the input can not be converted.</param> /// <returns>The converted input</returns> /// <example> /// <code lang="xsl" title=""> /// <![CDATA[ /// <xsl:value-of select="upac:ToNumber('123', 0)" /> /// Would give: 123 /// /// <xsl:value-of select="upac:ToNumber('XXX', 0)" /> /// Would give: 0 /// ]]> /// </code> /// </example> public static int ToNumber(string input, int defaultNumber) { return(CommonUtil.ConvertToIntSafe(input, defaultNumber)); }