Example #1
0
        /// <summary>
        /// Writes out a message to all SignalR clients
        /// </summary>
        /// <param name="queueItem"></param>
        /// <param name="elapsed"></param>
        /// <param name="waiting"></param>
        public static void WriteMessage(QueueMessageItem queueItem,
                                        int elapsed   = 0,
                                        int waiting   = -1,
                                        DateTime?time = null)
        {
            string elapsedString = string.Empty;

            if (elapsed > 0)
            {
                elapsedString = (Convert.ToDecimal(elapsed) / 1000).ToString("N2") + "s";
            }

            var msg = HtmlUtils.DisplayMemo(queueItem.Message);

            if (time == null)
            {
                time = DateTime.UtcNow;
            }

            // Write out message to SignalR clients
            HubContext.Clients.All.writeMessage(msg,
                                                queueItem.Status,
                                                time.Value.ToString("HH:mm:ss"),
                                                queueItem.Id,
                                                elapsedString,
                                                waiting, queueItem.QueueName);
        }
Example #2
0
        public string SaveMainComment(string snippetId, string comment)
        {
            busCodeSnippet busSnippet = new busCodeSnippet();

            if (busSnippet.Load(snippetId) == null)
            {
                throw new ArgumentException("Invalid snippetId passed.");
            }

            if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
            {
                throw new AccessViolationException("You are not allowed to edit this snippet.");
            }

            busSnippet.Entity.Comment = comment.Replace("\n", "\r\n");
            if (!busSnippet.Save())
            {
                throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
            }

            string tagResult = HtmlUtils.DisplayMemo(comment);

            return(tagResult);
        }
Example #3
0
        /// <summary>
        /// Returns and rss or atom feed
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        protected internal ActionResult GetFeed(object instance)
        {
            try
            {
                string title  = "CodePaste.NET";
                string action = this.RouteData.Values["listAction"] as string;
                if (string.IsNullOrEmpty(action))
                {
                    action = this.RouteData.Values["Action"] as string ?? string.Empty;
                }
                action = action.ToLower();

                if (action == "recent")
                {
                    title = "CodePaste.NET Recent Snippets";
                }
                else if (action == "mysnippets")
                {
                    title = "CodePaste.NET - My Snippets";
                }

                SyndicationFeed feed = new SyndicationFeed(title, "Paste and Link .NET Code", new Uri(Request.Url.AbsoluteUri));

                feed.BaseUri         = new Uri("http://codepaste.net/recent");
                feed.LastUpdatedTime = DateTime.Now;

                List <SyndicationItem> feedItems = new List <SyndicationItem>();
                foreach (CodeSnippetListItem item in (IEnumerable)instance)
                {
                    // remove lower ascii characters (< 32 exclude 9,10,13)
                    string code = Regex.Replace(item.Code, @"[\u0000-\u0008,\u000B,\u000C,\u000E-\u001F]", "");

                    SyndicationItem rssItem = new SyndicationItem()
                    {
                        Id      = item.Id,
                        Title   = SyndicationContent.CreateHtmlContent(item.Title),
                        Content = SyndicationContent.CreateHtmlContent(
                            //"<link href=\"" + WebUtils.ResolveServerUrl("~/css/csharp.css") + "\" rel=\"stylesheet\" type=\"text/css\">\r\n <style type='text/css'>.kwrd { color: blue; font-weight: bold }</style>\r\n" +
                            "<pre>\r\n" +
                            HtmlUtils.HtmlEncode(code) +
                            //snippet.GetFormattedCode(item.Code, item.Language, item.ShowLineNumbers) +
                            "</pre>"),
                        PublishDate = item.Entered,
                    };

                    if (!string.IsNullOrEmpty(item.Author))
                    {
                        rssItem.Authors.Add(new SyndicationPerson("", item.Author, null));
                    }

                    rssItem.Links.Add(new SyndicationLink(new Uri(WebUtils.GetFullApplicationPath() + "/" + item.Id),
                                                          "alternate", item.Title, "text/html", 1000));

                    feedItems.Add(rssItem);
                }

                feed.Items = feedItems;

                MemoryStream ms       = new MemoryStream();
                var          settings = new XmlWriterSettings()
                {
                    CheckCharacters = false
                };
                XmlWriter writer = XmlWriter.Create(ms, settings);
                if (this.Format == "rss")
                {
                    Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
                    rssFormatter.WriteTo(writer);
                }
                else
                {
                    Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(feed);
                    atomFormatter.WriteTo(writer);
                }
                writer.Flush();

                ms.Position = 0;

                return(this.Content(Encoding.UTF8.GetString(ms.ToArray()), "application/xml"));
            }
            catch (Exception ex)
            {
                Response.Write(HtmlUtils.DisplayMemo("Error: " + ex.GetBaseException().Message + "\r\n" + ex.StackTrace));
                Response.End();
                return(null);
            }

            return(null);
        }