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 = 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. [ValidateAntiForgeryToken]
  145. public ActionResult CreatePodcast(int episode, string title, string description)
  146. {
  147. if (ModelState.IsValid)
  148. {
  149. if (User.IsInRole("Podcast"))
  150. {
  151. // Grab the next episode number
  152. Models.Podcast lastPod = db.Podcasts.Where(p => p.Episode == episode).FirstOrDefault();
  153. if (lastPod == null)
  154. {
  155. if (Request.Files.Count > 0)
  156. {
  157. // Create the podcast object
  158. Models.Podcast podcast = db.Podcasts.Create();
  159. podcast.Episode = episode;
  160. podcast.Title = title;
  161. podcast.Description = description;
  162. podcast.DatePosted = DateTime.Now;
  163. podcast.DatePublished = DateTime.Now;
  164. podcast.DateEdited = DateTime.Now;
  165. // Handle saving of files
  166. for (int i = 0; i < Request.Files.Count; i++)
  167. {
  168. HttpPostedFileBase file = Request.Files[i]; //Uploaded file
  169. //Use the following properties to get file's name, size and MIMEType
  170. int fileSize = file.ContentLength;
  171. string fileName = file.FileName;
  172. string fileExt = Path.GetExtension(fileName);
  173. if (!Directory.Exists(Config.PodcastConfig.PodcastDirectory))
  174. {
  175. Directory.CreateDirectory(Config.PodcastConfig.PodcastDirectory);
  176. }
  177. string newName = string.Format("Teknikast_Episode_{0}{1}", episode, fileExt);
  178. int index = 1;
  179. while (System.IO.File.Exists(Path.Combine(Config.PodcastConfig.PodcastDirectory, newName)))
  180. {
  181. newName = string.Format("Teknikast_Episode_{0} ({1}){2}", episode, index, fileExt);
  182. index++;
  183. }
  184. string fullPath = Path.Combine(Config.PodcastConfig.PodcastDirectory, newName);
  185. PodcastFile podFile = new PodcastFile();
  186. podFile.Path = fullPath;
  187. podFile.FileName = newName;
  188. podFile.ContentType = file.ContentType;
  189. podFile.ContentLength = file.ContentLength;
  190. podcast.Files = new List<PodcastFile>();
  191. podcast.Files.Add(podFile);
  192. file.SaveAs(fullPath);
  193. }
  194. db.Podcasts.Add(podcast);
  195. db.SaveChanges();
  196. return Json(new { result = true });
  197. }
  198. return Json(new { error = "You must submit at least one podcast audio file" });
  199. }
  200. return Json(new { error = "That episode already exists" });
  201. }
  202. return Json(new { error = "You don't have permission to create a podcast" });
  203. }
  204. return Json(new { error = "No podcast created" });
  205. }
  206. [HttpPost]
  207. [ValidateAntiForgeryToken]
  208. public ActionResult EditPodcast(int podcastId, int episode, string title, string description)
  209. {
  210. if (ModelState.IsValid)
  211. {
  212. if (User.IsInRole("Podcast"))
  213. {
  214. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  215. if (podcast != null)
  216. {
  217. if (db.Podcasts.Where(p => p.Episode != episode).FirstOrDefault() == null)
  218. {
  219. podcast.Episode = episode;
  220. podcast.Title = title;
  221. podcast.Description = description;
  222. podcast.DateEdited = DateTime.Now;
  223. db.Entry(podcast).State = EntityState.Modified;
  224. db.SaveChanges();
  225. return Json(new { result = true });
  226. }
  227. return Json(new { error = "That episode already exists" });
  228. }
  229. return Json(new { error = "No podcast found" });
  230. }
  231. return Json(new { error = "You don't have permission to edit this podcast" });
  232. }
  233. return Json(new { error = "Invalid Inputs" });
  234. }
  235. [HttpPost]
  236. [ValidateAntiForgeryToken]
  237. public ActionResult PublishPodcast(int podcastId, bool publish)
  238. {
  239. if (ModelState.IsValid)
  240. {
  241. if (User.IsInRole("Podcast"))
  242. {
  243. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  244. if (podcast != null)
  245. {
  246. podcast.Published = publish;
  247. if (publish)
  248. podcast.DatePublished = DateTime.Now;
  249. db.Entry(podcast).State = EntityState.Modified;
  250. db.SaveChanges();
  251. return Json(new { result = true });
  252. }
  253. return Json(new { error = "No podcast found" });
  254. }
  255. return Json(new { error = "You don't have permission to publish this podcast" });
  256. }
  257. return Json(new { error = "Invalid Inputs" });
  258. }
  259. [HttpPost]
  260. [ValidateAntiForgeryToken]
  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. [ValidateAntiForgeryToken]
  310. public ActionResult CreateComment(int podcastId, string article)
  311. {
  312. if (ModelState.IsValid)
  313. {
  314. if (db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault() != null)
  315. {
  316. PodcastComment comment = db.PodcastComments.Create();
  317. comment.PodcastId = podcastId;
  318. comment.UserId = db.Users.Where(u => u.Username == User.Identity.Name).First().UserId;
  319. comment.Article = article;
  320. comment.DatePosted = DateTime.Now;
  321. comment.DateEdited = DateTime.Now;
  322. db.PodcastComments.Add(comment);
  323. db.SaveChanges();
  324. return Json(new { result = true });
  325. }
  326. return Json(new { error = "That podcast does not exist" });
  327. }
  328. return Json(new { error = "Invalid Parameters" });
  329. }
  330. [HttpPost]
  331. [ValidateAntiForgeryToken]
  332. public ActionResult EditComment(int commentID, string article)
  333. {
  334. if (ModelState.IsValid)
  335. {
  336. PodcastComment comment = db.PodcastComments.Include("User").Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  337. if (comment != null)
  338. {
  339. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  340. {
  341. comment.Article = article;
  342. comment.DateEdited = DateTime.Now;
  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. PodcastComment comment = db.PodcastComments.Include("User").Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  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. }