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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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.PodcastId = foundPodcast.PodcastId;
  57. model.Episode = foundPodcast.Episode;
  58. model.Title = foundPodcast.Title;
  59. model.Description = foundPodcast.Description;
  60. model.Files = foundPodcast.Files;
  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. [ValidateAntiForgeryToken]
  149. public ActionResult CreatePodcast(int episode, string title, string description)
  150. {
  151. if (ModelState.IsValid)
  152. {
  153. if (User.IsInRole("Podcast"))
  154. {
  155. // Grab the next episode number
  156. Models.Podcast lastPod = db.Podcasts.Where(p => p.Episode == episode).FirstOrDefault();
  157. if (lastPod == null)
  158. {
  159. if (Request.Files.Count > 0)
  160. {
  161. // Create the podcast object
  162. Models.Podcast podcast = db.Podcasts.Create();
  163. podcast.Episode = episode;
  164. podcast.Title = title;
  165. podcast.Description = description;
  166. podcast.DatePosted = DateTime.Now;
  167. podcast.DatePublished = DateTime.Now;
  168. // Handle saving of files
  169. for (int i = 0; i < Request.Files.Count; i++)
  170. {
  171. HttpPostedFileBase file = Request.Files[i]; //Uploaded file
  172. //Use the following properties to get file's name, size and MIMEType
  173. int fileSize = file.ContentLength;
  174. string fileName = file.FileName;
  175. string mimeType = file.ContentType;
  176. string fileExt = Path.GetExtension(fileName);
  177. if (!Directory.Exists(Config.PodcastConfig.PodcastDirectory))
  178. {
  179. Directory.CreateDirectory(Config.PodcastConfig.PodcastDirectory);
  180. }
  181. string newName = string.Format("Teknikast_Episode_{0}{1}", episode, fileExt);
  182. int index = 1;
  183. while (System.IO.File.Exists(Path.Combine(Config.PodcastConfig.PodcastDirectory, newName)))
  184. {
  185. newName = string.Format("Teknikast_Episode_{0} ({1}){2}", episode, index, fileExt);
  186. index++;
  187. }
  188. string fullPath = Path.Combine(Config.PodcastConfig.PodcastDirectory, newName);
  189. PodcastFile podFile = new PodcastFile();
  190. podFile.Path = fullPath;
  191. podFile.FileName = newName;
  192. podFile.ContentType = mimeType;
  193. podcast.Files = new List<PodcastFile>();
  194. podcast.Files.Add(podFile);
  195. file.SaveAs(fullPath);
  196. }
  197. db.Podcasts.Add(podcast);
  198. db.SaveChanges();
  199. return Json(new { result = true });
  200. }
  201. return Json(new { error = "You must submit at least one podcast audio file" });
  202. }
  203. return Json(new { error = "That episode already exists" });
  204. }
  205. return Json(new { error = "You don't have permission to create a podcast" });
  206. }
  207. return Json(new { error = "No podcast created" });
  208. }
  209. [HttpPost]
  210. [ValidateAntiForgeryToken]
  211. public ActionResult EditPodcast(int podcastId, int episode, string title, string description)
  212. {
  213. if (ModelState.IsValid)
  214. {
  215. if (User.IsInRole("Podcast"))
  216. {
  217. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  218. if (podcast != null)
  219. {
  220. if (db.Podcasts.Where(p => p.Episode != episode).FirstOrDefault() == null)
  221. {
  222. podcast.Episode = episode;
  223. podcast.Title = title;
  224. podcast.Description = description;
  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. [ValidateAntiForgeryToken]
  239. public ActionResult PublishPodcast(int podcastId, bool publish)
  240. {
  241. if (ModelState.IsValid)
  242. {
  243. if (User.IsInRole("Podcast"))
  244. {
  245. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  246. if (podcast != null)
  247. {
  248. podcast.Published = publish;
  249. if (publish)
  250. podcast.DatePublished = DateTime.Now;
  251. db.Entry(podcast).State = EntityState.Modified;
  252. db.SaveChanges();
  253. return Json(new { result = true });
  254. }
  255. return Json(new { error = "No podcast found" });
  256. }
  257. return Json(new { error = "You don't have permission to publish this podcast" });
  258. }
  259. return Json(new { error = "Invalid Inputs" });
  260. }
  261. [HttpPost]
  262. [ValidateAntiForgeryToken]
  263. public ActionResult DeletePodcast(int podcastId)
  264. {
  265. if (ModelState.IsValid)
  266. {
  267. if (User.IsInRole("Podcast"))
  268. {
  269. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  270. if (podcast != null)
  271. {
  272. db.Podcasts.Remove(podcast);
  273. db.SaveChanges();
  274. return Json(new { result = true });
  275. }
  276. return Json(new { error = "No podcast found" });
  277. }
  278. return Json(new { error = "You don't have permission to delete this podcast" });
  279. }
  280. return Json(new { error = "Invalid Inputs" });
  281. }
  282. #endregion
  283. #region Comments
  284. [HttpPost]
  285. [AllowAnonymous]
  286. public ActionResult GetComments(int podcastId, int startCommentID, int count)
  287. {
  288. var comments = db.PodcastComments.Include("Podcast").Where(p => (p.PodcastId == podcastId)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
  289. List<CommentViewModel> commentViews = new List<CommentViewModel>();
  290. if (comments != null)
  291. {
  292. foreach (Models.PodcastComment comment in comments)
  293. {
  294. commentViews.Add(new CommentViewModel(comment));
  295. }
  296. }
  297. return PartialView("~/Areas/Podcast/Views/Podcast/Comments.cshtml", commentViews);
  298. }
  299. [HttpPost]
  300. [AllowAnonymous]
  301. public ActionResult GetCommentArticle(int commentID)
  302. {
  303. Models.PodcastComment comment = db.PodcastComments.Include("Podcast").Where(p => (p.PodcastCommentId == commentID)).First();
  304. if (comment != null)
  305. {
  306. return Json(new { result = comment.Article });
  307. }
  308. return Json(new { error = "No article found" });
  309. }
  310. [HttpPost]
  311. [ValidateAntiForgeryToken]
  312. public ActionResult CreateComment(int podcastId, string article)
  313. {
  314. if (ModelState.IsValid)
  315. {
  316. if (db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault() != null)
  317. {
  318. Models.PodcastComment comment = db.PodcastComments.Create();
  319. comment.PodcastId = podcastId;
  320. comment.UserId = db.Users.Where(u => u.Username == User.Identity.Name).First().UserId;
  321. comment.Article = article;
  322. comment.DatePosted = DateTime.Now;
  323. db.PodcastComments.Add(comment);
  324. db.SaveChanges();
  325. return Json(new { result = true });
  326. }
  327. return Json(new { error = "That podcast does not exist" });
  328. }
  329. return Json(new { error = "Invalid Parameters" });
  330. }
  331. [HttpPost]
  332. [ValidateAntiForgeryToken]
  333. public ActionResult EditComment(int commentID, string article)
  334. {
  335. if (ModelState.IsValid)
  336. {
  337. Models.PodcastComment comment = db.PodcastComments.Find(commentID);
  338. if (comment != null)
  339. {
  340. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  341. {
  342. comment.Article = article;
  343. db.Entry(comment).State = EntityState.Modified;
  344. db.SaveChanges();
  345. return Json(new { result = true });
  346. }
  347. return Json(new { error = "You don't have permission to edit this comment" });
  348. }
  349. return Json(new { error = "No comment found" });
  350. }
  351. return Json(new { error = "Invalid Parameters" });
  352. }
  353. [HttpPost]
  354. [ValidateAntiForgeryToken]
  355. public ActionResult DeleteComment(int commentID)
  356. {
  357. if (ModelState.IsValid)
  358. {
  359. Models.PodcastComment comment = db.PodcastComments.Find(commentID);
  360. if (comment != null)
  361. {
  362. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  363. {
  364. db.PodcastComments.Remove(comment);
  365. db.SaveChanges();
  366. return Json(new { result = true });
  367. }
  368. return Json(new { error = "You don't have permission to delete this comment" });
  369. }
  370. return Json(new { error = "No comment found" });
  371. }
  372. return Json(new { error = "Invalid Parameters" });
  373. }
  374. #endregion
  375. }
  376. }