protected void PreviewData() { lblError.Text = ""; mvPreviewResults.SetActiveView(vwPreviewResults); // default to showing results. mfbImportAircraft1.CandidatesForImport = new AircraftImportMatchRow[0]; // start fresh every time. byte[] rgb = CurrentCSVSource; if (rgb == null || rgb.Length == 0) { lblFileRequired.Text = Resources.LogbookEntry.errImportInvalidCSVFile; SetWizardStep(wsUpload); return; } // issue #280: some files have \r\r\n as line separators! rgb = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(rgb).Replace("\r\r\n", "\r\n")); // Validate the file ExternalFormatImporter efi = ExternalFormatImporter.GetImporter(rgb); if (efi != null) { rgb = efi.PreProcess(rgb); IsPendingOnly = efi.IsPendingOnly; } MemoryStream ms = new MemoryStream(rgb); try { pnlConverted.Visible = pnlAudit.Visible = false; lblAudit.Text = string.Empty; using (CSVAnalyzer csvAnalyzer = new CSVAnalyzer(ms)) { ms = null; // Avoid CA2202 CSVAnalyzer.CSVStatus result = csvAnalyzer.Status; hdnAuditState.Value = result.ToString(); if (result != CSVAnalyzer.CSVStatus.Broken) { string szCSV = null; if (efi == null) // was already CSV - only update it if it was fixed (vs. broken) { if (result == CSVAnalyzer.CSVStatus.Fixed) { szCSV = csvAnalyzer.DataAsCSV; } } else // But if it was converted, ALWAYS update the CSV. { szCSV = efi.CSVFromDataTable(csvAnalyzer.Data); } if (szCSV != null) { CurrentCSVSource = rgb = System.Text.Encoding.UTF8.GetBytes(szCSV); } // And show conversion, if it was converted if (efi != null) { lblFileWasConverted.Text = String.Format(CultureInfo.CurrentCulture, Resources.LogbookEntry.importLabelFileWasConverted, efi.Name); pnlConverted.Visible = true; } } if (result != CSVAnalyzer.CSVStatus.OK) { pnlAudit.Visible = true; } lblAudit.Text = csvAnalyzer.Audit; pnlAudit.Visible = pnlConverted.Visible || !String.IsNullOrEmpty(lblAudit.Text); } } finally { if (ms != null) { ms.Dispose(); } } ErrorContext.Clear(); CSVImporter csvimporter = CurrentImporter = new CSVImporter() { ModelNameMappings = mfbImportAircraft1.ModelMapping }; AutoFillOptions afo = ckAutofill.Checked ? new AutoFillOptions(Request.Cookies) { IncludeHeliports = true } : null; using (MemoryStream ms2 = new MemoryStream(rgb)) csvimporter.FInitFromStream(ms2, User.Identity.Name, AddSuccessRow, AddErrorRow, afo); if (csvimporter.FlightsToImport == null) { lblFileRequired.Text = csvimporter.ErrorMessage; SetWizardStep(wsUpload); return; } rptPreview.DataSource = csvimporter.FlightsToImport; rptPreview.DataBind(); mvPreview.SetActiveView(csvimporter.FlightsToImport.Count > 0 ? vwPreview : vwNoResults); mvMissingAircraft.SetActiveView(vwNoMissingAircraft); // default state. if (csvimporter.FlightsToImport.Count > 0) { if (csvimporter.HasErrors) { if (!IsPendingOnly) { lblError.Text = String.Format(CultureInfo.InvariantCulture, "<p>{0}</p><p>{1}</p>", Resources.LogbookEntry.ImportPreviewNotSuccessful, lblError.Text); } List <AircraftImportMatchRow> missing = new List <AircraftImportMatchRow>(csvimporter.MissingAircraft); if (missing.Count > 0) { mfbImportAircraft1.CandidatesForImport = missing; mvMissingAircraft.SetActiveView(vwAddMissingAircraft); } ((Button)wzImportFlights.FindControl("FinishNavigationTemplateContainerID$btnNewFile")).Visible = true; } ((AjaxControlToolkit.ConfirmButtonExtender)wzImportFlights.FindControl("FinishNavigationTemplateContainerID$confirmImportWithErrors")).Enabled = csvimporter.HasErrors; } }
public static ExternalFormatConvertResults ConvertToCSV(byte[] rgb) { ExternalFormatConvertResults result = new ExternalFormatConvertResults { // issue #280: some files have \r\r\n as line separators! _newRGB = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(rgb).Replace("\r\r\n", "\r\n")), InputStatus = CSVAnalyzer.CSVStatus.OK }; // Validate the file ExternalFormatImporter efi = ExternalFormatImporter.GetImporter(rgb); if (efi != null) { try { rgb = efi.PreProcess(rgb); result.IsPendingOnly = efi.IsPendingOnly; } catch (Exception ex) when(ex is MyFlightbookException || ex is MyFlightbookValidationException) { result.InputStatus = CSVAnalyzer.CSVStatus.Broken; result.AuditResult = ex.Message; } } if (result.InputStatus != CSVAnalyzer.CSVStatus.Broken) { using (DataTable dt = new DataTable() { Locale = CultureInfo.CurrentCulture }) { CSVAnalyzer csvAnalyzer; using (MemoryStream ms = new MemoryStream(rgb)) { csvAnalyzer = new CSVAnalyzer(ms, dt); } result.InputStatus = csvAnalyzer.Status; if (result.InputStatus != CSVAnalyzer.CSVStatus.Broken) { string szCSV = null; if (efi == null) // was already CSV - only update it if it was fixed (vs. broken) { if (result.InputStatus == CSVAnalyzer.CSVStatus.Fixed) { szCSV = csvAnalyzer.DataAsCSV; } } else // But if it was converted, ALWAYS update the CSV. { szCSV = efi.CSVFromDataTable(csvAnalyzer.Data); } if (szCSV != null) { result._newRGB = rgb = Encoding.UTF8.GetBytes(szCSV); } // And show conversion, if it was converted if (efi != null) { result.ConvertedName = efi.Name; } } result.AuditResult = csvAnalyzer.Audit; } } return(result); }