Пример #1
0
        /// <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());
        }