public ActionResult Index(HttpPostedFileBase[] files) { ViewBag.UploadStatus = ""; if (ModelState.IsValid) { IUploadFileBLL uploadFileBLL = new UploadFileBLL(); ActionResultReturnDTO <FileInfo> actionResultReturnDTO = uploadFileBLL.ValidateUsersList(files); if (actionResultReturnDTO.HasErrors) { ViewBag.UploadStatus = actionResultReturnDTO.UserMessage; return(View()); } ViewBag.UploadStatus = "Procesamiento exitoso"; byte[] fileData = null; using (BinaryReader binaryReader = new BinaryReader(actionResultReturnDTO.ResultData.OpenRead())) { fileData = binaryReader.ReadBytes((int)actionResultReturnDTO.ResultData.Length); } return(File(fileData, "text/plain", Constants.ResultsFileName)); } return(View()); }
/// <summary> /// Remove letters from list letting read a letter once /// </summary> /// <param name="listData">List contents data</param> /// <param name="currentUser">Current user</param> /// <returns>ActionResultReturnDTO (T as IList (T as string)) with new list content data</returns> private ActionResultReturnDTO <IList <string> > RemoveLettersFromListData(IList <string> listData, string currentUser) { ActionResultReturnDTO <IList <string> > actionResultReturnDTO = new ActionResultReturnDTO <IList <string> >(); try { if (listData.Count == 0) { actionResultReturnDTO.ResultData = listData; return(actionResultReturnDTO); } for (int i = 0; i < currentUser.Length; i++) { listData.RemoveAt(listData.IndexOf(currentUser[i].ToString())); } actionResultReturnDTO.ResultData = listData; } catch (Exception ex) { actionResultReturnDTO.SetError("Ocurrió un error buscando los usuarios en la lista", ex.ToString()); } return(actionResultReturnDTO); }
/// <summary> /// Get report from users searched in files /// </summary> /// <returns>ActionResultReturnDTO (T as FileInfo) to download file</returns> private ActionResultReturnDTO <FileInfo> GetUsersList() { ActionResultReturnDTO <FileInfo> actionResultReturnDTO = new ActionResultReturnDTO <FileInfo>(); try { DirectoryInfo directoryInfo = new DirectoryInfo(ServerFilesFolderPath); FileInfo[] filesFromFolder = directoryInfo.GetFiles(); IList <string> fileDataList = new List <string>(); IList <string> fileUsersList = new List <string>(); foreach (FileInfo currentFileInfo in filesFromFolder) { string filename = currentFileInfo.Name; using (StreamReader currentFileStream = new StreamReader(ServerFilesFolderPath + filename)) { string fileContent = currentFileStream.ReadToEnd(); fileContent = fileContent.Replace("\r", ""); if (filename.Equals(Constants.DataListFileName)) { fileDataList = fileContent.Split('\n').ToList(); } else if (filename.Equals(Constants.UsersListFileName)) { fileUsersList = fileContent.Split('\n').ToList(); } } } actionResultReturnDTO = EvalUsersList(fileUsersList, fileDataList); } catch (Exception ex) { actionResultReturnDTO.SetError("Ocurrió un error validando los usuarios", ex.ToString()); } return(actionResultReturnDTO); }
/// <summary> /// Validate if newshore user exists in list /// </summary> /// <param name="files">Array of files uploaded</param> /// <returns>ActionResultReturnDTO (T as FileInfo) to download file</returns> public ActionResultReturnDTO <FileInfo> ValidateUsersList(HttpPostedFileBase[] files) { ActionResultReturnDTO <FileInfo> actionResultReturnDTO = new ActionResultReturnDTO <FileInfo>(); try { ActionResultDTO actionResultDTO = UploadFilesToServer(files); if (actionResultDTO.HasErrors) { actionResultReturnDTO.SetError(actionResultDTO.UserMessage, actionResultDTO.StackTraceMessage); return(actionResultReturnDTO); } actionResultReturnDTO = GetUsersList(); } catch (Exception ex) { actionResultReturnDTO.SetError("Ocurrió un error tratanto de validar los usuarios", ex.ToString()); } return(actionResultReturnDTO); }
/// <summary> /// Eval users list with contents list to find current users /// </summary> /// <param name="usersList">Users list</param> /// <param name="listData">List data</param> /// <returns>ActionResultReturnDTO (T as FileInfo) to download file</returns> private ActionResultReturnDTO <FileInfo> EvalUsersList(IList <string> usersList, IList <string> listData) { ActionResultReturnDTO <FileInfo> actionResultReturnDTO = new ActionResultReturnDTO <FileInfo>(); try { string path = ServerFilesFolderPath + Constants.ResultsFileName; DirectoryInfo directoryInfo = new DirectoryInfo(ServerFilesFolderPath); File.Create(path).Dispose(); using (StreamWriter fileStream = new StreamWriter(path, true)) { for (int i = 0; i < usersList.Count; i++) { string currentUser = usersList[i]; if (string.IsNullOrEmpty(currentUser)) { continue; } fileStream.Write(string.Format("{0}-->", currentUser)); string userFounded = string.Empty; for (int j = 0; j < currentUser.Length; j++) { string currentUserLetter = currentUser[j].ToString(); for (int k = 0; k < listData.Count; k++) { if (currentUserLetter.Equals(listData[k])) { userFounded += listData[k]; break; } } } if (currentUser.Equals(userFounded)) { fileStream.Write("Si Existe"); ActionResultReturnDTO <IList <string> > actionResultReturnDTORemoveLetters = RemoveLettersFromListData(listData, userFounded); if (actionResultReturnDTORemoveLetters.HasErrors) { actionResultReturnDTO.SetError(actionResultReturnDTORemoveLetters.UserMessage, actionResultReturnDTORemoveLetters.StackTraceMessage); return(actionResultReturnDTO); } listData = actionResultReturnDTORemoveLetters.ResultData; } else { fileStream.Write("No Existe"); } fileStream.WriteLine(); } } FileInfo resultsFile = directoryInfo.GetFiles() .Where(x => x.Name.Equals(Constants.ResultsFileName)) .FirstOrDefault(); actionResultReturnDTO.ResultData = resultsFile; } catch (Exception ex) { actionResultReturnDTO.SetError("Ocurrió un error buscando los usuarios en la lista", ex.ToString()); } return(actionResultReturnDTO); }