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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. [AllowAnonymous]
  145. public ActionResult GetPodcastFiles(int podcastId)
  146. {
  147. bool editor = User.IsInRole("Podcast");
  148. var foundPodcast = db.Podcasts.Include("Files").Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
  149. if (foundPodcast != null)
  150. {
  151. List<object> files = new List<object>();
  152. foreach (PodcastFile file in foundPodcast.Files)
  153. {
  154. object fileObj = new
  155. {
  156. name = file.FileName,
  157. id = file.PodcastFileId
  158. };
  159. files.Add(fileObj);
  160. }
  161. return Json(new { result = new { files = files } });
  162. }
  163. return Json(new { error = "No podcast found" });
  164. }
  165. [HttpPost]
  166. public ActionResult CreatePodcast(int episode, string title, string description)
  167. {
  168. if (ModelState.IsValid)
  169. {
  170. if (User.IsInRole("Podcast"))
  171. {
  172. // Grab the next episode number
  173. Models.Podcast lastPod = db.Podcasts.Where(p => p.Episode == episode).FirstOrDefault();
  174. if (lastPod == null)
  175. {
  176. // Create the podcast object
  177. Models.Podcast podcast = db.Podcasts.Create();
  178. podcast.Episode = episode;
  179. podcast.Title = title;
  180. podcast.Description = description;
  181. podcast.DatePosted = DateTime.Now;
  182. podcast.DatePublished = DateTime.Now;
  183. podcast.DateEdited = DateTime.Now;
  184. podcast.Files = SaveFiles(Request.Files, episode);
  185. db.Podcasts.Add(podcast);
  186. db.SaveChanges();
  187. return Json(new { result = true });
  188. }
  189. return Json(new { error = "That episode already exists" });
  190. }
  191. return Json(new { error = "You don't have permission to create a podcast" });
  192. }
  193. return Json(new { error = "No podcast created" });
  194. }
  195. [HttpPost]
  196. public ActionResult EditPodcast(int podcastId, int episode, string title, string description, string fileIds)
  197. {
  198. if (ModelState.IsValid)
  199. {
  200. if (User.IsInRole("Podcast"))
  201. {
  202. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  203. if (podcast != null)
  204. {
  205. if (db.Podcasts.Where(p => p.Episode != episode).FirstOrDefault() == null)
  206. {
  207. podcast.Episode = episode;
  208. podcast.Title = title;
  209. podcast.Description = description;
  210. podcast.DateEdited = DateTime.Now;
  211. // Remove any files not in fileIds
  212. List<string> fileIdList = fileIds.Split(',').ToList();
  213. for (int i = 0; i < podcast.Files.Count; i++)
  214. {
  215. PodcastFile curFile = podcast.Files[i];
  216. if (!fileIdList.Exists(id => id == curFile.PodcastFileId.ToString()))
  217. {
  218. podcast.Files.Remove(curFile);
  219. }
  220. }
  221. // Add any new files
  222. List<PodcastFile> newFiles = SaveFiles(Request.Files, episode);
  223. podcast.Files.AddRange(newFiles);
  224. // Save podcast
  225. db.Entry(podcast).State = EntityState.Modified;
  226. db.SaveChanges();
  227. return Json(new { result = true });
  228. }
  229. return Json(new { error = "That episode already exists" });
  230. }
  231. return Json(new { error = "No podcast found" });
  232. }
  233. return Json(new { error = "You don't have permission to edit this podcast" });
  234. }
  235. return Json(new { error = "Invalid Inputs" });
  236. }
  237. [HttpPost]
  238. public ActionResult PublishPodcast(int podcastId, bool publish)
  239. {
  240. if (ModelState.IsValid)
  241. {
  242. if (User.IsInRole("Podcast"))
  243. {
  244. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  245. if (podcast != null)
  246. {
  247. podcast.Published = publish;
  248. if (publish)
  249. podcast.DatePublished = DateTime.Now;
  250. db.Entry(podcast).State = EntityState.Modified;
  251. db.SaveChanges();
  252. return Json(new { result = true });
  253. }
  254. return Json(new { error = "No podcast found" });
  255. }
  256. return Json(new { error = "You don't have permission to publish this podcast" });
  257. }
  258. return Json(new { error = "Invalid Inputs" });
  259. }
  260. [HttpPost]
  261. public ActionResult DeletePodcast(int podcastId)
  262. {
  263. if (ModelState.IsValid)
  264. {
  265. if (User.IsInRole("Podcast"))
  266. {
  267. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  268. if (podcast != null)
  269. {
  270. db.Podcasts.Remove(podcast);
  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 delete this podcast" });
  277. }
  278. return Json(new { error = "Invalid Inputs" });
  279. }
  280. #endregion
  281. #region Comments
  282. [HttpPost]
  283. [AllowAnonymous]
  284. public ActionResult GetComments(int podcastId, int startCommentID, int count)
  285. {
  286. 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();
  287. List<CommentViewModel> commentViews = new List<CommentViewModel>();
  288. if (comments != null)
  289. {
  290. foreach (PodcastComment comment in comments)
  291. {
  292. commentViews.Add(new CommentViewModel(comment));
  293. }
  294. }
  295. return PartialView("~/Areas/Podcast/Views/Podcast/Comments.cshtml", commentViews);
  296. }
  297. [HttpPost]
  298. [AllowAnonymous]
  299. public ActionResult GetCommentArticle(int commentID)
  300. {
  301. PodcastComment comment = db.PodcastComments.Where(p => (p.PodcastCommentId == commentID)).FirstOrDefault();
  302. if (comment != null)
  303. {
  304. return Json(new { result = comment.Article });
  305. }
  306. return Json(new { error = "No article found" });
  307. }
  308. [HttpPost]
  309. public ActionResult CreateComment(int podcastId, string article)
  310. {
  311. if (ModelState.IsValid)
  312. {
  313. if (db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault() != null)
  314. {
  315. PodcastComment comment = db.PodcastComments.Create();
  316. comment.PodcastId = podcastId;
  317. comment.UserId = db.Users.Where(u => u.Username == User.Identity.Name).First().UserId;
  318. comment.Article = article;
  319. comment.DatePosted = DateTime.Now;
  320. comment.DateEdited = DateTime.Now;
  321. db.PodcastComments.Add(comment);
  322. db.SaveChanges();
  323. return Json(new { result = true });
  324. }
  325. return Json(new { error = "That podcast does not exist" });
  326. }
  327. return Json(new { error = "Invalid Parameters" });
  328. }
  329. [HttpPost]
  330. public ActionResult EditComment(int commentID, string article)
  331. {
  332. if (ModelState.IsValid)
  333. {
  334. PodcastComment comment = db.PodcastComments.Include("User").Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  335. if (comment != null)
  336. {
  337. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  338. {
  339. comment.Article = article;
  340. comment.DateEdited = DateTime.Now;
  341. db.Entry(comment).State = EntityState.Modified;
  342. db.SaveChanges();
  343. return Json(new { result = true });
  344. }
  345. return Json(new { error = "You don't have permission to edit this comment" });
  346. }
  347. return Json(new { error = "No comment found" });
  348. }
  349. return Json(new { error = "Invalid Parameters" });
  350. }
  351. [HttpPost]
  352. public ActionResult DeleteComment(int commentID)
  353. {
  354. if (ModelState.IsValid)
  355. {
  356. PodcastComment comment = db.PodcastComments.Include("User").Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  357. if (comment != null)
  358. {
  359. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  360. {
  361. db.PodcastComments.Remove(comment);
  362. db.SaveChanges();
  363. return Json(new { result = true });
  364. }
  365. return Json(new { error = "You don't have permission to delete this comment" });
  366. }
  367. return Json(new { error = "No comment found" });
  368. }
  369. return Json(new { error = "Invalid Parameters" });
  370. }
  371. #endregion
  372. public List<PodcastFile> SaveFiles(HttpFileCollectionBase files, int episode)
  373. {
  374. List<PodcastFile> podFiles = new List<PodcastFile>();
  375. if (files.Count > 0)
  376. {
  377. for (int i = 0; i < Request.Files.Count; i++)
  378. {
  379. HttpPostedFileBase file = Request.Files[i];
  380. int fileSize = file.ContentLength;
  381. string fileName = file.FileName;
  382. string fileExt = Path.GetExtension(fileName);
  383. if (!Directory.Exists(Config.PodcastConfig.PodcastDirectory))
  384. {
  385. Directory.CreateDirectory(Config.PodcastConfig.PodcastDirectory);
  386. }
  387. string newName = string.Format("Teknikast_Episode_{0}{1}", episode, fileExt);
  388. int index = 1;
  389. while (System.IO.File.Exists(Path.Combine(Config.PodcastConfig.PodcastDirectory, newName)))
  390. {
  391. newName = string.Format("Teknikast_Episode_{0} ({1}){2}", episode, index, fileExt);
  392. index++;
  393. }
  394. string fullPath = Path.Combine(Config.PodcastConfig.PodcastDirectory, newName);
  395. PodcastFile podFile = new PodcastFile();
  396. podFile.Path = fullPath;
  397. podFile.FileName = newName;
  398. podFile.ContentType = file.ContentType;
  399. podFile.ContentLength = file.ContentLength;
  400. podFiles.Add(podFile);
  401. file.SaveAs(fullPath);
  402. }
  403. }
  404. return podFiles;
  405. }
  406. }
  407. }