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

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