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

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