123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Teknik.Areas.Podcast.Models;
- using Teknik.Areas.Podcast.ViewModels;
- using Teknik.Controllers;
- using Teknik.Models;
-
- namespace Teknik.Areas.Podcast.Controllers
- {
- public class PodcastController : DefaultController
- {
- private TeknikEntities db = new TeknikEntities();
-
- [AllowAnonymous]
- public ActionResult Index()
- {
- MainViewModel model = new MainViewModel();
- model.Title = Config.PodcastConfig.Title;
- model.Description = Config.PodcastConfig.Description;
- try
- {
- ViewBag.Title = "Teknikast - " + Config.Title;
- bool editor = User.IsInRole("Podcast");
- var foundPodcasts = db.Podcasts.Where(p => (p.Published || editor)).FirstOrDefault();
- if (foundPodcasts != null)
- {
- model.HasPodcasts = (foundPodcasts != null);
- }
- else
- {
- model.Error = true;
- model.ErrorMessage = "No Podcasts Available";
- }
-
- return View("~/Areas/Podcast/Views/Podcast/Main.cshtml", model);
- }
- catch (Exception ex)
- {
- model.Error = true;
- model.ErrorMessage = ex.Message;
-
- return View("~/Areas/Podcast/Views/Podcast/Main.cshtml", model);
- }
- }
-
- #region Podcasts
- [AllowAnonymous]
- public ActionResult View(int episode)
- {
- PodcastViewModel model = new PodcastViewModel();
- // find the podcast specified
- bool editor = User.IsInRole("Podcast");
- var foundPodcast = db.Podcasts.Include("Files").Where(p => ((p.Published || editor) && p.Episode == episode)).FirstOrDefault();
- if (foundPodcast != null)
- {
- model.PodcastId = foundPodcast.PodcastId;
- model.Episode = foundPodcast.Episode;
- model.Title = foundPodcast.Title;
- model.Description = foundPodcast.Description;
- model.Files = foundPodcast.Files;
-
- ViewBag.Title = model.Title + " - Teknikast - " + Config.Title;
- return View("~/Areas/Podcast/Views/Podcast/ViewPodcast.cshtml", model);
- }
- model.Error = true;
- model.ErrorMessage = "No Podcasts Available";
- return View("~/Areas/Podcast/Views/Podcast/ViewPodcast.cshtml", model);
- }
-
- [AllowAnonymous]
- public ActionResult Download(int episode, string fileName)
- {
- // find the podcast specified
- var foundPodcast = db.Podcasts.Include("Files").Where(p => (p.Published && p.Episode == episode)).FirstOrDefault();
- if (foundPodcast != null)
- {
- PodcastFile file = foundPodcast.Files.Where(f => f.FileName == fileName).FirstOrDefault();
- if (file != null)
- {
- if (System.IO.File.Exists(file.Path))
- {
- // Read in the file
- byte[] data = System.IO.File.ReadAllBytes(file.Path);
-
- // Create File
- var cd = new System.Net.Mime.ContentDisposition
- {
- FileName = file.FileName,
- Inline = true
- };
-
- Response.AppendHeader("Content-Disposition", cd.ToString());
-
- return File(data, file.ContentType);
- }
- }
- }
- return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
- }
-
- [HttpPost]
- [AllowAnonymous]
- public ActionResult GetPodcasts(int startPodcastID, int count)
- {
- bool editor = User.IsInRole("Podcast");
- var podcasts = db.Podcasts.Include("Files").Where(p => p.Published || editor).OrderByDescending(p => p.DatePosted).Skip(startPodcastID).Take(count).ToList();
- List<PodcastViewModel> podcastViews = new List<PodcastViewModel>();
- if (podcasts != null)
- {
- foreach (Models.Podcast podcast in podcasts)
- {
- podcastViews.Add(new PodcastViewModel(podcast));
- }
- }
- return PartialView("~/Areas/Podcast/Views/Podcast/Podcasts.cshtml", podcastViews);
- }
-
- [HttpPost]
- [AllowAnonymous]
- public ActionResult GetPodcastEpisode(int podcastId)
- {
- bool editor = User.IsInRole("Podcast");
- var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
- if (foundPodcast != null)
- {
- return Json(new { result = foundPodcast.Episode });
- }
- return Json(new { error = "No podcast found" });
- }
-
- [HttpPost]
- [AllowAnonymous]
- public ActionResult GetPodcastTitle(int podcastId)
- {
- bool editor = User.IsInRole("Podcast");
- var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
- if (foundPodcast != null)
- {
- return Json(new { result = foundPodcast.Title });
- }
- return Json(new { error = "No podcast found" });
- }
-
- [HttpPost]
- [AllowAnonymous]
- public ActionResult GetPodcastDescription(int podcastId)
- {
- bool editor = User.IsInRole("Podcast");
- var foundPodcast = db.Podcasts.Where(p => ((p.Published || editor) && p.PodcastId == podcastId)).FirstOrDefault();
- if (foundPodcast != null)
- {
- return Json(new { result = foundPodcast.Description });
- }
- return Json(new { error = "No podcast found" });
- }
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult CreatePodcast(int episode, string title, string description)
- {
- if (ModelState.IsValid)
- {
- if (User.IsInRole("Podcast"))
- {
- // Grab the next episode number
- Models.Podcast lastPod = db.Podcasts.Where(p => p.Episode == episode).FirstOrDefault();
- if (lastPod == null)
- {
- if (Request.Files.Count > 0)
- {
- // Create the podcast object
- Models.Podcast podcast = db.Podcasts.Create();
- podcast.Episode = episode;
- podcast.Title = title;
- podcast.Description = description;
- podcast.DatePosted = DateTime.Now;
- podcast.DatePublished = DateTime.Now;
-
- // Handle saving of files
- for (int i = 0; i < Request.Files.Count; i++)
- {
- HttpPostedFileBase file = Request.Files[i]; //Uploaded file
- //Use the following properties to get file's name, size and MIMEType
- int fileSize = file.ContentLength;
- string fileName = file.FileName;
- string mimeType = file.ContentType;
- string fileExt = Path.GetExtension(fileName);
- if (!Directory.Exists(Config.PodcastConfig.PodcastDirectory))
- {
- Directory.CreateDirectory(Config.PodcastConfig.PodcastDirectory);
- }
- string newName = string.Format("Teknikast_Episode_{0}{1}", episode, fileExt);
- int index = 1;
- while (System.IO.File.Exists(Path.Combine(Config.PodcastConfig.PodcastDirectory, newName)))
- {
- newName = string.Format("Teknikast_Episode_{0} ({1}){2}", episode, index, fileExt);
- index++;
- }
- string fullPath = Path.Combine(Config.PodcastConfig.PodcastDirectory, newName);
- PodcastFile podFile = new PodcastFile();
- podFile.Path = fullPath;
- podFile.FileName = newName;
- podFile.ContentType = mimeType;
- podcast.Files = new List<PodcastFile>();
- podcast.Files.Add(podFile);
-
- file.SaveAs(fullPath);
- }
-
- db.Podcasts.Add(podcast);
- db.SaveChanges();
- return Json(new { result = true });
- }
- return Json(new { error = "You must submit at least one podcast audio file" });
- }
- return Json(new { error = "That episode already exists" });
- }
- return Json(new { error = "You don't have permission to create a podcast" });
- }
- return Json(new { error = "No podcast created" });
- }
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult EditPodcast(int podcastId, int episode, string title, string description)
- {
- if (ModelState.IsValid)
- {
- if (User.IsInRole("Podcast"))
- {
- Models.Podcast podcast = db.Podcasts.Find(podcastId);
- if (podcast != null)
- {
- if (db.Podcasts.Where(p => p.Episode != episode).FirstOrDefault() == null)
- {
- podcast.Episode = episode;
- podcast.Title = title;
- podcast.Description = description;
- db.Entry(podcast).State = EntityState.Modified;
- db.SaveChanges();
- return Json(new { result = true });
- }
- return Json(new { error = "That episode already exists" });
- }
- return Json(new { error = "No podcast found" });
- }
- return Json(new { error = "You don't have permission to edit this podcast" });
- }
- return Json(new { error = "Invalid Inputs" });
- }
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult PublishPodcast(int podcastId, bool publish)
- {
- if (ModelState.IsValid)
- {
- if (User.IsInRole("Podcast"))
- {
- Models.Podcast podcast = db.Podcasts.Find(podcastId);
- if (podcast != null)
- {
- podcast.Published = publish;
- if (publish)
- podcast.DatePublished = DateTime.Now;
- db.Entry(podcast).State = EntityState.Modified;
- db.SaveChanges();
- return Json(new { result = true });
- }
- return Json(new { error = "No podcast found" });
- }
- return Json(new { error = "You don't have permission to publish this podcast" });
- }
- return Json(new { error = "Invalid Inputs" });
- }
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult DeletePodcast(int podcastId)
- {
- if (ModelState.IsValid)
- {
- if (User.IsInRole("Podcast"))
- {
- Models.Podcast podcast = db.Podcasts.Find(podcastId);
- if (podcast != null)
- {
- db.Podcasts.Remove(podcast);
- db.SaveChanges();
- return Json(new { result = true });
- }
- return Json(new { error = "No podcast found" });
- }
- return Json(new { error = "You don't have permission to delete this podcast" });
- }
- return Json(new { error = "Invalid Inputs" });
- }
- #endregion
-
- #region Comments
- [HttpPost]
- [AllowAnonymous]
- public ActionResult GetComments(int podcastId, int startCommentID, int count)
- {
- var comments = db.PodcastComments.Include("Podcast").Where(p => (p.PodcastId == podcastId)).OrderByDescending(p => p.DatePosted).Skip(startCommentID).Take(count).ToList();
- List<CommentViewModel> commentViews = new List<CommentViewModel>();
- if (comments != null)
- {
- foreach (Models.PodcastComment comment in comments)
- {
- commentViews.Add(new CommentViewModel(comment));
- }
- }
- return PartialView("~/Areas/Podcast/Views/Podcast/Comments.cshtml", commentViews);
- }
-
- [HttpPost]
- [AllowAnonymous]
- public ActionResult GetCommentArticle(int commentID)
- {
- Models.PodcastComment comment = db.PodcastComments.Include("Podcast").Where(p => (p.PodcastCommentId == commentID)).First();
- if (comment != null)
- {
- return Json(new { result = comment.Article });
- }
- return Json(new { error = "No article found" });
- }
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult CreateComment(int podcastId, string article)
- {
- if (ModelState.IsValid)
- {
- if (db.Podcasts.Where(p => p.PodcastId == podcastId).FirstOrDefault() != null)
- {
- Models.PodcastComment comment = db.PodcastComments.Create();
- comment.PodcastId = podcastId;
- comment.UserId = db.Users.Where(u => u.Username == User.Identity.Name).First().UserId;
- comment.Article = article;
- comment.DatePosted = DateTime.Now;
-
- db.PodcastComments.Add(comment);
- db.SaveChanges();
- return Json(new { result = true });
- }
- return Json(new { error = "That podcast does not exist" });
- }
- return Json(new { error = "Invalid Parameters" });
- }
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult EditComment(int commentID, string article)
- {
- if (ModelState.IsValid)
- {
- Models.PodcastComment comment = db.PodcastComments.Find(commentID);
- if (comment != null)
- {
- if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
- {
- comment.Article = article;
- db.Entry(comment).State = EntityState.Modified;
- db.SaveChanges();
- return Json(new { result = true });
- }
- return Json(new { error = "You don't have permission to edit this comment" });
- }
- return Json(new { error = "No comment found" });
- }
- return Json(new { error = "Invalid Parameters" });
- }
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult DeleteComment(int commentID)
- {
- if (ModelState.IsValid)
- {
- Models.PodcastComment comment = db.PodcastComments.Find(commentID);
- if (comment != null)
- {
- if (comment.User.Username == User.Identity.Name || User.IsInRole("Admin"))
- {
- db.PodcastComments.Remove(comment);
- db.SaveChanges();
- return Json(new { result = true });
- }
- return Json(new { error = "You don't have permission to delete this comment" });
- }
- return Json(new { error = "No comment found" });
- }
- return Json(new { error = "Invalid Parameters" });
- }
- #endregion
- }
- }
|