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

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