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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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.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.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.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.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.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.ElementAt(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. foreach (PodcastFile file in newFiles)
  237. {
  238. podcast.Files.Add(file);
  239. }
  240. // Save podcast
  241. db.Entry(podcast).State = EntityState.Modified;
  242. db.SaveChanges();
  243. return Json(new { result = true });
  244. }
  245. return Json(new { error = "That episode already exists" });
  246. }
  247. return Json(new { error = "No podcast found" });
  248. }
  249. return Json(new { error = "You don't have permission to edit this podcast" });
  250. }
  251. return Json(new { error = "Invalid Inputs" });
  252. }
  253. [HttpPost]
  254. public ActionResult PublishPodcast(int podcastId, bool publish)
  255. {
  256. if (ModelState.IsValid)
  257. {
  258. if (User.IsInRole("Podcast"))
  259. {
  260. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  261. if (podcast != null)
  262. {
  263. podcast.Published = publish;
  264. if (publish)
  265. podcast.DatePublished = DateTime.Now;
  266. db.Entry(podcast).State = EntityState.Modified;
  267. db.SaveChanges();
  268. return Json(new { result = true });
  269. }
  270. return Json(new { error = "No podcast found" });
  271. }
  272. return Json(new { error = "You don't have permission to publish this podcast" });
  273. }
  274. return Json(new { error = "Invalid Inputs" });
  275. }
  276. [HttpPost]
  277. public ActionResult DeletePodcast(int podcastId)
  278. {
  279. if (ModelState.IsValid)
  280. {
  281. if (User.IsInRole("Podcast"))
  282. {
  283. Models.Podcast podcast = db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault();
  284. if (podcast != null)
  285. {
  286. foreach (PodcastFile file in podcast.Files)
  287. {
  288. System.IO.File.Delete(file.Path);
  289. }
  290. db.Podcasts.Remove(podcast);
  291. db.SaveChanges();
  292. return Json(new { result = true });
  293. }
  294. return Json(new { error = "No podcast found" });
  295. }
  296. return Json(new { error = "You don't have permission to delete this podcast" });
  297. }
  298. return Json(new { error = "Invalid Inputs" });
  299. }
  300. #endregion
  301. #region Comments
  302. [HttpPost]
  303. [AllowAnonymous]
  304. public ActionResult GetComments(int podcastId, int startCommentID, int count)
  305. {
  306. var comments = db.PodcastComments.Where(p => (p.PodcastId == podcastId)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
  307. List<CommentViewModel> commentViews = new List<CommentViewModel>();
  308. if (comments != null)
  309. {
  310. foreach (PodcastComment comment in comments)
  311. {
  312. commentViews.Add(new CommentViewModel(comment));
  313. }
  314. }
  315. return PartialView("~/Areas/Podcast/Views/Podcast/Comments.cshtml", commentViews);
  316. }
  317. [HttpPost]
  318. [AllowAnonymous]
  319. public ActionResult GetCommentArticle(int commentID)
  320. {
  321. PodcastComment comment = db.PodcastComments.Where(p => (p.PodcastCommentId == commentID)).FirstOrDefault();
  322. if (comment != null)
  323. {
  324. return Json(new { result = comment.Article });
  325. }
  326. return Json(new { error = "No article found" });
  327. }
  328. [HttpPost]
  329. public ActionResult CreateComment(int podcastId, string article)
  330. {
  331. if (ModelState.IsValid)
  332. {
  333. if (db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault() != null)
  334. {
  335. PodcastComment comment = db.PodcastComments.Create();
  336. comment.PodcastId = podcastId;
  337. comment.UserId = db.Users.Where(u => u.Username == User.Identity.Name).First().UserId;
  338. comment.Article = article;
  339. comment.DatePosted = DateTime.Now;
  340. comment.DateEdited = DateTime.Now;
  341. db.PodcastComments.Add(comment);
  342. db.SaveChanges();
  343. return Json(new { result = true });
  344. }
  345. return Json(new { error = "That podcast does not exist" });
  346. }
  347. return Json(new { error = "Invalid Parameters" });
  348. }
  349. [HttpPost]
  350. public ActionResult EditComment(int commentID, string article)
  351. {
  352. if (ModelState.IsValid)
  353. {
  354. PodcastComment comment = db.PodcastComments.Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  355. if (comment != null)
  356. {
  357. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  358. {
  359. comment.Article = article;
  360. comment.DateEdited = DateTime.Now;
  361. db.Entry(comment).State = EntityState.Modified;
  362. db.SaveChanges();
  363. return Json(new { result = true });
  364. }
  365. return Json(new { error = "You don't have permission to edit this comment" });
  366. }
  367. return Json(new { error = "No comment found" });
  368. }
  369. return Json(new { error = "Invalid Parameters" });
  370. }
  371. [HttpPost]
  372. public ActionResult DeleteComment(int commentID)
  373. {
  374. if (ModelState.IsValid)
  375. {
  376. PodcastComment comment = db.PodcastComments.Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  377. if (comment != null)
  378. {
  379. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  380. {
  381. db.PodcastComments.Remove(comment);
  382. db.SaveChanges();
  383. return Json(new { result = true });
  384. }
  385. return Json(new { error = "You don't have permission to delete this comment" });
  386. }
  387. return Json(new { error = "No comment found" });
  388. }
  389. return Json(new { error = "Invalid Parameters" });
  390. }
  391. #endregion
  392. public List<PodcastFile> SaveFiles(HttpFileCollectionBase files, int episode)
  393. {
  394. List<PodcastFile> podFiles = new List<PodcastFile>();
  395. if (files.Count > 0)
  396. {
  397. for (int i = 0; i < files.Count; i++)
  398. {
  399. HttpPostedFileBase file = files[i];
  400. int fileSize = file.ContentLength;
  401. string fileName = file.FileName;
  402. string fileExt = Path.GetExtension(fileName);
  403. if (!Directory.Exists(Config.PodcastConfig.PodcastDirectory))
  404. {
  405. Directory.CreateDirectory(Config.PodcastConfig.PodcastDirectory);
  406. }
  407. string newName = string.Format("Teknikast_Episode_{0}{1}", episode, fileExt);
  408. int index = 1;
  409. while (System.IO.File.Exists(Path.Combine(Config.PodcastConfig.PodcastDirectory, newName)))
  410. {
  411. newName = string.Format("Teknikast_Episode_{0} ({1}){2}", episode, index, fileExt);
  412. index++;
  413. }
  414. string fullPath = Path.Combine(Config.PodcastConfig.PodcastDirectory, newName);
  415. PodcastFile podFile = new PodcastFile();
  416. podFile.Path = fullPath;
  417. podFile.FileName = newName;
  418. podFile.ContentType = file.ContentType;
  419. podFile.ContentLength = file.ContentLength;
  420. podFiles.Add(podFile);
  421. file.SaveAs(fullPath);
  422. }
  423. }
  424. return podFiles;
  425. }
  426. }
  427. }