Files
raven/server/AyaNova/Controllers/ReportController.cs
2020-08-25 19:18:36 +00:00

197 lines
8.1 KiB
C#

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Logging;
using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
using AyaNova.Util;
using PuppeteerSharp;
namespace AyaNova.Api.Controllers
{
[ApiController]
[ApiVersion("8.0")]
[Route("api/v{version:apiVersion}/report")]
[Produces("application/json")]
[Authorize]
public class ReportController : ControllerBase
{
private readonly AyContext ct;
private readonly ILogger<ReportController> log;
private readonly ApiServerState serverState;
/// <summary>
/// ctor
/// </summary>
/// <param name="dbcontext"></param>
/// <param name="logger"></param>
/// <param name="apiServerState"></param>
public ReportController(AyContext dbcontext, ILogger<ReportController> logger, ApiServerState apiServerState)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
}
[HttpGet("render-test")]
[AllowAnonymous]
public async Task<IActionResult> GetTestReport([FromRoute] string test)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
string outputFile = FileUtil.NewRandomTempFilesFolderFileName;
switch (test)
{
case "chrome-reddit-to-pdf":
//first test, just render a web page to pdf and return it
//return PhysicalFile(filePath, mimetype, dbObject.DisplayFileName);
outputFile += ".pdf";
//http://www.puppeteersharp.com/api/index.html
//https://github.com/hardkoded/puppeteer-sharp
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
});
var page = await browser.NewPageAsync();
await page.GoToAsync("https://github.com/hardkoded/puppeteer-sharp");
await page.PdfAsync(outputFile);
return PhysicalFile(outputFile, "application/pdf");
}
return NotFound(test);
}
//---------------------------------------------------------------------
[HttpGet("poc")]
[AllowAnonymous]
public async Task<IActionResult> ProofOfConcept([FromRoute] string url)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
var httpConnectionFeature = HttpContext.Features.Get<IHttpConnectionFeature>();
var API_URL = $"http://127.0.0.1:{httpConnectionFeature.LocalPort}/api/v8/";
//var localIpAddress = httpConnectionFeature?.LocalIpAddress;
//todo: validate files are present somehow?
var ReportJSFolderPath = Path.Combine(ServerBootConfig.AYANOVA_CONTENT_ROOT_PATH, "resource", "rpt");
if (!Directory.Exists(ReportJSFolderPath))
throw new System.Exception($"E1012: \"reportjs\" folder not found where expected: \"{ReportJSFolderPath}\", installation damaged?");
//sample CSS
var reportCSS = @"
@page {
margin: 1cm;
}
@page :first {
margin: 2cm;
}
.ay-red {
color:red;
}
.ay-blue {
color:blue;
}
";
//sample template
//var reportTemplate = "'<div>{{#with person}}<span class=\"ay-red\">{{firstname}}</span> {{aycaps lastname}}{{/with}}</div>'";
var reportTemplate = "'<!DOCTYPE html><html><head><title>test title</title></head><body> <img src=\"{{aylogo.medium}}\" />" +
"<h1>Test page top</h1><p class=\"ay-blue\">A blue paragraph</p><div>{{#with person}}<span class=\"ay-red\">{{firstname}}</span> {{aycaps lastname}}{{/with}}</div>" +
"<br/><br/><h4>Some markdown</h4><div>{{{aymarkdown mdtest}}}</div></body></html>'";
//data object
var aylogo = $"{{small:'{API_URL}logo/small',medium:'{API_URL}logo/medium',large:'{API_URL}logo/large'}}";
var reportData = "{ person: { firstname: 'Tyler', lastname: 'Mott' },aylogo:[AYLOGO], mdtest:'| CODE | MEANING |\\n| ----- | ------------------------------ |\\n| E1000 | Could not connect to the database specified in the [connection string](ops-config-db.md). |\\n| E1050 | XXXXXXXX |\\n| E1012 | Missing resource folder. AyaNova was started from the wrong location or was not installed properly. |\\n' }";
reportData = reportData.Replace("[AYLOGO]", aylogo);
log.LogInformation($"setting Chrome launchoptions for os:");
var lo = new LaunchOptions { Headless = true };
bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
if (!isWindows)
{
log.LogInformation($"IS NOT WINDOWS: setting executable path for chrome");
lo.ExecutablePath = "/usr/bin/chromium-browser";
lo.Args = new string[] { "--no-sandbox" };
}
else
{
log.LogInformation($"IS WINDOWS: Calling browserFetcher download async now:");
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
}
log.LogInformation($"Calling browser.launchAsync now:");
using (var browser = await Puppeteer.LaunchAsync(lo))
using (var page = await browser.NewPageAsync())
{
log.LogInformation($"In using for page ops adding scripts and report now:");
//Add handlebars JS for compiling and presenting
await page.AddScriptTagAsync(new AddTagOptions() { Path = Path.Combine(ReportJSFolderPath, "ay-hb.js") });
//add marked for markdown processing
await page.AddScriptTagAsync(new AddTagOptions() { Path = Path.Combine(ReportJSFolderPath, "ay-md.js") });
//test add helpers
await page.AddScriptTagAsync(new AddTagOptions() { Path = Path.Combine(ReportJSFolderPath, "ay-report.js") });
//execute to add to handlebars
await page.EvaluateExpressionAsync("ayRegisterHelpers();");
//compile and run handlebars template
var compileScript = $"Handlebars.compile({reportTemplate})({reportData});";
var resultHTML = await page.EvaluateExpressionAsync<string>(compileScript);
//render report as HTML
await page.SetContentAsync(resultHTML);
//add style (after page or it won't work)
await page.AddStyleTagAsync(new AddTagOptions { Content = reportCSS });
//useful for debugging purposes only
//var pagecontent = await page.GetContentAsync();
//render to pdf and return
var pdfBuffer = await page.PdfDataAsync();
log.LogInformation($"returning results now:");
return new FileContentResult(pdfBuffer, "application/pdf");
}
}
//-----------------------------------------
//------------
/*
NOTES/TODO during testing
Need job to automatically erase any temp files older than 5 minutes (or whatever)
*/
}//eoc
}//eons