public void UpdateVariant(Models.GameMapVariant model) { db.GameMapVariants.Attach(model); var entry = db.Entry(model); entry.State = System.Data.Entity.EntityState.Modified; }
public ActionResult Edit(int id, Models.GameMapVariant model) { if (ModelState.IsValid) { var variant = GameMapService.GetVariant(id); if (!(variant.AuthorId == User.Identity.GetUserId <int>() || User.IsInRole("Mod"))) { return(new HttpUnauthorizedResult()); } variant.Title = model.Title; variant.Description = model.Description; variant.ShortDescription = model.ShortDescription; variant.MinPlayers = model.MinPlayers; variant.MaxPlayers = model.MaxPlayers; string path = System.IO.Path.Combine(Server.MapPath("~/Content/Files/Forge/"), variant.File.FileName); using (FileStream stream = System.IO.File.Open(path, FileMode.Open)) { VariantLib.ForgeVariant forge = new VariantLib.ForgeVariant(stream); forge.VariantDescription = variant.ShortDescription; forge.VariantName = variant.Title; forge.Save(); } if (User.IsInRole("Mod")) { variant.IsStaffPick = model.IsStaffPick; } GameMapService.Save(); SetAlert(string.Format("The forge variant is saved.", variant.Title), AlertType.Success); return(RedirectToAction("Details", new { slug = variant.Slug })); } return(View(model)); }
public ActionResult Forge(ViewModels.UploadViewModel model) { Response.TrySkipIisCustomErrors = true; if (ModelState.IsValid) { var currentUser = UserManager.FindById(User.Identity.GetUserId<int>()); Models.GameMapVariant variant = new Models.GameMapVariant(); variant.Title = model.Title.Replace("\0", ""); variant.ShortDescription = model.Description; variant.Description = model.Content; variant.CreatedOn = DateTime.UtcNow; variant.AuthorId = User.Identity.GetUserId<int>(); variant.File = new Models.File() { FileSize = model.File.ContentLength, FileName = Guid.NewGuid().ToString() + ".vrt", UploadedOn = variant.CreatedOn, MD5 = model.File.InputStream.ToMD5() }; // Validate the variant to see if it's not a duplicate. var validateMap = GameMapService.ValidateHash(variant.File.MD5); if (validateMap != null) { Response.StatusCode = 400; return Content(string.Format( "<b>Keep it Clean!</b> The forge variant has already been uploaded: <a target=\"_blank\" href=\"{0}\">{1}</a>.", Url.Action("Details", "Forge", new { slug = validateMap.Slug }), validateMap.Title)); } /* Read the map type from the uploaded file. * This is also a validation message to make sure * that the file uploaded is an actual map. */ var detectType = VariantDetector.Detect(model.File.InputStream); if (detectType == VariantType.Invalid) { // The given map doesn't exist. model.File.InputStream.Close(); Response.StatusCode = 400; return Content("<b>Keep it Clean!</b> The file uploaded is invalid. Please make sure the map is in the new format."); } else if (detectType == VariantType.GameVariant) { // The given map doesn't exist. model.File.InputStream.Close(); Response.StatusCode = 400; return Content("<strong>PARDON OUR DUST!</strong> Can't upload game variant as forge variant."); } string path = System.IO.Path.Combine(Server.MapPath("~/Content/Files/Forge/"), variant.File.FileName); using (var stream = new System.IO.MemoryStream()) { model.File.InputStream.Seek(0, System.IO.SeekOrigin.Begin); model.File.InputStream.CopyTo(stream); ForgeVariant variantItem = new ForgeVariant(stream); var map = GameMapService.GetMapByInternalId(variantItem.MapId); if (map != null) { variant.GameMapId = map.Id; variantItem.VariantName = model.Title; //variantItem.VariantAuthor = currentUser.UploaderName; variantItem.VariantDescription = variant.ShortDescription; variantItem.Save(); // Save the file. using (var fileStream = System.IO.File.Create(path)) { stream.Seek(0, System.IO.SeekOrigin.Begin); stream.CopyTo(fileStream); } } else { Response.StatusCode = 400; return Content("<strong>PARDON OUR DUST!</strong> We currently do not support the uploaded map."); } } GameMapService.AddVariant(variant); GameMapService.Save(); return Content(Url.Action("Details", "Forge", new { slug = variant.Slug })); } Response.StatusCode = 400; return View("~/Views/Shared/_ModelState.cshtml"); }
public void AddVariant(Models.GameMapVariant model) { db.GameMapVariants.Add(model); }
public ActionResult Forge(ViewModels.UploadViewModel model) { Response.TrySkipIisCustomErrors = true; if (ModelState.IsValid) { var currentUser = UserManager.FindById(User.Identity.GetUserId <int>()); Models.GameMapVariant variant = new Models.GameMapVariant(); variant.Title = model.Title.Replace("\0", ""); variant.ShortDescription = model.Description; variant.Description = model.Content; variant.CreatedOn = DateTime.UtcNow; variant.AuthorId = User.Identity.GetUserId <int>(); variant.File = new Models.File() { FileSize = model.File.ContentLength, FileName = Guid.NewGuid().ToString() + ".vrt", UploadedOn = variant.CreatedOn, MD5 = model.File.InputStream.ToMD5() }; // Validate the variant to see if it's not a duplicate. var validateMap = GameMapService.ValidateHash(variant.File.MD5); if (validateMap != null) { Response.StatusCode = 400; return(Content(string.Format( "<b>Keep it Clean!</b> The forge variant has already been uploaded: <a target=\"_blank\" href=\"{0}\">{1}</a>.", Url.Action("Details", "Forge", new { slug = validateMap.Slug }), validateMap.Title))); } /* Read the map type from the uploaded file. * This is also a validation message to make sure * that the file uploaded is an actual map. */ var detectType = VariantDetector.Detect(model.File.InputStream); if (detectType == VariantType.Invalid) { // The given map doesn't exist. model.File.InputStream.Close(); Response.StatusCode = 400; return(Content("<b>Keep it Clean!</b> The file uploaded is invalid. Please make sure the map is in the new format.")); } else if (detectType == VariantType.GameVariant) { // The given map doesn't exist. model.File.InputStream.Close(); Response.StatusCode = 400; return(Content("<strong>PARDON OUR DUST!</strong> Can't upload game variant as forge variant.")); } string path = System.IO.Path.Combine(Server.MapPath("~/Content/Files/Forge/"), variant.File.FileName); using (var stream = new System.IO.MemoryStream()) { model.File.InputStream.Seek(0, System.IO.SeekOrigin.Begin); model.File.InputStream.CopyTo(stream); ForgeVariant variantItem = new ForgeVariant(stream); var map = GameMapService.GetMapByInternalId(variantItem.MapId); if (map != null) { variant.GameMapId = map.Id; variantItem.VariantName = model.Title; //variantItem.VariantAuthor = currentUser.UploaderName; variantItem.VariantDescription = variant.ShortDescription; variantItem.Save(); // Save the file. using (var fileStream = System.IO.File.Create(path)) { stream.Seek(0, System.IO.SeekOrigin.Begin); stream.CopyTo(fileStream); } } else { Response.StatusCode = 400; return(Content("<strong>PARDON OUR DUST!</strong> We currently do not support the uploaded map.")); } } GameMapService.AddVariant(variant); GameMapService.Save(); return(Content(Url.Action("Details", "Forge", new { slug = variant.Slug }))); } Response.StatusCode = 400; return(View("~/Views/Shared/_ModelState.cshtml")); }