apc-viewer/APC Viewer/Controllers/HomeController.cs

300 lines
12 KiB
C#

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<HomeController>();
}
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<int, object, object, string?> 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("<html><body><table border = '1'>");
if (string.IsNullOrEmpty(pdsfFile))
throw new Exception("<tr><td>Invalid input</td></tr>");
else if (!System.IO.File.Exists(pdsfFile))
_ = result.AppendLine("<tr><td>File doesn't exist!</td></tr>");
else
{
bool header = true;
bool body = false;
bool footer = false;
string logisticsSegment;
List<string> 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("</table><hr /><table border = '1'>");
}
}
if (pdsfLine.StartsWith("NUM_DATA_ROWS") || pdsfLine.StartsWith("END_HEADER"))
{
body = false;
footer = true;
_ = result.AppendLine("</table><hr /><table border = '1'>");
}
if (header)
_ = result.Append("<tr><td>").Append(pdsfLine.Replace("\t", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else if (body)
_ = result.Append("<tr><td>").Append(pdsfLine.Replace("\t", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else if (footer)
{
if (pdsfLine.StartsWith("DELIMITER"))
_ = result.Append("<tr><td>").Append(pdsfLine.Replace(";", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else
_ = result.Append("<tr><td>").Append(pdsfLine.Replace("\t", "</td><td>").Replace("=", "</td><td>").Replace(";", "</td><td>")).AppendLine("&nbsp;</td></tr>");
}
else
throw new Exception();
if (pdsfLine.StartsWith("END_OFFSET"))
{
header = false;
body = true;
_ = result.AppendLine("</table><hr /><table border = '1'>");
}
}
}
_ = result.AppendLine("</table></body>");
return result;
}
public ContentResult ViewPDSF(string? id = null)
{
string pdsfFile;
StringBuilder result = new();
if (string.IsNullOrEmpty(id) || !id.Contains('_'))
_ = result.AppendLine("<html><body><table border = '1'><tr><td>A) Error: Invalid input</td></tr></table></body>");
else
{
if (!long.TryParse(id.Split('_')[1], out long sequence))
_ = result.AppendLine("<html><body><table border = '1'><tr><td>B) Error: Invalid input</td></tr></table></body>");
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<int, object, object, string?> 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("<html><body><table border = '1'>");
if (string.IsNullOrEmpty(ipdsfFile))
throw new Exception("<tr><td>Invalid input</td></tr>");
else if (!System.IO.File.Exists(ipdsfFile))
_ = result.AppendLine("<tr><td>File doesn't exist!</td></tr>");
else
{
bool header = true;
bool body = false;
bool footer = false;
string logisticsSegment;
List<string> 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("</table><hr /><table border = '1'>");
}
}
if (ipdsfLine.StartsWith("NUM_DATA_ROWS") || ipdsfLine.StartsWith("END_HEADER"))
{
body = false;
footer = true;
_ = result.AppendLine("</table><hr /><table border = '1'>");
}
if (header)
_ = result.Append("<tr><td>").Append(ipdsfLine.Replace("\t", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else if (body)
_ = result.Append("<tr><td>").Append(ipdsfLine.Replace("\t", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else if (footer)
{
if (ipdsfLine.StartsWith("DELIMITER"))
_ = result.Append("<tr><td>").Append(ipdsfLine.Replace(";", "</td><td>")).AppendLine("&nbsp;</td></tr>");
else
_ = result.Append("<tr><td>").Append(ipdsfLine.Replace("\t", "</td><td>").Replace("=", "</td><td>").Replace(";", "</td><td>")).AppendLine("&nbsp;</td></tr>");
}
else
throw new Exception();
if (ipdsfLine.StartsWith("END_OFFSET"))
{
header = false;
body = true;
_ = result.AppendLine("</table><hr /><table border = '1'>");
}
}
}
_ = result.AppendLine("</table></body>");
return result;
}
public ContentResult ViewIPDSF(string? id = null)
{
string ipdsfFile;
StringBuilder result = new();
if (string.IsNullOrEmpty(id) || !id.Contains('_'))
_ = result.AppendLine("<html><body><table border = '1'><tr><td>A) Error: Invalid input</td></tr></table></body>");
else
{
if (!long.TryParse(id.Split('_')[1], out long sequence))
_ = result.AppendLine("<html><body><table border = '1'><tr><td>B) Error: Invalid input</td></tr></table></body>");
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<string[]>, List<string[]>> tuple = _Background.GetTimePivot(isGaN: is_gaN, isSi: is_Si);
ViewBag.forIPDSF = tuple.Item1;
ViewBag.forPDSF = tuple.Item2;
return View();
}
}