using APCViewer.Models.Stateless.Methods; using IFX.Shared; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Serilog.Context; using Shared; using System.Text; using System.Text.Json; namespace APCViewer.Pages; public class IPDSFPage : PageModel, Models.Properties.IIPDSFPage, IIPDSFPage { protected string? _Directory; protected int _Files; protected Dictionary>> _Grouped; protected List> _Sorted; public string? Directory => _Directory; public int Files => _Files; public Dictionary>> Grouped => _Grouped; public List> Sorted => _Sorted; private readonly Serilog.ILogger _Log; private readonly Singleton.Background _Background; private readonly Models.Methods.IBackground _BackgroundMethods; #nullable disable public IPDSFPage(Singleton.Background background) { _Background = background; _BackgroundMethods = background; _Log = Serilog.Log.ForContext(); } #nullable enable public override string ToString() { string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); return result; } public void OnGet(string? directory = null, string? filter = null, bool is_gaN = false, bool is_Si = false) { string? methodName = IMethodName.GetActualAsyncMethodName(); using (LogContext.PushProperty("MethodName", methodName)) { _Log.Debug("() => ..."); Tuple>>, List>, string?> tuple = _BackgroundMethods.SetViewBag(directory, filter, isGaN: is_gaN, isSi: is_Si, forIPDSF: true); _Files = tuple.Item1; _Grouped = tuple.Item2; _Sorted = tuple.Item3; _Directory = tuple.Item4; } } private static StringBuilder GetIPDSFHtml(string? ipdsfFile) { StringBuilder result = new(); _ = result.AppendLine(""); if (string.IsNullOrEmpty(ipdsfFile)) throw new Exception(""); else if (!System.IO.File.Exists(ipdsfFile)) _ = result.AppendLine(""); else { bool header = true; bool body = false; bool footer = false; string logisticsSegment; List logistics = new(); string[] ipdsfLines = System.IO.File.ReadAllLines(ipdsfFile); foreach (string ipdsfLine in ipdsfLines) { if (ipdsfLine.StartsWith("LOGISTICS_")) { logisticsSegment = ipdsfLine.Split('\t')[0]; if (!logistics.Contains(logisticsSegment)) { logistics.Add(logisticsSegment); _ = result.AppendLine("
Invalid input
File doesn't exist!

"); } } if (ipdsfLine.StartsWith("NUM_DATA_ROWS") || ipdsfLine.StartsWith("END_HEADER")) { body = false; footer = true; _ = result.AppendLine("

"); } if (header) _ = result.Append(""); else if (body) _ = result.Append(""); else if (footer) { if (ipdsfLine.StartsWith("DELIMITER")) _ = result.Append(""); else _ = result.Append(""); } else throw new Exception(); if (ipdsfLine.StartsWith("END_OFFSET")) { header = false; body = true; _ = result.AppendLine("
").Append(ipdsfLine.Replace("\t", "")).AppendLine(" 
").Append(ipdsfLine.Replace("\t", "")).AppendLine(" 
").Append(ipdsfLine.Replace(";", "")).AppendLine(" 
").Append(ipdsfLine.Replace("\t", "").Replace("=", "").Replace(";", "")).AppendLine(" 

"); } } } _ = result.AppendLine("
"); return result; } public ContentResult OnGetView(string? id = null) { string ipdsfFile; StringBuilder result = new(); if (string.IsNullOrEmpty(id) || !id.Contains('_')) _ = result.AppendLine("
A) Error: Invalid input
"); else { if (!long.TryParse(id.Split('_')[1], out long sequence)) _ = result.AppendLine("
B) Error: Invalid input
"); else { ipdsfFile = _Background.GetIPDSF(sequence); result = GetIPDSFHtml(ipdsfFile); } } return Content(result.ToString(), "text/html"); } public FileResult OnGetDownloadFile(string? id = null) { string ipdsfFile; if (string.IsNullOrEmpty(id) || !id.Contains('_')) throw new Exception("A) Error: Invalid input"); else { if (!long.TryParse(id.Split('_')[1], out long sequence)) throw new Exception("B) Error: Invalid input"); else ipdsfFile = _Background.GetIPDSF(sequence); } return File(ipdsfFile, "text/plain", Path.GetFileName(ipdsfFile)); } public ContentResult OnGetViewCustom(string? ipdsf_file = null) { StringBuilder result = GetIPDSFHtml(ipdsf_file); return Content(result.ToString(), "text/html"); } public FileResult OnGetDownloadFileCustom(string? ipdsf_file = null) { if (string.IsNullOrEmpty(ipdsf_file)) throw new Exception("Error: Invalid input"); else if (!System.IO.File.Exists(ipdsf_file)) throw new Exception("Error: file does not exist"); return File(ipdsf_file, "text/plain", Path.GetFileName(ipdsf_file)); } }