public override void OnActionExecuting(ActionExecutingContext context) { string version = (string)context.ActionArguments["version"]; string filename = (string)context.ActionArguments["filename"]; string branch = (string)context.ActionArguments["branch"]; var notValid = new ApiErrorModel(); if (filename == null) { notValid.code = "InvalidFilename"; notValid.message = "Invalid File name"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if (!ValidVersion.ValidSemver(version)) { notValid.code = "NotValidVersion"; notValid.message = "The version in the path isn't valid"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } var upwardDb = (upwardContext)context.HttpContext.RequestServices.GetService(typeof(upwardContext)); var Id = context.HttpContext.Response.Headers["X-Project-Id"]; var ver = version.Split("."); int major = int.Parse(ver[0]); int minor = int.Parse(ver[1]); int patch = int.Parse(ver[2]); var doesExists = upwardDb.Pkgfile .Any(r => r.Project.ToString() == Id.ToString() && r.Major == major && r.Minor == minor && r.Patch == patch && r.Branch == branch && r.Filename.Contains(filename)); if (!doesExists) { notValid.code = "NoSuchPackage"; notValid.message = "No package with that version or filename exists"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } }
public async Task <IActionResult> Create([FromBody] CreateModel create) { var projectName = HttpContext.Request.Host.ToString().Split(".")[0]; var project = accountsDb.Project.Where(r => r.Name == projectName).FirstOrDefault(); var hasNoRollback = await ValidVersion.NoRollback(create.Version, create.Branch, project.Id, upwardDb); var apiError = new ApiErrorModel(); if (!hasNoRollback) { apiError.code = "VersionHasRollback"; apiError.message = "The version specified in the header has a rollback"; HttpContext.Response.StatusCode = 400; return(Json(apiError)); } var ver = create.Version.Split("."); var currentDate = DateTime.Now; var newPackage = new Pkgfile { Project = project.Id, Major = int.Parse(ver[0]), Minor = int.Parse(ver[1]), Patch = int.Parse(ver[2]), Branch = create.Branch, Filename = new string[] { }, Created = currentDate }; await upwardDb.Pkgfile.AddAsync(newPackage); await upwardDb.SaveChangesAsync(); var SuccessResponse = new CreateSuccessModel { Url = $"{HttpContext.Request.Host.ToString()}/{create.Branch}/{create.Version}/", Created = currentDate.ToString(), Branch = create.Branch, Version = create.Version }; return(Json(SuccessResponse)); }
public override void OnActionExecuting(ActionExecutingContext context) { CreateModel create = (CreateModel)context.ActionArguments["create"]; var notValid = new ApiErrorModel(); if (string.IsNullOrWhiteSpace(create.Version)) { notValid.code = "NoVersion"; notValid.message = "There is no version in the body"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if (!ValidVersion.ValidSemver(create.Version)) { notValid.code = "NotValidVersion"; notValid.message = "The version in the body is not valid"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if (string.IsNullOrWhiteSpace(create.Branch)) { notValid.code = "EmptyBranch"; notValid.message = "The branch name is empty or consist of whitespace(s)"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if ((string)context.HttpContext.Request.Headers["content-type"] == null) { notValid.code = "NoContentType"; notValid.message = "Missing Content-Type in header"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } }
public override void OnActionExecuting(ActionExecutingContext context) { string version = (string)context.ActionArguments["version"]; string filename = (string)context.ActionArguments["filename"]; string branch = (string)context.ActionArguments["branch"]; var notValid = new ApiErrorModel(); if (filename == null) { notValid.code = "InvalidFilename"; notValid.message = "Invalid File name"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if (!Regex.IsMatch(filename, @"^[\w\-. ]+$")) { notValid.code = "InvalidFilename"; notValid.message = "Invalid File name"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if (!ValidVersion.ValidSemver(version)) { notValid.code = "NotValidVersion"; notValid.message = "The version in the path isn't valid"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } var upwardDb = (upwardContext)context.HttpContext.RequestServices.GetService(typeof(upwardContext)); var Id = context.HttpContext.Response.Headers["X-Project-Id"]; string contentType = context.HttpContext.Request.Headers["content-type"]; var ver = version.Split("."); int major = int.Parse(ver[0]); int minor = int.Parse(ver[1]); int patch = int.Parse(ver[2]); var pkg = upwardDb.Pkgfile .Where(r => r.Project.ToString() == Id.ToString() && r.Major == major && r.Minor == minor && r.Patch == patch && r.Branch == branch) .FirstOrDefault(); if (pkg == null) { notValid.code = "NoSuchVersion"; notValid.message = "This version hasn't been created"; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if (pkg.Filename.Contains(filename)) { notValid.code = "FileNameAlreadyExist"; notValid.message = "A same file already exist with the same name and version."; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if (string.IsNullOrEmpty(contentType)) { notValid.code = "WrongMediaType"; notValid.message = "Content-Type isn't valid."; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if (contentType.Split("/").Count() != 2) { notValid.code = "WrongMediaType"; notValid.message = "Content-Type isn't valid."; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if (context.HttpContext.Request.ContentLength == -1) { notValid.code = "MissingContentLength"; notValid.message = "Content-Length isn't valid."; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } if (context.HttpContext.Request.ContentLength > 2147483648) { notValid.code = "MissingContentLength"; notValid.message = "Upload exceeds the maximum allowed package size."; context.HttpContext.Response.StatusCode = 400; context.Result = new JsonResult(notValid); return; } }