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.

PodcastController.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using Teknik.Areas.Podcast.Models;
  9. using Teknik.Areas.Podcast.ViewModels;
  10. using Teknik.Controllers;
  11. using Teknik.Models;
  12. namespace Teknik.Areas.Podcast.Controllers
  13. {
  14. public class PodcastController : DefaultController
  15. {
  16. private TeknikEntities db = new TeknikEntities();
  17. [AllowAnonymous]
  18. public ActionResult Index()
  19. {
  20. MainViewModel model = new MainViewModel();
  21. model.Title = Config.PodcastConfig.Title;
  22. model.Description = Config.PodcastConfig.Description;
  23. try
  24. {
  25. ViewBag.Title = "Teknikast - " + Config.Title;
  26. bool editor = User.IsInRole("Podcast");
  27. var foundPodcasts = db.Podcasts.Where(p => (p.Published || editor)).FirstOrDefault();
  28. if (foundPodcasts != null)
  29. {
  30. model.HasPodcasts = (foundPodcasts != null);
  31. }
  32. else
  33. {
  34. model.Error = true;
  35. model.ErrorMessage = "No Podcasts Available";
  36. }
  37. return View("~/Areas/Podcast/Views/Podcast/Main.cshtml", model);
  38. }
  39. catch (Exception ex)
  40. {
  41. model.Error = true;
  42. model.ErrorMessage = ex.Message;
  43. return View("~/Areas/Podcast/Views/Podcast/Main.cshtml", model);
  44. }
  45. }
  46. #region Podcasts
  47. [AllowAnonymous]
  48. public ActionResult View(int episode)
  49. {
  50. PodcastViewModel model = new PodcastViewModel();
  51. // find the podcast specified
  52. bool editor = User.IsInRole("Podcast");
  53. var foundPodcast = db.Podcasts.Include("Files").Where(p => ((p.Published || editor) && p.Episode == episode)).FirstOrDefault();
  54. if (foundPodcast != null)
  55. {
  56. model = new PodcastViewModel(foundPodcast);
  57. ViewBag.Title = model.Title + " - Teknikast - " + Config.Title;
  58. return View("~/Areas/Podcast/Views/Podcast/ViewPodcast.cshtml", model);
  59. }
  60. model.Error = true;
  61. model.ErrorMessage = "No Podcasts Available";
  62. return View("~/Areas/Podcast/Views/Podcast/ViewPodcast.cshtml", model);
  63. }
  64. [AllowAnonymous]
  65. public ActionResult Download(int episode, string fileName)
  66. {
  67. // find the podcast specified
  68. var foundPodcast = db.Podcasts.Include("Files").Where(p => (p.Published && p.Episode == episode)).FirstOrDefault();
  69. if (foundPodcast != null)
  70. {
  71. PodcastFile file = foundPodcast.Files.Where(f => f.FileName == fileName).FirstOrDefault();
  72. if (file != null)
  73. {
  74. if (System.IO.File.Exists(file.Path))
  75. {
  76. // Read in the file
  77. byte[] data = System.IO.File.ReadAllBytes(file.Path);
  78. // Create File
  79. var cd = new System.Net.Mime.ContentDisposition
  80. {
  81. FileName = file.FileName,
  82. Inline = true
  83. };
  84. Response.AppendHeader("Content-Disposition", cd.ToString());
  85. return File(data, file.ContentType);
  86. }
  87. }
  88. }
  89. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  90. }
  91. [HttpPost]
  92. [AllowAnonymous]
  93. public ActionResult GetPodcasts(int startPodcastID, int count)
  94. {
  95. bool editor = User.IsInRole("Podcast");
  96. var podcasts = db.Podcasts.Include("Files").Where(p => p.Published || editor).OrderByDescending(p => p.DatePosted).Skip(startPodcastID).Take(count).ToList();
  97. List<PodcastViewModel> podcastViews = new List<PodcastViewModel>();
  98. if (podcasts != null)
  99. {
  100. foreach (Models.Podcast podcast in podcasts)
  101. {
  102. podcastViews.Add(new PodcastViewModel(podcast));
  103. }
  104. }
  105. return PartialView("~/Areas/Podcast/Views/Podcast/Podcasts.cshtml", podcastViews);
  106. }
  107. [HttpPost]
  108. [AllowAnonymous]
  109. public ActionResult GetPodcastEpisode(int podcastId)
  110. {
  111. bool editor = User.IsInRole("Podcast");
  112. var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
  113. if (foundPodcast != null)
  114. {
  115. return Json(new { result = foundPodcast.Episode });
  116. }
  117. return Json(new { error = "No podcast found" });
  118. }
  119. [HttpPost]
  120. [AllowAnonymous]
  121. public ActionResult GetPodcastTitle(int podcastId)
  122. {
  123. bool editor = User.IsInRole("Podcast");
  124. var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
  125. if (foundPodcast != null)
  126. {
  127. return Json(new { result = foundPodcast.Title });
  128. }
  129. return Json(new { error = "No podcast found" });
  130. }
  131. [HttpPost]
  132. [AllowAnonymous]
  133. public ActionResult GetPodcastDescription(int podcastId)
  134. {
  135. bool editor = User.IsInRole("Podcast");
  136. var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
  137. if (foundPodcast != null)
  138. {
  139. return Json(new { result = foundPodcast.Description });
  140. }
  141. return Json(new { error = "No podcast found" });
  142. }
  143. [HttpPost]
  144. public ActionResult CreatePodcast(int episode, string title, string description)
  145. {
  146. if (ModelState.IsValid)
  147. {
  148. if (User.IsInRole("Podcast"))
  149. {
  150. // Grab the next episode number
  151. Models.Podcast lastPod = db.Podcasts.Where(p => p.Episode == episode).FirstOrDefault();
  152. if (lastPod == null)
  153. {
  154. // Create the podcast object
  155. Models.Podcast podcast = db.Podcasts.Create();
  156. podcast.Episode = episode;
  157. podcast.Title = title;
  158. podcast.Description = description;
  159. podcast.DatePosted = DateTime.Now;
  160. podcast.DatePublished = DateTime.Now;
  161. podcast.DateEdited = DateTime.Now;
  162. podcast.Files = SaveFiles(Request.Files, episode);
  163. db.Podcasts.Add(podcast);
  164. db.SaveChanges();
  165. return Json(new { result = true });
  166. }
  167. return Json(new { error = "That episode already exists" });
  168. }
  169. return Json(new { error = "You don't have permission to create a podcast" });
  170. }
  171. return Json(new { error = "No podcast created" });
  172. }
  173. [HttpPost]
  174. public ActionResult EditPodcast(int podcastId, int episode, string title, string description)
  175. {
  176. if (ModelState.IsValid)
  177. {
  178. if (User.IsInRole("Podcast"))
  179. {
  180. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  181. if (podcast != null)
  182. {
  183. if (db.Podcasts.Where(p => p.Episode != episode).FirstOrDefault() == null)
  184. {
  185. podcast.Episode = episode;
  186. podcast.Title = title;
  187. podcast.Description = description;
  188. podcast.DateEdited = DateTime.Now;
  189. List<PodcastFile> newFiles = SaveFiles(Request.Files, episode);
  190. podcast.Files.AddRange(newFiles);
  191. db.Entry(podcast).State = EntityState.Modified;
  192. db.SaveChanges();
  193. return Json(new { result = true });
  194. }
  195. return Json(new { error = "That episode already exists" });
  196. }
  197. return Json(new { error = "No podcast found" });
  198. }
  199. return Json(new { error = "You don't have permission to edit this podcast" });
  200. }
  201. return Json(new { error = "Invalid Inputs" });
  202. }
  203. [HttpPost]
  204. public ActionResult PublishPodcast(int podcastId, bool publish)
  205. {
  206. if (ModelState.IsValid)
  207. {
  208. if (User.IsInRole("Podcast"))
  209. {
  210. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  211. if (podcast != null)
  212. {
  213. podcast.Published = publish;
  214. if (publish)
  215. podcast.DatePublished = DateTime.Now;
  216. db.Entry(podcast).State = EntityState.Modified;
  217. db.SaveChanges();
  218. return Json(new { result = true });
  219. }
  220. return Json(new { error = "No podcast found" });
  221. }
  222. return Json(new { error = "You don't have permission to publish this podcast" });
  223. }
  224. return Json(new { error = "Invalid Inputs" });
  225. }
  226. [HttpPost]
  227. public ActionResult DeletePodcast(int podcastId)
  228. {
  229. if (ModelState.IsValid)
  230. {
  231. if (User.IsInRole("Podcast"))
  232. {
  233. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  234. if (podcast != null)
  235. {
  236. db.Podcasts.Remove(podcast);
  237. db.SaveChanges();
  238. return Json(new { result = true });
  239. }
  240. return Json(new { error = "No podcast found" });
  241. }
  242. return Json(new { error = "You don't have permission to delete this podcast" });
  243. }
  244. return Json(new { error = "Invalid Inputs" });
  245. }
  246. #endregion
  247. #region Comments
  248. [HttpPost]
  249. [AllowAnonymous]
  250. public ActionResult GetComments(int podcastId, int startCommentID, int count)
  251. {
  252. var comments = db.PodcastComments.Include("BlogPost").Include("BlogPost.Blog").Include("BlogPost.Blog.User").Include("User").Where(p => (p.PodcastId == podcastId)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
  253. List<CommentViewModel> commentViews = new List<CommentViewModel>();
  254. if (comments != null)
  255. {
  256. foreach (PodcastComment comment in comments)
  257. {
  258. commentViews.Add(new CommentViewModel(comment));
  259. }
  260. }
  261. return PartialView("~/Areas/Podcast/Views/Podcast/Comments.cshtml", commentViews);
  262. }
  263. [HttpPost]
  264. [AllowAnonymous]
  265. public ActionResult GetCommentArticle(int commentID)
  266. {
  267. PodcastComment comment = db.PodcastComments.Where(p => (p.PodcastCommentId == commentID)).FirstOrDefault();
  268. if (comment != null)
  269. {
  270. return Json(new { result = comment.Article });
  271. }
  272. return Json(new { error = "No article found" });
  273. }
  274. [HttpPost]
  275. public ActionResult CreateComment(int podcastId, string article)
  276. {
  277. if (ModelState.IsValid)
  278. {
  279. if (db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault() != null)
  280. {
  281. PodcastComment comment = db.PodcastComments.Create();
  282. comment.PodcastId = podcastId;
  283. comment.UserId = db.Users.Where(u => u.Username == User.Identity.Name).First().UserId;
  284. comment.Article = article;
  285. comment.DatePosted = DateTime.Now;
  286. comment.DateEdited = DateTime.Now;
  287. db.PodcastComments.Add(comment);
  288. db.SaveChanges();
  289. return Json(new { result = true });
  290. }
  291. return Json(new { error = "That podcast does not exist" });
  292. }
  293. return Json(new { error = "Invalid Parameters" });
  294. }
  295. [HttpPost]
  296. public ActionResult EditComment(int commentID, string article)
  297. {
  298. if (ModelState.IsValid)
  299. {
  300. PodcastComment comment = db.PodcastComments.Include("User").Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  301. if (comment != null)
  302. {
  303. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  304. {
  305. comment.Article = article;
  306. comment.DateEdited = DateTime.Now;
  307. db.Entry(comment).State = EntityState.Modified;
  308. db.SaveChanges();
  309. return Json(new { result = true });
  310. }
  311. return Json(new { error = "You don't have permission to edit this comment" });
  312. }
  313. return Json(new { error = "No comment found" });
  314. }
  315. return Json(new { error = "Invalid Parameters" });
  316. }
  317. [HttpPost]
  318. public ActionResult DeleteComment(int commentID)
  319. {
  320. if (ModelState.IsValid)
  321. {
  322. PodcastComment comment = db.PodcastComments.Include("User").Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  323. if (comment != null)
  324. {
  325. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  326. {
  327. db.PodcastComments.Remove(comment);
  328. db.SaveChanges();
  329. return Json(new { result = true });
  330. }
  331. return Json(new { error = "You don't have permission to delete this comment" });
  332. }
  333. return Json(new { error = "No comment found" });
  334. }
  335. return Json(new { error = "Invalid Parameters" });
  336. }
  337. #endregion
  338. public List<PodcastFile> SaveFiles(HttpFileCollectionBase files, int episode)
  339. {
  340. List<PodcastFile> podFiles = new List<PodcastFile>();
  341. if (files.Count > 0)
  342. {
  343. for (int i = 0; i < Request.Files.Count; i++)
  344. {
  345. HttpPostedFileBase file = Request.Files[i];
  346. int fileSize = file.ContentLength;
  347. string fileName = file.FileName;
  348. string fileExt = Path.GetExtension(fileName);
  349. if (!Directory.Exists(Config.PodcastConfig.PodcastDirectory))
  350. {
  351. Directory.CreateDirectory(Config.PodcastConfig.PodcastDirectory);
  352. }
  353. string newName = string.Format("Teknikast_Episode_{0}{1}", episode, fileExt);
  354. int index = 1;
  355. while (System.IO.File.Exists(Path.Combine(Config.PodcastConfig.PodcastDirectory, newName)))
  356. {
  357. newName = string.Format("Teknikast_Episode_{0} ({1}){2}", episode, index, fileExt);
  358. index++;
  359. }
  360. string fullPath = Path.Combine(Config.PodcastConfig.PodcastDirectory, newName);
  361. PodcastFile podFile = new PodcastFile();
  362. podFile.Path = fullPath;
  363. podFile.FileName = newName;
  364. podFile.ContentType = file.ContentType;
  365. podFile.ContentLength = file.ContentLength;
  366. podFiles.Add(podFile);
  367. file.SaveAs(fullPath);
  368. }
  369. }
  370. return podFiles;
  371. }
  372. }
  373. }