private OPCProblemDetails _Validate(SurveyPAModel model) { #region Test Data //ValidationProblemDetails valid = new ValidationProblemDetails(); //valid.Detail = "There is errors with the validation, see error list"; //valid.Title = "Validation errors"; //valid.Errors.Add("mykey", new string[] { "value1", "value2" }); //valid.Errors.Add("another mykey", new string[] { "more value1", "stuff" }); //return BadRequest(valid); //throw new Exception("this is a test exception", new Exception("this is the inner exception")); #endregion var validator = new SurveyPAModelValidator(_localizer); var results = validator.Validate(model); if (!results.IsValid) { OPCProblemDetails valid = new OPCProblemDetails(); valid.Detail = "There is errors with the validation, see error list"; valid.Title = "Validation errors"; foreach (var error in results.Errors) { valid.AddError(error.PropertyName, error.ErrorMessage); } return(valid); } return(null); }
public IActionResult Validate([FromBody] SurveyPAModel model, [FromQuery] string complaintId) { OPCProblemDetails problems = _Validate(model); if (problems != null) { return(BadRequest(problems)); } return(Ok()); }
public IActionResult Complete([FromBody] SurveyPAModel model, [FromQuery] string complaintId) { OPCProblemDetails problems = _Validate(model); if (problems != null) { return(BadRequest(problems)); } return(Ok(new { ReferenceNumber = Guid.NewGuid().ToString() })); }
public IActionResult Get([FromQuery] string complaintId, [FromQuery] string fileUniqueId, [FromQuery] string fileName) { // NOTE: The complaintId should probably be used to validate that this is a proper request try { // Files are organized by complaint id in the folder FileUploads var folderName = Path.Combine("FileUploads", fileUniqueId); var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); var fullPath = Path.Combine(pathToSave, fileName); var net = new System.Net.WebClient(); var data = net.DownloadData(fullPath); var content = new System.IO.MemoryStream(data); var contentType = "APPLICATION/octet-stream"; return(File(content, contentType, fileName)); } catch (WebException) { // TODO: Log this somewhere OPCProblemDetails problem = new OPCProblemDetails { Detail = "The file is not found or has been removed from the server", Status = 400, Title = "" }; return(BadRequest(problem)); } catch (Exception) { // TODO: Log this somewhere OPCProblemDetails problem = new OPCProblemDetails { Detail = "The file is empty", Status = 400, Title = "" }; return(BadRequest(problem)); } }
private OPCProblemDetails _Validate(SurveyContactInfoModel model) { var validator = new SurveyContactInfoModelValidator(_localizer); var results = validator.Validate(model); if (!results.IsValid) { OPCProblemDetails valid = new OPCProblemDetails(); valid.Detail = "There is errors with the validation, see error list"; valid.Title = "Validation errors"; foreach (var error in results.Errors) { valid.AddError(error.PropertyName, error.ErrorMessage); } return(valid); } return(null); }
public IActionResult Validate([FromBody] SurveyPipedaModel model, [FromQuery] string complaintId) { var validator = new SurveyPipedaModelValidator(_localizer); var results = validator.Validate(model); if (!results.IsValid) { OPCProblemDetails valid = new OPCProblemDetails(); valid.Detail = "There is errors with the validation, see error list"; valid.Title = "Validation errors"; foreach (var error in results.Errors) { //valid.Errors.Add(error., new string[] { "more value1", "stuff" }); valid.AddError(error.PropertyName, error.ErrorMessage); } return(BadRequest(valid)); } return(Ok()); }
public async Task <IActionResult> Upload(string complaintI, string questionName) { // NOTE: The complaintId should probably be used to validate that this is a proper request try { var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), FileUploadeFolderName); var files = new Dictionary <string, SurveyFile>(); OPCProblemDetails problem = new OPCProblemDetails { Status = 400 }; foreach (var file in Request.Form.Files) { var extension = Path.GetExtension(file.FileName); if (!_allowedFileTypes.Contains(extension)) { problem.Errors.Add(questionName, new List <string> { $"Extension {extension} not allowed" }); continue; } if (file.Length < 0) { problem.Errors.Add(questionName, new List <string> { "The file is empty" }); continue; } var uniqueId = Guid.NewGuid().ToString(); var folder = Path.Combine(pathToSave, uniqueId); var fullPath = Path.Combine(folder, file.FileName); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } files.Add(file.FileName, new SurveyFile { name = file.FileName, type = file.ContentType, content = uniqueId, size = file.Length }); } if (problem.Errors.Count > 0) { return(BadRequest(problem)); } return(Ok(files)); } catch (Exception ex) { // TODO: Log this somewhere return(StatusCode(500, $"Internal server error: {ex}")); } }
public IActionResult ValidateAttachments([FromBody] PAFilePageData files, [FromQuery] string complaintId) { //throw new Exception("this is a test exception", new Exception("this is the inner exception")); //ValidationProblemDetails valid = new ValidationProblemDetails(); //valid.Detail = "There is errors with the validation, see error list"; //valid.Title = "Validation errors"; //valid.Errors.Add("mykey", new string[] { "value1", "value2" }); //valid.Errors.Add("another mykey", new string[] { "more value1", "stuff" }); //return BadRequest(valid); if (files.Documentation_type == "upload" || files.Documentation_type == "both") { List <SurveyFile> allFiles = new List <SurveyFile>(); if (files.Documentation_file_upload != null) { allFiles.AddRange(files.Documentation_file_upload); } if (files.Documentation_file_upload_rep != null) { allFiles.AddRange(files.Documentation_file_upload_rep); } if (allFiles.Count > 0) { long totalSizes = 0; long multipleFileMaxSize = 26214400; var folderName = Path.Combine("FileUploads", complaintId); var folderpath = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (!Directory.Exists(folderpath)) { Directory.CreateDirectory(folderpath); } DirectoryInfo directory = new DirectoryInfo(folderpath); FileInfo[] filesStored = directory.GetFiles(); // We need to make sure the list of files sent to us saved in local storage (in other words the files the users think he is // uploading) are all saved on the server properly with the right size OPCProblemDetails fileMissingProblem = new OPCProblemDetails { Status = 400, Detail = _localizer.GetLocalizedStringSharedResource("FileNotFound"), Title = _localizer.GetLocalizedStringSharedResource("ValidationIssues") }; foreach (SurveyFile file in allFiles) { long.TryParse(file.content, out long fileSize); if (fileSize == 0 || filesStored.Where(f => f.Name == file.name && f.Length == fileSize).Any() == false) { fileMissingProblem.AddError(_localizer.GetLocalizedStringSharedResource("FileNotFound"), string.Format(_localizer.GetLocalizedStringSharedResource("FileMissing"), file.name)); } else { // We are adding to the total file size that we will process later totalSizes += fileSize; } } if (fileMissingProblem.Errors.Count > 0) { return(BadRequest(fileMissingProblem)); } // Next step is to validate for total file size. if (totalSizes > multipleFileMaxSize) { OPCProblemDetails problem = new OPCProblemDetails { Detail = _localizer.GetLocalizedStringSharedResource("SizeOfFilesExceeded"), Status = 400, Title = _localizer.GetLocalizedStringSharedResource("ValidationIssues") }; problem.Errors.Add(_localizer.GetLocalizedStringSharedResource("Attachments"), new List <string>() { _localizer.GetLocalizedStringSharedResource("SizeOfFilesExceeded") }); return(BadRequest(problem)); } } else { OPCProblemDetails problem = new OPCProblemDetails { Detail = "There is no files uploaded", Status = 400, Title = _localizer.GetLocalizedStringSharedResource("ValidationIssues") }; return(BadRequest(problem)); } } return(Ok()); }