|
1 | 1 | using System.Diagnostics; |
2 | 2 | using System.Net.Mime; |
3 | 3 | using System.Reflection; |
4 | | -using System.Text.RegularExpressions; |
| 4 | + |
5 | 5 | using Microsoft.AspNetCore.Mvc; |
6 | 6 |
|
| 7 | +using Pdf2Html.Settings; |
| 8 | + |
7 | 9 | namespace Pdf2Html.Controllers; |
8 | 10 |
|
9 | 11 | [ApiController] |
10 | 12 | [Route("/")] |
11 | | -public class RootController(ILogger<RootController> logger, IConfiguration configuration) : ControllerBase |
| 13 | +public class RootController(ILogger<RootController> logger, ConversionOptions conversionOptions) : ControllerBase |
12 | 14 | { |
13 | 15 | [HttpGet] |
14 | 16 | public ActionResult Get() |
@@ -57,11 +59,10 @@ public async Task<ActionResult> Post() |
57 | 59 | private async Task<(bool Success, ICollection<string> logs)> ConvertAsync(string inputFile, string outputFile) |
58 | 60 | { |
59 | 61 | using var p = new Process(); |
60 | | - string options = ToCommandLineArguments(configuration.GetSection("ConversionOptions").AsEnumerable()); |
61 | 62 | p.StartInfo = new ProcessStartInfo |
62 | 63 | { |
63 | 64 | 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)}", |
65 | 66 | CreateNoWindow = true, |
66 | 67 | RedirectStandardOutput = true, |
67 | 68 | RedirectStandardError = true |
@@ -91,13 +92,5 @@ void AddLog(string? log) |
91 | 92 | return (p.ExitCode == 0, logs); |
92 | 93 | } |
93 | 94 |
|
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 | | - |
102 | 95 | private static string FormatToMb(long bytesLength) => (bytesLength / 1024.0 / 1024.0).ToString("0.00 MB"); |
103 | 96 | } |
0 commit comments