diff --git a/Teknik/Areas/Podcast/Controllers/PodcastController.cs b/Teknik/Areas/Podcast/Controllers/PodcastController.cs index 5de3909..8336085 100644 --- a/Teknik/Areas/Podcast/Controllers/PodcastController.cs +++ b/Teknik/Areas/Podcast/Controllers/PodcastController.cs @@ -166,53 +166,19 @@ namespace Teknik.Areas.Podcast.Controllers 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; - podcast.DateEdited = DateTime.Now; + // 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; + podcast.DateEdited = DateTime.Now; + podcast.Files = SaveFiles(Request.Files, episode); - // 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 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 = file.ContentType; - podFile.ContentLength = file.ContentLength; - podcast.Files = new List(); - 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" }); + db.Podcasts.Add(podcast); + db.SaveChanges(); + return Json(new { result = true }); } return Json(new { error = "That episode already exists" }); } @@ -237,6 +203,8 @@ namespace Teknik.Areas.Podcast.Controllers podcast.Title = title; podcast.Description = description; podcast.DateEdited = DateTime.Now; + List newFiles = SaveFiles(Request.Files, episode); + podcast.Files.AddRange(newFiles); db.Entry(podcast).State = EntityState.Modified; db.SaveChanges(); return Json(new { result = true }); @@ -392,5 +360,44 @@ namespace Teknik.Areas.Podcast.Controllers return Json(new { error = "Invalid Parameters" }); } #endregion + + public List SaveFiles(HttpFileCollectionBase files, int episode) + { + List podFiles = new List(); + + if (files.Count > 0) + { + for (int i = 0; i < Request.Files.Count; i++) + { + HttpPostedFileBase file = Request.Files[i]; + + int fileSize = file.ContentLength; + string fileName = file.FileName; + 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 = file.ContentType; + podFile.ContentLength = file.ContentLength; + podFiles.Add(podFile); + + file.SaveAs(fullPath); + } + } + + return podFiles; + } } } \ No newline at end of file diff --git a/Teknik/Areas/Podcast/Scripts/Podcast.js b/Teknik/Areas/Podcast/Scripts/Podcast.js index 8537a6f..1bc3a61 100644 --- a/Teknik/Areas/Podcast/Scripts/Podcast.js +++ b/Teknik/Areas/Podcast/Scripts/Podcast.js @@ -1,5 +1,6 @@ $(document).ready(function () { $("#podcast_submit").click(function () { + $.blockUI({ message: '

Saving...

' }); $('#newPodcast').modal('hide'); var fd = new FormData(); var fileInput = document.getElementById('podcast_files'); @@ -22,12 +23,16 @@ xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { obj = JSON.parse(xhr.responseText); + $.unblockUI(); if (obj.result) { window.location.reload(); } else { + var error = obj; + if (obj.error) + error = obj.error; $("#top_msg").css('display', 'inline', 'important'); - $("#top_msg").html('
' + obj.error + '
'); + $("#top_msg").html('
' + error + '
'); } } } diff --git a/Teknik/Areas/Podcast/Views/Podcast/Main.cshtml b/Teknik/Areas/Podcast/Views/Podcast/Main.cshtml index 07da35e..ad8408b 100644 --- a/Teknik/Areas/Podcast/Views/Podcast/Main.cshtml +++ b/Teknik/Areas/Podcast/Views/Podcast/Main.cshtml @@ -74,7 +74,7 @@
- +
@@ -119,6 +119,12 @@ +
+
+ + +
+
- - + +
-
-
- -