Browse Source

Modified file adding/editing for podcasts.

master
Teknikode 6 years ago
parent
commit
76d1f1c82e
  1. 99
      Teknik/Areas/Podcast/Controllers/PodcastController.cs
  2. 7
      Teknik/Areas/Podcast/Scripts/Podcast.js
  3. 8
      Teknik/Areas/Podcast/Views/Podcast/Main.cshtml
  4. 8
      Teknik/Areas/Podcast/Views/Podcast/ViewPodcast.cshtml
  5. 8
      Teknik/Areas/Profile/Scripts/Profile.js

99
Teknik/Areas/Podcast/Controllers/PodcastController.cs

@ -166,53 +166,19 @@ namespace Teknik.Areas.Podcast.Controllers @@ -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;
// 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<PodcastFile>();
podcast.Files.Add(podFile);
// 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);
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 @@ -237,6 +203,8 @@ namespace Teknik.Areas.Podcast.Controllers
podcast.Title = title;
podcast.Description = description;
podcast.DateEdited = DateTime.Now;
List<PodcastFile> 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 @@ -392,5 +360,44 @@ namespace Teknik.Areas.Podcast.Controllers
return Json(new { error = "Invalid Parameters" });
}
#endregion
public List<PodcastFile> SaveFiles(HttpFileCollectionBase files, int episode)
{
List<PodcastFile> podFiles = new List<PodcastFile>();
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;
}
}
}

7
Teknik/Areas/Podcast/Scripts/Podcast.js

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
$(document).ready(function () {
$("#podcast_submit").click(function () {
$.blockUI({ message: '<div class="text-center"><h3>Saving...</h3></div>' });
$('#newPodcast').modal('hide');
var fd = new FormData();
var fileInput = document.getElementById('podcast_files');
@ -22,12 +23,16 @@ @@ -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('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + obj.error + '</div>');
$("#top_msg").html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + error + '</div>');
}
}
}

8
Teknik/Areas/Podcast/Views/Podcast/Main.cshtml

@ -74,7 +74,7 @@ @@ -74,7 +74,7 @@
</div>
<div class="row">
<div class="form-group col-sm-12">
<label for="files"><h4>Upload Podcast</h4></label>
<label for="podcast_files"><h4>Upload Podcast</h4></label>
<input id="podcast_files" name="podcast_files" type="file" placeholder="podcast.ogg" title="select the podcast file." multiple>
</div>
</div>
@ -119,6 +119,12 @@ @@ -119,6 +119,12 @@
<textarea class="form-control wmd-input" name="edit_podcast_description" id="edit_podcast_description" placeholder="We talked about awesome stuff." title="enter what the podcast was about." data-provide="markdown" rows="10"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-sm-12">
<label for="edit_podcast_files"><h4>Upload Podcast</h4></label>
<input id="edit_podcast_files" name="edit_podcast_files" type="file" placeholder="podcast.ogg" title="select the podcast file." multiple>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

8
Teknik/Areas/Podcast/Views/Podcast/ViewPodcast.cshtml

@ -59,14 +59,10 @@ @@ -59,14 +59,10 @@
</div>
<div class="row">
<div class="form-group col-sm-12">
<label for="uploadPodcast"><h4>Upload Podcast</h4></label>
<input id="edit_uploadPodcast" name="file" type="file" placeholder="podcast.ogg" title="select the podcast file." />
<label for="edit_podcast_files"><h4>Upload Podcast</h4></label>
<input id="edit_podcast_files" name="edit_podcast_files" type="file" placeholder="podcast.ogg" title="select the podcast file." multiple>
</div>
</div>
<div class="row">
<div class="form-group col-sm-12" id="edit_uploadedPodcasts"></div>
<input name="edit_podcast_file" id="edit_podcast_file" type="hidden" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

8
Teknik/Areas/Profile/Scripts/Profile.js

@ -53,14 +53,16 @@ @@ -53,14 +53,16 @@
serverSideEncrypt: upload_serverSideEncrypt
},
success: function (html) {
$.unblockUI();
if (html.result) {
$.unblockUI();
window.location.reload();
}
else {
$.unblockUI();
var error = html;
if (html.error)
error = html.error;
$("#top_msg").css('display', 'inline', 'important');
$("#top_msg").html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + html.error + '</div>');
$("#top_msg").html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + error + '</div>');
}
}
});

Loading…
Cancel
Save