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 19KB

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