The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RSSController.cs 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using MarkdownDeep;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.ServiceModel.Syndication;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Xml.Linq;
  9. using Teknik.Areas.Blog.Models;
  10. using Teknik.Controllers;
  11. using Teknik.Filters;
  12. using Teknik.Utilities;
  13. using Teknik.Models;
  14. using Teknik.Attributes;
  15. namespace Teknik.Areas.RSS.Controllers
  16. {
  17. [TeknikAuthorize(AuthType.Basic)]
  18. public class RSSController : DefaultController
  19. {
  20. private TeknikEntities db = new TeknikEntities();
  21. [AllowAnonymous]
  22. public ActionResult Index()
  23. {
  24. SyndicationFeed feed = new SyndicationFeed("Teknik RSS", "RSS feeds for the Teknik Services.", new Uri(Url.SubRouteUrl("help", "Help.RSS")));
  25. return new RssResult(feed);
  26. }
  27. [TrackDownload]
  28. [AllowAnonymous]
  29. public ActionResult Blog(string username)
  30. {
  31. // If empty, grab the main blog
  32. Blog.Models.Blog blog = null;
  33. string blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
  34. string title = string.Empty;
  35. string description = string.Empty;
  36. bool isSystem = string.IsNullOrEmpty(username);
  37. if (isSystem)
  38. {
  39. blog = db.Blogs.Where(b => b.BlogId == Config.BlogConfig.ServerBlogId).FirstOrDefault();
  40. blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
  41. }
  42. else
  43. {
  44. blog = db.Blogs.Where(b => b.User.Username == username).FirstOrDefault();
  45. blogUrl = Url.SubRouteUrl("blog", "Blog.Blog", new { username = username });
  46. }
  47. if (blog != null)
  48. {
  49. if (isSystem)
  50. {
  51. title = Config.BlogConfig.Title;
  52. description = Config.BlogConfig.Description;
  53. }
  54. else
  55. {
  56. title = blog.User.BlogSettings.Title;
  57. description = blog.User.BlogSettings.Description;
  58. }
  59. List<SyndicationItem> items = new List<SyndicationItem>();
  60. foreach (BlogPost post in blog.BlogPosts.OrderByDescending(p => p.BlogPostId))
  61. {
  62. if (post.Published && post.System == isSystem)
  63. {
  64. items.Add(new SyndicationItem(
  65. post.Title,
  66. MarkdownHelper.Markdown(post.Article).ToHtmlString(),
  67. new Uri(Url.SubRouteUrl("blog", "Blog.Post", new { username = post.Blog.User.Username, id = post.BlogPostId })),
  68. post.BlogPostId.ToString(),
  69. post.DateEdited
  70. ));
  71. }
  72. }
  73. SyndicationFeed feed = new SyndicationFeed(title, description, new Uri(blogUrl), items);
  74. return new RssResult(feed);
  75. }
  76. SyndicationFeed badFeed = new SyndicationFeed("No Blog Available", "The specified blog does not exist", new Uri(blogUrl));
  77. return new RssResult(badFeed);
  78. }
  79. [TrackDownload]
  80. [AllowAnonymous]
  81. public ActionResult Podcast()
  82. {
  83. List<SyndicationItem> items = new List<SyndicationItem>();
  84. List<Podcast.Models.Podcast> podcasts = db.Podcasts.Where(p => p.Published).OrderByDescending(p => p.Episode).ToList();
  85. if (podcasts != null)
  86. {
  87. foreach (Podcast.Models.Podcast podcast in podcasts)
  88. {
  89. SyndicationItem item = new SyndicationItem(
  90. podcast.Title,
  91. MarkdownHelper.Markdown(podcast.Description).ToHtmlString(),
  92. new Uri(Url.SubRouteUrl("podcast", "Podcast.View", new { episode = podcast.Episode })),
  93. podcast.Episode.ToString(),
  94. podcast.DateEdited
  95. );
  96. foreach (Podcast.Models.PodcastFile file in podcast.Files)
  97. {
  98. SyndicationLink enclosure = SyndicationLink.CreateMediaEnclosureLink(new Uri(Url.SubRouteUrl("podcast", "Podcast.Download", new { episode = podcast.Episode, fileName = file.FileName })), file.ContentType, file.ContentLength);
  99. item.Links.Add(enclosure);
  100. }
  101. items.Add(item);
  102. }
  103. }
  104. SyndicationFeed feed = new SyndicationFeed(Config.PodcastConfig.Title, Config.PodcastConfig.Description, new Uri(Url.SubRouteUrl("podcast", "Podcast.Index")), items);
  105. return new RssResult(feed);
  106. }
  107. }
  108. }