diff options
Diffstat (limited to 'Project-Unite/Controllers')
| -rw-r--r-- | Project-Unite/Controllers/ContestsController.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/Project-Unite/Controllers/ContestsController.cs b/Project-Unite/Controllers/ContestsController.cs index 143f885..1d403f2 100644 --- a/Project-Unite/Controllers/ContestsController.cs +++ b/Project-Unite/Controllers/ContestsController.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; +using Microsoft.AspNet.Identity; using Project_Unite.Models; namespace Project_Unite.Controllers @@ -40,10 +42,74 @@ namespace Project_Unite.Controllers } + public ActionResult SubmitEntry(string id) + { + var db = new ApplicationDbContext(); + var contest = db.Contests.FirstOrDefault(x => x.Id == id); + if (contest == null) + return new HttpStatusCodeResult(404); + if (contest.UserSubmitted(User.Identity.GetUserId())) + return new HttpStatusCodeResult(403); + + var model = new SubmitContestEntryViewModel(); + model.ContestId = contest.Id; + return View(model); + } + + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult SubmitEntry(SubmitContestEntryViewModel model) + { + if(model.Download != null) + { + if (!model.Download.FileName.ToLower().EndsWith(".zip")) + ModelState.AddModelError("Download", new Exception("The uploaded file is not a .zip file.")); + } + + if (!ModelState.IsValid) + return View(model); + + var db = new ApplicationDbContext(); + var entry = new ContestEntry(); + entry.Name = model.Name; + entry.Description = model.Description; + entry.PostedAt = DateTime.Now; + entry.Disqualified = false; + entry.AuthorId = User.Identity.GetUserId(); + entry.ContestId = model.ContestId; + entry.VideoId = model.VideoID; + string allowed = "abcdefghijklmnopqrstuvwxyz1234567890_"; + entry.Id = entry.Name.ToLower(); + foreach (var ch in entry.Id.ToCharArray()) + if (!allowed.Contains(ch)) + entry.Id = entry.Id.Replace(ch, '_'); + if (model.Download != null) + { + string fname = model.Download.FileName.ToLower().Replace(".zip", ""); + foreach (var ch in fname.ToCharArray()) + if (!allowed.Contains(ch)) + fname = fname.Replace(ch, '_'); + fname += ".zip"; + string serverpath = "~/Uploads/" + ACL.UserNameRaw(User.Identity.GetUserId()) + "/ContestSubmissions/" + model.ContestId; + string mapped = Server.MapPath(serverpath); + string mappedwithfilename = Path.Combine(mapped, fname); + string dbpath = serverpath.Remove(0, 1) + "/" + fname; + if (!Directory.Exists(mapped)) + Directory.CreateDirectory(mapped); + model.Download.SaveAs(mappedwithfilename); + entry.DownloadURL = dbpath; + } + + db.ContestEntries.Add(entry); + db.SaveChanges(); + return RedirectToAction("ViewSubmission", "Contests", new { id = entry.Id }); + } + [RequiresAdmin] public ActionResult CreateContest() { var model = new CreateContestViewModel(); + model.EndDate = DateTime.Now; return View(model); } |
