1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Modified file adding/editing for podcasts.

This commit is contained in:
Uncled1023 2016-02-04 23:01:25 -08:00
parent 897fd5ecfd
commit 76d1f1c82e
5 changed files with 73 additions and 57 deletions

View File

@ -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<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" });
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<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
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;
}
}
}

View File

@ -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 @@
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>');
}
}
}

View File

@ -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 @@
<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>

View File

@ -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>

View File

@ -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>');
}
}
});