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

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