public TioParser(int tournamentFileId) { tournamentFile = db.TournamentFiles.Find(tournamentFileId); if (tournamentFile.Processed) { throw new FileAlreadyParsedException(); } Tournament existingTournament = (from t in db.Tournaments where t.TournamentGuid.Equals(tournamentFile.TournamentGuid) select t).FirstOrDefault(); if (existingTournament != null) { throw new FileAlreadyParsedException(); } tournament = new Tournament(); tournamentXML = XDocument.Parse(tournamentFile.XML); CultureInfo ci = new CultureInfo(tournamentXML.Descendants("Culture").First().Value, false); tournament.Date = DateTime.Parse(tournamentXML.Descendants("StartDate").First().Value, ci).AddHours(12); tournament.Name = tournamentXML.Descendants("Event").First().Element("Name").Value; tournament.locked = false; tournament.TournamentGuid = tournamentFile.TournamentGuid; db.Tournaments.Add(tournament); db.SaveChanges(); }
public ActionResult Upload(HttpPostedFileBase file) { if (file == null || file.ContentLength == 0) { ViewBag.UploadMessage = "Please select a file for upload."; return(View("LoggedInIndex")); } var userId = Convert.ToInt32(Session["userId"]); if (userId <= 0) { return(View("Index")); } string xml; using (StreamReader sr = new StreamReader(file.InputStream)) { xml = sr.ReadToEnd(); } XmlDocument doc = new XmlDocument(); try { doc.LoadXml(xml); } catch { ViewBag.UploadMessage = "Error: The TIO file you uploaded was not well-formed xml."; return(View("LoggedInIndex")); } Guid tioGuid = Guid.Parse(doc.SelectSingleNode("//EventList/Event/ID/text()").Value); TournamentFile foundFile = (from t in db.TournamentFiles where t.TournamentGuid.Equals(tioGuid) select t).FirstOrDefault(); Tournament foundTournament = (from t in db.Tournaments where t.TournamentGuid.Equals(tioGuid) select t).FirstOrDefault(); if (foundTournament != null || foundFile != null) { if (!Convert.ToBoolean(Session["userModerator"])) { ViewBag.UploadMessage = "Error: This TIO file has been uploaded before."; return(View("LoggedInIndex")); } else { if (foundTournament != null) { eraserHelper.EraseTournament(foundTournament.TournamentID); } if (foundFile != null) { eraserHelper.EraseTournamentFile(foundFile.TournamentFileID); } } } string originalFileName = new string(file.FileName.Take(32).ToArray()); originalFileName = Regex.Replace(originalFileName, "\\s+", ""); var fileName = originalFileName + DateTime.Now.ToString("yyyy-MM-dd-ffffff"); string path = "/Files/UnauthorizedUploads"; var filePath = Path.Combine(Server.MapPath(path), fileName); System.IO.File.WriteAllText(filePath, xml); TournamentFile tournamentFile = new TournamentFile(); tournamentFile.XML = xml; tournamentFile.Processed = false; tournamentFile.Inserted = DateTime.Now; tournamentFile.OriginalFileName = originalFileName; tournamentFile.ProcessedAt = null; tournamentFile.TournamentGuid = tioGuid; tournamentFile.UserID = userId; db.TournamentFiles.Add(tournamentFile); db.SaveChanges(); ViewBag.UploadMessage = "Thank you for your submission! It will be processed as soon as it's verified."; return(View("LoggedInIndex")); }