-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRootController.cs
More file actions
98 lines (83 loc) · 3.16 KB
/
RootController.cs
File metadata and controls
98 lines (83 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Diagnostics;
using System.Net.Mime;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using Pdf2Html.Settings;
namespace Pdf2Html.Controllers;
[ApiController]
[Route("/")]
public class RootController(ILogger<RootController> logger, ConversionOptions conversionOptions) : ControllerBase
{
[HttpGet]
public ActionResult Get()
{
var versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
return Ok(new
{
name = versionInfo.ProductName,
version = versionInfo.ProductVersion
});
}
[HttpPost]
[DisableRequestSizeLimit]
public async Task<ActionResult> Post()
{
var inputFile = Path.GetTempFileName();
var outputFile = $"{inputFile}.html";
try
{
await using (var tempFileStream = System.IO.File.Open(inputFile, FileMode.Truncate))
{
await Request.Body.CopyToAsync(tempFileStream);
logger.LogInformation($"Copied {FormatToMb(new FileInfo(inputFile).Length)} to {inputFile}");
}
logger.LogInformation("Starting conversion...");
var (success, logs) = await ConvertAsync(inputFile, outputFile);
if (!success)
{
logger.LogError("Conversion failed");
return StatusCode(StatusCodes.Status500InternalServerError, new { pdf2htmlEX = new { logs } });
}
logger.LogInformation($"Conversion completed ({FormatToMb(new FileInfo(outputFile).Length)} written to {outputFile})");
return File(await System.IO.File.ReadAllBytesAsync(outputFile), MediaTypeNames.Text.Html);
}
finally
{
// TODO: make this configurable
logger.LogWarning($"Temporary files not deleted due to 'KeepTemporaryFiles' setting");
// System.IO.File.Delete(inputFile);
// System.IO.File.Delete(outputFile);
}
}
private async Task<(bool Success, ICollection<string> logs)> ConvertAsync(string inputFile, string outputFile)
{
using var p = new Process();
p.StartInfo = new ProcessStartInfo
{
FileName = "pdf2htmlEX",
Arguments = $"{conversionOptions.CommandLineArguments} --dest-dir={Path.GetDirectoryName(outputFile)} {inputFile} {Path.GetFileName(outputFile)}",
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var logs = new List<string>();
void AddLog(string? log)
{
if (string.IsNullOrEmpty(log))
{
return;
}
logs.Add(log);
logger.LogInformation(log);
}
p.OutputDataReceived += (_, e) => AddLog(e.Data);
p.ErrorDataReceived += (_, e) => AddLog(e.Data);
p.EnableRaisingEvents = true;
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
await p.WaitForExitAsync();
return (p.ExitCode == 0, logs);
}
private static string FormatToMb(long bytesLength) => (bytesLength / 1024.0 / 1024.0).ToString("0.00 MB");
}