The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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.Areas.Users.Utility;
  11. using Teknik.Controllers;
  12. using Teknik.Filters;
  13. using Teknik.Models;
  14. namespace Teknik.Areas.Podcast.Controllers
  15. {
  16. public class PodcastController : DefaultController
  17. {
  18. private TeknikEntities db = new TeknikEntities();
  19. [TrackPageView]
  20. [AllowAnonymous]
  21. public ActionResult Index()
  22. {
  23. MainViewModel model = new MainViewModel();
  24. model.Title = Config.PodcastConfig.Title;
  25. model.Description = Config.PodcastConfig.Description;
  26. try
  27. {
  28. ViewBag.Title = Config.PodcastConfig.Title + " - " + Config.Title;
  29. ViewBag.Description = Config.PodcastConfig.Description;
  30. bool editor = User.IsInRole("Podcast");
  31. var foundPodcasts = db.Podcasts.Where(p => (p.Published || editor)).FirstOrDefault();
  32. if (foundPodcasts != null)
  33. {
  34. model.HasPodcasts = (foundPodcasts != null);
  35. }
  36. else
  37. {
  38. model.Error = true;
  39. model.ErrorMessage = "No Podcasts Available";
  40. }
  41. return View("~/Areas/Podcast/Views/Podcast/Main.cshtml", model);
  42. }
  43. catch (Exception ex)
  44. {
  45. model.Error = true;
  46. model.ErrorMessage = ex.Message;
  47. return View("~/Areas/Podcast/Views/Podcast/Main.cshtml", model);
  48. }
  49. }
  50. #region Podcasts
  51. [TrackPageView]
  52. [AllowAnonymous]
  53. public ActionResult View(int episode)
  54. {
  55. PodcastViewModel model = new PodcastViewModel();
  56. // find the podcast specified
  57. bool editor = User.IsInRole("Podcast");
  58. var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.Episode == episode)).FirstOrDefault();
  59. if (foundPodcast != null)
  60. {
  61. model = new PodcastViewModel(foundPodcast);
  62. ViewBag.Title = model.Title + " - Teknikast - " + Config.Title;
  63. return View("~/Areas/Podcast/Views/Podcast/ViewPodcast.cshtml", model);
  64. }
  65. model.Error = true;
  66. model.ErrorMessage = "No Podcasts Available";
  67. return View("~/Areas/Podcast/Views/Podcast/ViewPodcast.cshtml", model);
  68. }
  69. [AllowAnonymous]
  70. public ActionResult Download(int episode, string fileName)
  71. {
  72. // find the podcast specified
  73. var foundPodcast = db.Podcasts.Where(p => (p.Published && p.Episode == episode)).FirstOrDefault();
  74. if (foundPodcast != null)
  75. {
  76. PodcastFile file = foundPodcast.Files.Where(f => f.FileName == fileName).FirstOrDefault();
  77. if (file != null)
  78. {
  79. if (System.IO.File.Exists(file.Path))
  80. {
  81. // Read in the file
  82. byte[] data = System.IO.File.ReadAllBytes(file.Path);
  83. // Create File
  84. var cd = new System.Net.Mime.ContentDisposition
  85. {
  86. FileName = file.FileName,
  87. Inline = true
  88. };
  89. Response.AppendHeader("Content-Disposition", cd.ToString());
  90. return File(data, file.ContentType);
  91. }
  92. }
  93. }
  94. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  95. }
  96. [HttpPost]
  97. [AllowAnonymous]
  98. public ActionResult GetPodcasts(int startPodcastID, int count)
  99. {
  100. bool editor = User.IsInRole("Podcast");
  101. var podcasts = db.Podcasts.Where(p => p.Published || editor).OrderByDescending(p => p.DatePosted).Skip(startPodcastID).Take(count).ToList();
  102. List<PodcastViewModel> podcastViews = new List<PodcastViewModel>();
  103. if (podcasts != null)
  104. {
  105. foreach (Models.Podcast podcast in podcasts)
  106. {
  107. podcastViews.Add(new PodcastViewModel(podcast));
  108. }
  109. }
  110. return PartialView("~/Areas/Podcast/Views/Podcast/Podcasts.cshtml", podcastViews);
  111. }
  112. [HttpPost]
  113. [AllowAnonymous]
  114. public ActionResult GetPodcastEpisode(int podcastId)
  115. {
  116. bool editor = User.IsInRole("Podcast");
  117. var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
  118. if (foundPodcast != null)
  119. {
  120. return Json(new { result = foundPodcast.Episode });
  121. }
  122. return Json(new { error = "No podcast found" });
  123. }
  124. [HttpPost]
  125. [AllowAnonymous]
  126. public ActionResult GetPodcastTitle(int podcastId)
  127. {
  128. bool editor = User.IsInRole("Podcast");
  129. var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
  130. if (foundPodcast != null)
  131. {
  132. return Json(new { result = foundPodcast.Title });
  133. }
  134. return Json(new { error = "No podcast found" });
  135. }
  136. [HttpPost]
  137. [AllowAnonymous]
  138. public ActionResult GetPodcastDescription(int podcastId)
  139. {
  140. bool editor = User.IsInRole("Podcast");
  141. var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
  142. if (foundPodcast != null)
  143. {
  144. return Json(new { result = foundPodcast.Description });
  145. }
  146. return Json(new { error = "No podcast found" });
  147. }
  148. [HttpPost]
  149. [AllowAnonymous]
  150. public ActionResult GetPodcastFiles(int podcastId)
  151. {
  152. bool editor = User.IsInRole("Podcast");
  153. var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
  154. if (foundPodcast != null)
  155. {
  156. List<object> files = new List<object>();
  157. foreach (PodcastFile file in foundPodcast.Files)
  158. {
  159. object fileObj = new
  160. {
  161. name = file.FileName,
  162. id = file.PodcastFileId
  163. };
  164. files.Add(fileObj);
  165. }
  166. return Json(new { result = new { files = files } });
  167. }
  168. return Json(new { error = "No podcast found" });
  169. }
  170. [HttpPost]
  171. public ActionResult CreatePodcast(int episode, string title, string description)
  172. {
  173. if (ModelState.IsValid)
  174. {
  175. if (User.IsInRole("Podcast"))
  176. {
  177. // Grab the next episode number
  178. Models.Podcast lastPod = db.Podcasts.Where(p => p.Episode == episode).FirstOrDefault();
  179. if (lastPod == null)
  180. {
  181. // Create the podcast object
  182. Models.Podcast podcast = db.Podcasts.Create();
  183. podcast.Episode = episode;
  184. podcast.Title = title;
  185. podcast.Description = description;
  186. podcast.DatePosted = DateTime.Now;
  187. podcast.DatePublished = DateTime.Now;
  188. podcast.DateEdited = DateTime.Now;
  189. podcast.Files = SaveFiles(Request.Files, episode);
  190. db.Podcasts.Add(podcast);
  191. db.SaveChanges();
  192. return Json(new { result = true });
  193. }
  194. return Json(new { error = "That episode already exists" });
  195. }
  196. return Json(new { error = "You don't have permission to create a podcast" });
  197. }
  198. return Json(new { error = "No podcast created" });
  199. }
  200. [HttpPost]
  201. public ActionResult EditPodcast(int podcastId, int episode, string title, string description, string fileIds)
  202. {
  203. if (ModelState.IsValid)
  204. {
  205. if (User.IsInRole("Podcast"))
  206. {
  207. Models.Podcast podcast = db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault();
  208. if (podcast != null)
  209. {
  210. if (db.Podcasts.Where(p => p.Episode != episode).FirstOrDefault() == null || podcast.Episode == episode)
  211. {
  212. podcast.Episode = episode;
  213. podcast.Title = title;
  214. podcast.Description = description;
  215. podcast.DateEdited = DateTime.Now;
  216. // Remove any files not in fileIds
  217. List<string> fileIdList = new List<string>();
  218. if (!string.IsNullOrEmpty(fileIds))
  219. {
  220. fileIdList = fileIds.Split(',').ToList();
  221. }
  222. for (int i = 0; i < podcast.Files.Count; i++)
  223. {
  224. PodcastFile curFile = podcast.Files.ElementAt(i);
  225. if (!fileIdList.Exists(id => id == curFile.PodcastFileId.ToString()))
  226. {
  227. if (System.IO.File.Exists(curFile.Path))
  228. {
  229. System.IO.File.Delete(curFile.Path);
  230. }
  231. db.PodcastFiles.Remove(curFile);
  232. podcast.Files.Remove(curFile);
  233. }
  234. }
  235. // Add any new files
  236. List<PodcastFile> newFiles = SaveFiles(Request.Files, episode);
  237. foreach (PodcastFile file in newFiles)
  238. {
  239. podcast.Files.Add(file);
  240. }
  241. // Save podcast
  242. db.Entry(podcast).State = EntityState.Modified;
  243. db.SaveChanges();
  244. return Json(new { result = true });
  245. }
  246. return Json(new { error = "That episode already exists" });
  247. }
  248. return Json(new { error = "No podcast found" });
  249. }
  250. return Json(new { error = "You don't have permission to edit this podcast" });
  251. }
  252. return Json(new { error = "Invalid Inputs" });
  253. }
  254. [HttpPost]
  255. public ActionResult PublishPodcast(int podcastId, bool publish)
  256. {
  257. if (ModelState.IsValid)
  258. {
  259. if (User.IsInRole("Podcast"))
  260. {
  261. Models.Podcast podcast = db.Podcasts.Find(podcastId);
  262. if (podcast != null)
  263. {
  264. podcast.Published = publish;
  265. if (publish)
  266. podcast.DatePublished = DateTime.Now;
  267. db.Entry(podcast).State = EntityState.Modified;
  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 publish this podcast" });
  274. }
  275. return Json(new { error = "Invalid Inputs" });
  276. }
  277. [HttpPost]
  278. public ActionResult DeletePodcast(int podcastId)
  279. {
  280. if (ModelState.IsValid)
  281. {
  282. if (User.IsInRole("Podcast"))
  283. {
  284. Models.Podcast podcast = db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault();
  285. if (podcast != null)
  286. {
  287. foreach (PodcastFile file in podcast.Files)
  288. {
  289. System.IO.File.Delete(file.Path);
  290. }
  291. db.Podcasts.Remove(podcast);
  292. db.SaveChanges();
  293. return Json(new { result = true });
  294. }
  295. return Json(new { error = "No podcast found" });
  296. }
  297. return Json(new { error = "You don't have permission to delete this podcast" });
  298. }
  299. return Json(new { error = "Invalid Inputs" });
  300. }
  301. #endregion
  302. #region Comments
  303. [HttpPost]
  304. [AllowAnonymous]
  305. public ActionResult GetComments(int podcastId, int startCommentID, int count)
  306. {
  307. var comments = db.PodcastComments.Where(p => (p.PodcastId == podcastId)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
  308. List<CommentViewModel> commentViews = new List<CommentViewModel>();
  309. if (comments != null)
  310. {
  311. foreach (PodcastComment comment in comments)
  312. {
  313. commentViews.Add(new CommentViewModel(comment));
  314. }
  315. }
  316. return PartialView("~/Areas/Podcast/Views/Podcast/Comments.cshtml", commentViews);
  317. }
  318. [HttpPost]
  319. [AllowAnonymous]
  320. public ActionResult GetCommentArticle(int commentID)
  321. {
  322. PodcastComment comment = db.PodcastComments.Where(p => (p.PodcastCommentId == commentID)).FirstOrDefault();
  323. if (comment != null)
  324. {
  325. return Json(new { result = comment.Article });
  326. }
  327. return Json(new { error = "No article found" });
  328. }
  329. [HttpPost]
  330. public ActionResult CreateComment(int podcastId, string article)
  331. {
  332. if (ModelState.IsValid)
  333. {
  334. if (db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault() != null)
  335. {
  336. PodcastComment comment = db.PodcastComments.Create();
  337. comment.PodcastId = podcastId;
  338. comment.UserId = UserHelper.GetUser(db, User.Identity.Name).UserId;
  339. comment.Article = article;
  340. comment.DatePosted = DateTime.Now;
  341. comment.DateEdited = DateTime.Now;
  342. db.PodcastComments.Add(comment);
  343. db.SaveChanges();
  344. return Json(new { result = true });
  345. }
  346. return Json(new { error = "That podcast does not exist" });
  347. }
  348. return Json(new { error = "Invalid Parameters" });
  349. }
  350. [HttpPost]
  351. public ActionResult EditComment(int commentID, string article)
  352. {
  353. if (ModelState.IsValid)
  354. {
  355. PodcastComment comment = db.PodcastComments.Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  356. if (comment != null)
  357. {
  358. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  359. {
  360. comment.Article = article;
  361. comment.DateEdited = DateTime.Now;
  362. db.Entry(comment).State = EntityState.Modified;
  363. db.SaveChanges();
  364. return Json(new { result = true });
  365. }
  366. return Json(new { error = "You don't have permission to edit this comment" });
  367. }
  368. return Json(new { error = "No comment found" });
  369. }
  370. return Json(new { error = "Invalid Parameters" });
  371. }
  372. [HttpPost]
  373. public ActionResult DeleteComment(int commentID)
  374. {
  375. if (ModelState.IsValid)
  376. {
  377. PodcastComment comment = db.PodcastComments.Where(c => c.PodcastCommentId == commentID).FirstOrDefault();
  378. if (comment != null)
  379. {
  380. if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
  381. {
  382. db.PodcastComments.Remove(comment);
  383. db.SaveChanges();
  384. return Json(new { result = true });
  385. }
  386. return Json(new { error = "You don't have permission to delete this comment" });
  387. }
  388. return Json(new { error = "No comment found" });
  389. }
  390. return Json(new { error = "Invalid Parameters" });
  391. }
  392. #endregion
  393. public List<PodcastFile> SaveFiles(HttpFileCollectionBase files, int episode)
  394. {
  395. List<PodcastFile> podFiles = new List<PodcastFile>();
  396. if (files.Count > 0)
  397. {
  398. for (int i = 0; i < files.Count; i++)
  399. {
  400. HttpPostedFileBase file = files[i];
  401. int fileSize = file.ContentLength;
  402. string fileName = file.FileName;
  403. string fileExt = Path.GetExtension(fileName);
  404. if (!Directory.Exists(Config.PodcastConfig.PodcastDirectory))
  405. {
  406. Directory.CreateDirectory(Config.PodcastConfig.PodcastDirectory);
  407. }
  408. string newName = string.Format("Teknikast_Episode_{0}{1}", episode, fileExt);
  409. int index = 1;
  410. while (System.IO.File.Exists(Path.Combine(Config.PodcastConfig.PodcastDirectory, newName)))
  411. {
  412. newName = string.Format("Teknikast_Episode_{0} ({1}){2}", episode, index, fileExt);
  413. index++;
  414. }
  415. string fullPath = Path.Combine(Config.PodcastConfig.PodcastDirectory, newName);
  416. PodcastFile podFile = new PodcastFile();
  417. podFile.Path = fullPath;
  418. podFile.FileName = newName;
  419. podFile.ContentType = file.ContentType;
  420. podFile.ContentLength = file.ContentLength;
  421. podFiles.Add(podFile);
  422. file.SaveAs(fullPath);
  423. }
  424. }
  425. return podFiles;
  426. }
  427. }
  428. }