using APCViewer.Models; using APCViewer.Models.Methods; using APCViewer.Singleton; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using System.Web; namespace APCViewer.Controllers; public class HomeController : Controller, IHomeController { private readonly Serilog.ILogger _Log; private readonly Background _Background; private readonly IBackground _BackgroundMethods; public HomeController(Background background) { _Background = background; _BackgroundMethods = background; _Log = Serilog.Log.ForContext(); } public ActionResult Index() => View(); public ActionResult Privacy() => View(); [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public ActionResult Error() => View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); public ActionResult Encode(string value = "") { string result = string.Empty; if (!string.IsNullOrEmpty(value)) result = HttpUtility.UrlEncode(value); return Content(result, "text/plain"); } public ActionResult TestTry() { ActionResult result; try { throw new Exception("This is a test"); } catch (Exception ex) { result = Content(string.Concat(ex.Message, Environment.NewLine, Environment.NewLine, ex.StackTrace)); } return result; } public ActionResult PDSF(string? directory = null, string? filter = null, bool is_gaN = false, bool is_Si = false) { Tuple tuple = _Background.SetViewBag(directory, filter, isGaN: is_gaN, isSi: is_Si, forPDSF: true); ViewBag.Files = tuple.Item1; ViewBag.Grouped = tuple.Item2; ViewBag.Sorted = tuple.Item3; ViewBag.Directory = tuple.Item4; return View(); } private static StringBuilder GetPDSFHtml(string? pdsfFile) { StringBuilder result = new(); _ = result.AppendLine(""); if (string.IsNullOrEmpty(pdsfFile)) throw new Exception(""); else if (!System.IO.File.Exists(pdsfFile)) _ = result.AppendLine(""); else { bool header = true; bool body = false; bool footer = false; string logisticsSegment; List logistics = new(); string[] pdsfLines = System.IO.File.ReadAllLines(pdsfFile); foreach (string pdsfLine in pdsfLines) { if (pdsfLine.StartsWith("LOGISTICS_")) { logisticsSegment = pdsfLine.Split('\t')[0]; if (!logistics.Contains(logisticsSegment)) { logistics.Add(logisticsSegment); _ = result.AppendLine("
Invalid input
File doesn't exist!

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

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

"); } } } _ = result.AppendLine("
"); return result; } public ContentResult ViewPDSF(string? id = null) { string pdsfFile; 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 { pdsfFile = _Background.GetPDSF(sequence); result = GetPDSFHtml(pdsfFile); } } return Content(result.ToString(), "text/html"); } public ActionResult DownloadPDSF(string? id = null) { string pdsfFile; 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 pdsfFile = _Background.GetPDSF(sequence); } return File(pdsfFile, "text/plain", Path.GetFileName(pdsfFile)); } public ContentResult ViewCustomPDSF(string? pdsf_file = null) { StringBuilder result = GetPDSFHtml(pdsf_file); return Content(result.ToString(), "text/html"); } public ActionResult DownloadCustomPDSF(string? pdsf_file = null) { if (string.IsNullOrEmpty(pdsf_file)) throw new Exception("Error: Invalid input"); else if (!System.IO.File.Exists(pdsf_file)) throw new Exception("Error: file does not exist"); return File(pdsf_file, "text/plain", Path.GetFileName(pdsf_file)); } public ActionResult IPDSF(string? directory = null, string? filter = null, bool is_gaN = false, bool is_Si = false) { Tuple tuple = _Background.SetViewBag(directory, filter, isGaN: is_gaN, isSi: is_Si, forIPDSF: true); ViewBag.Files = tuple.Item1; ViewBag.Grouped = tuple.Item2; ViewBag.Sorted = tuple.Item3; ViewBag.Directory = tuple.Item4; return View(); } 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 ViewIPDSF(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 ActionResult DownloadIPDSF(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 ViewCustomIPDSF(string? ipdsf_file = null) { StringBuilder result = GetIPDSFHtml(ipdsf_file); return Content(result.ToString(), "text/html"); } public ActionResult DownloadCustomIPDSF(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)); } public ActionResult TimePivot(bool is_gaN = false, bool is_Si = false) { Tuple, List> tuple = _Background.GetTimePivot(isGaN: is_gaN, isSi: is_Si); ViewBag.forIPDSF = tuple.Item1; ViewBag.forPDSF = tuple.Item2; return View(); } }