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

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