Skip to content

Commit dad17e1

Browse files
author
Ethan Bishop
committed
Extract settings object, log some app version and settings on startup
1 parent dd01764 commit dad17e1

4 files changed

Lines changed: 34 additions & 15 deletions

File tree

src/Pdf2Html/Controllers/RootController.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
using System.Diagnostics;
22
using System.Net.Mime;
33
using System.Reflection;
4-
using System.Text.RegularExpressions;
4+
55
using Microsoft.AspNetCore.Mvc;
66

7+
using Pdf2Html.Settings;
8+
79
namespace Pdf2Html.Controllers;
810

911
[ApiController]
1012
[Route("/")]
11-
public class RootController(ILogger<RootController> logger, IConfiguration configuration) : ControllerBase
13+
public class RootController(ILogger<RootController> logger, ConversionOptions conversionOptions) : ControllerBase
1214
{
1315
[HttpGet]
1416
public ActionResult Get()
@@ -57,11 +59,10 @@ public async Task<ActionResult> Post()
5759
private async Task<(bool Success, ICollection<string> logs)> ConvertAsync(string inputFile, string outputFile)
5860
{
5961
using var p = new Process();
60-
string options = ToCommandLineArguments(configuration.GetSection("ConversionOptions").AsEnumerable());
6162
p.StartInfo = new ProcessStartInfo
6263
{
6364
FileName = "pdf2htmlEX",
64-
Arguments = $"{options} --dest-dir={Path.GetDirectoryName(outputFile)} {inputFile} {Path.GetFileName(outputFile)}",
65+
Arguments = $"{conversionOptions.CommandLineArguments} --dest-dir={Path.GetDirectoryName(outputFile)} {inputFile} {Path.GetFileName(outputFile)}",
6566
CreateNoWindow = true,
6667
RedirectStandardOutput = true,
6768
RedirectStandardError = true
@@ -91,13 +92,5 @@ void AddLog(string? log)
9192
return (p.ExitCode == 0, logs);
9293
}
9394

94-
internal static string ToCommandLineArguments(IEnumerable<KeyValuePair<string, string?>> options) =>
95-
string.Join(' ', options.Where(kvp => kvp.Value != null).Select(kvp => $"--{ToKebabCase(kvp.Key.Replace("ConversionOptions:", ""))}={ValueToString(kvp.Value!)}"));
96-
97-
private static string ValueToString(string value) => bool.TryParse(value, out var boolValue) ? (boolValue ? "1" : "0") : value;
98-
99-
private static string ToKebabCase(string value) =>
100-
Regex.Replace(value, "(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z0-9])", "-$1", RegexOptions.Compiled).Trim().ToLower();
101-
10295
private static string FormatToMb(long bytesLength) => (bytesLength / 1024.0 / 1024.0).ToString("0.00 MB");
10396
}

src/Pdf2Html/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
using Pdf2Html.Settings;
2+
3+
using System.Diagnostics;
4+
using System.Reflection;
5+
16
var builder = WebApplication.CreateBuilder(args);
27
builder.Logging.ClearProviders();
38
builder.Logging.AddConsole();
49

510
// Add services to the container.
611
builder.Services.AddControllers();
12+
builder.Services.AddSingleton<ConversionOptions>();
713

814
var app = builder.Build();
15+
var versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
16+
app.Logger.LogInformation($"Starting {versionInfo.ProductName} {versionInfo.ProductVersion}");
17+
app.Logger.LogInformation($"Using pdf2htmlEX command line arguments: {app.Services.GetService<ConversionOptions>()!.CommandLineArguments}");
18+
919
app.MapControllers();
1020
app.Run();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace Pdf2Html.Settings;
4+
5+
public class ConversionOptions(IConfiguration configuration)
6+
{
7+
public string CommandLineArguments { get; } = ToCommandLineArguments(configuration.GetSection("ConversionOptions").AsEnumerable());
8+
9+
internal static string ToCommandLineArguments(IEnumerable<KeyValuePair<string, string?>> options) =>
10+
string.Join(' ', options.Where(kvp => kvp.Value != null).Select(kvp => $"--{ToKebabCase(kvp.Key.Replace("ConversionOptions:", ""))}={ValueToString(kvp.Value!)}"));
11+
12+
private static string ValueToString(string value) => bool.TryParse(value, out var boolValue) ? (boolValue ? "1" : "0") : value;
13+
14+
private static string ToKebabCase(string value) =>
15+
Regex.Replace(value, "(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z0-9])", "-$1", RegexOptions.Compiled).Trim().ToLower();
16+
}

tests/Unit.Tests/RootControllerTest.cs renamed to tests/Unit.Tests/ConversionOptionsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using Pdf2Html.Controllers;
1+
using Pdf2Html.Settings;
22

33
namespace Unit.Tests;
44

5-
public class RootControllerTest
5+
public class ConversionOptionsTests
66
{
77
[Test]
88
public void TestToCommandLineArguments()
@@ -14,7 +14,7 @@ public void TestToCommandLineArguments()
1414
{ "ConversionOptions:Hello", "World!" },
1515
{ "ConversionOptions:FizzBuzz", "5" },
1616
};
17-
var result = RootController.ToCommandLineArguments(input);
17+
var result = ConversionOptions.ToCommandLineArguments(input);
1818
Assert.That(result, Is.EqualTo("--foo-bar=1 --baz-blort=0 --hello=World! --fizz-buzz=5"));
1919
}
2020
}

0 commit comments

Comments
 (0)