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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. 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. podcast.DateEdited = DateTime.Now;
  164. // Handle saving of files
  165. for (int i = 0; i < Request.Files.Count; i++)
  166. {
  167. HttpPostedFileBase file = Request.Files[i]; //Uploaded file
  168. //Use the following properties to get file's name, size and MIMEType
  169. int fileSize = file.ContentLength;
  170. string fileName = file.FileName;
  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 = file.ContentType;
  188. podFile.ContentLength = file.ContentLength;
  189. podcast.Files = new List<PodcastFile>();
  190. podcast.Files.Add(podFile);
  191. file.SaveAs(fullPath);
  192. }
  193. db.Podcasts.Add(podcast);
  194. db.SaveChanges();
  195. return Json(new { result = true });
  196. }
  197. return Json(new { error = "You must submit at least one podcast audio file" });
  198. }
  199. return Json(new { error = "That episode already exists" });
  200. }
  201. return Json(new { error = "You don't have permission to create a podcast" });
  202. }
  203. return Json(new { error = "No podcast created" });
  204. }
  205. [HttpPost]
  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. podcast.DateEdited = DateTime.Now;
  221. db.Entry(podcast).State = EntityState.Modified;
  222. db.SaveChanges();
  223. return Json(new { result = true });
  224. }
  225. return Json(new { error = "That episode already exists" });
  226. }
  227. return Json(new { error = "No podcast found" });
  228. }
  229. return Json(new { error = "You don't have permission to edit this podcast" });
  230. }
  231. return Json(new { error = "Invalid Inputs" });
  232. }
  233. [HttpPost]
  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. public ActionResult DeletePodcast(int podcastId)
  258. {
  259. if (ModelState.IsValid)
  260. {
  261. if (User.IsInRole("Podcast"))
  262. {
  263. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  264. if (podcast != null)
  265. {
  266. db.Podcasts.Remove(podcast);
  267. db.SaveChanges();
  268. return Json(new { result = true });
  269. }
  270. return Json(new { error = "No podcast found" });
  271. }
  272. return Json(new { error = "You don't have permission to delete this podcast" });
  273. }
  274. return Json(new { error = "Invalid Inputs" });
  275. }
  276. #endregion
  277. #region Comments
  278. [HttpPost]
  279. [AllowAnonymous]
  280. public ActionResult GetComments(int podcastId, int startCommentID, int count)
  281. {
  282. 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();
  283. List<CommentViewModel> commentViews = new List<CommentViewModel>();
  284. if (comments != null)
  285. {
  286. foreach (PodcastComment comment in comments)
  287. {
  288. commentViews.Add(new CommentViewModel(comment));
  289. }
  290. }
  291. return PartialView("~/Areas/Podcast/Views/Podcast/Comments.cshtml", commentViews);
  292. }
  293. [HttpPost]
  294. [AllowAnonymous]
  295. public ActionResult GetCommentArticle(int commentID)
  296. {
  297. PodcastComment comment = db.PodcastComments.Where(p => (p.PodcastCommentId == commentID)).FirstOrDefault();
  298. if (comment != null)
  299. {
  300. return Json(new { result = comment.Article });
  301. }
  302. return Json(new { error = "No article found" });
  303. }
  304. [HttpPost]
  305. public ActionResult CreateComment(int podcastId, string article)
  306. {
  307. if (ModelState.IsValid)
  308. {
  309. if (db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault() != null)
  310. {
  311. PodcastComment comment = db.PodcastComments.Create();
  312. comment.PodcastId = podcastId;
  313. comment.UserId = db.Users.Where(u => u.Username == User.Identity.Name).First().UserId;
  314. comment.Article = article;
  315. comment.DatePosted = DateTime.Now;
  316. comment.DateEdited = DateTime.Now;
  317. db.PodcastComments.Add(comment);
  318. db.SaveChanges();
  319. return Json(new { result = true });
  320. }
  321. return Json(new { error = "That podcast does not exist" });
  322. }
  323. return Json(new { error = "Invalid Parameters" });
  324. }
  325. [HttpPost]
  326. public ActionResult EditComment(int commentID, string article)
  327. {
  328. if (ModelState.IsValid)
  329. {
  330. PodcastComment comment = db.PodcastComments.Include("User").Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  331. if (comment != null)
  332. {
  333. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  334. {
  335. comment.Article = article;
  336. comment.DateEdited = DateTime.Now;
  337. db.Entry(comment).State = EntityState.Modified;
  338. db.SaveChanges();
  339. return Json(new { result = true });
  340. }
  341. return Json(new { error = "You don't have permission to edit this comment" });
  342. }
  343. return Json(new { error = "No comment found" });
  344. }
  345. return Json(new { error = "Invalid Parameters" });
  346. }
  347. [HttpPost]
  348. public ActionResult DeleteComment(int commentID)
  349. {
  350. if (ModelState.IsValid)
  351. {
  352. PodcastComment comment = db.PodcastComments.Include("User").Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  353. if (comment != null)
  354. {
  355. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  356. {
  357. db.PodcastComments.Remove(comment);
  358. db.SaveChanges();
  359. return Json(new { result = true });
  360. }
  361. return Json(new { error = "You don't have permission to delete this comment" });
  362. }
  363. return Json(new { error = "No comment found" });
  364. }
  365. return Json(new { error = "Invalid Parameters" });
  366. }
  367. #endregion
  368. }
  369. }