Skip to content

Commit e255bff

Browse files
authored
Merge pull request #24 from kevbite/issue/23
Fixing issue with file paths including spaces
2 parents a2995e9 + 0bf1f44 commit e255bff

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

src/Kevsoft.PDFtk/PDFtk.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,16 @@ public async Task<IPDFtkResult<byte[]>> GetPagesAsync(string inputFile, params i
5555

5656
var pageRanges = GetPageRangeArgs(pages);
5757

58-
var executeProcessResult = await _pdftkProcess.ExecuteAsync(inputFile, "cat",
59-
string.Join(" ", pageRanges),
60-
"output", outputFile.TempFileName);
58+
var args = new List<string>(4 + pages.Length);
59+
args.Add(inputFile);
60+
args.Add("cat");
61+
62+
args.AddRange(pageRanges);
63+
64+
args.Add("output");
65+
args.Add(outputFile.TempFileName);
66+
67+
var executeProcessResult = await _pdftkProcess.ExecuteAsync(args.ToArray());
6168

6269
return await ResolveSingleFileExecutionResultAsync(executeProcessResult, outputFile);
6370
}
@@ -117,10 +124,9 @@ public async Task<IPDFtkResult<byte[]>> ConcatAsync(IEnumerable<string> filePath
117124
{
118125
using var outputFile = TempPDFtkFile.Create();
119126

120-
var inputFileNames = string.Join(" ", filePaths);
121-
122127
var executeProcessResult =
123-
await _pdftkProcess.ExecuteAsync(inputFileNames, "cat", "output", outputFile.TempFileName);
128+
await _pdftkProcess.ExecuteAsync(filePaths.Concat(new[] { "cat", "output", outputFile.TempFileName })
129+
.ToArray());
124130

125131
return await ResolveSingleFileExecutionResultAsync(executeProcessResult, outputFile);
126132
}
@@ -142,8 +148,7 @@ public async Task<IPDFtkResult<IReadOnlyCollection<KeyValuePair<string, byte[]>>
142148
public async Task<IPDFtkResult<byte[]>> StampAsync(string pdfFilePath, string stampPdfFilePath)
143149
{
144150
using var outputFile = TempPDFtkFile.Create();
145-
146-
151+
147152
var executeProcessResult = await _pdftkProcess.ExecuteAsync(pdfFilePath,
148153
"multistamp", stampPdfFilePath,
149154
"output", outputFile.TempFileName);

src/Kevsoft.PDFtk/PDFtkProcess.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using System.IO;
3+
using System.Linq;
34
using System.Text;
45
using System.Threading.Tasks;
56

@@ -14,12 +15,13 @@ public PDFtkProcess(PDFtkOptions options)
1415

1516
internal async Task<ExecutionResult> ExecuteAsync(params string[] args)
1617
{
18+
var arguments = BuildArguments(args);
1719
var process = new Process
1820
{
1921
StartInfo = new ProcessStartInfo
2022
{
2123
FileName = _options.PDFtkPath,
22-
Arguments = string.Join(" ", args),
24+
Arguments = arguments,
2325
UseShellExecute = false,
2426
RedirectStandardOutput = true,
2527
RedirectStandardError = true,
@@ -41,6 +43,15 @@ internal async Task<ExecutionResult> ExecuteAsync(params string[] args)
4143
return new ExecutionResult(process.ExitCode, standardOutput, standardError);
4244
}
4345

46+
private static string BuildArguments(string[] args)
47+
{
48+
return string.Join(" ", args.Select(x => x switch
49+
{
50+
{ } value when value.Contains(" ") => $"\"{x}\"",
51+
_ => x
52+
}));
53+
}
54+
4455
private static async Task<string> GetAllStreamAsync(StreamReader stream)
4556
{
4657
var stringBuilder = new StringBuilder();

test/Kevsoft.PDFtk.Tests/NumberOfPagesTests.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,52 @@ public async Task ShouldReturnSuccessAndCorrectTotalNumberOfPages_ForInputFileAs
1616
var pdfFileBytes = await File.ReadAllBytesAsync(TestFiles.TestFile1Path);
1717

1818
var result = await _pdFtk.GetNumberOfPagesAsync(pdfFileBytes);
19-
19+
2020
result.Success.Should().BeTrue();
2121
result.Result.Should().Be(10);
2222
}
23-
23+
2424
[Fact]
2525
public async Task ShouldReturnSuccessAndCorrectTotalNumberOfPages_ForInputFileFilePath()
2626
{
2727
var result = await _pdFtk.GetNumberOfPagesAsync(TestFiles.TestFile1Path);
28-
28+
2929
result.Success.Should().BeTrue();
3030
result.Result.Should().Be(10);
3131
}
32-
32+
33+
[Fact]
34+
public async Task ShouldReturnSuccessAndCorrectTotalNumberOfPages_ForInputFileFilePathWithSpaces()
35+
{
36+
var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
37+
try
38+
{
39+
Directory.CreateDirectory(path);
40+
41+
var pdfFilePath = Path.Combine(path, "file with spaces.pdf");
42+
File.Copy(TestFiles.TestFile1Path, pdfFilePath);
43+
44+
var result = await _pdFtk.GetNumberOfPagesAsync(pdfFilePath);
45+
46+
result.Success.Should().BeTrue();
47+
result.Result.Should().Be(10);
48+
}
49+
finally
50+
{
51+
Directory.Delete(path, true);
52+
}
53+
}
54+
3355
[Fact]
3456
public async Task ShouldReturnSuccessAndCorrectTotalNumberOfPages_ForStream()
3557
{
3658
await using var stream = File.OpenRead(TestFiles.TestFile1Path);
3759
var result = await _pdFtk.GetNumberOfPagesAsync(stream);
38-
60+
3961
result.Success.Should().BeTrue();
4062
result.Result.Should().Be(10);
4163
}
42-
64+
4365
[Fact]
4466
public async Task ShouldReturnUnsuccessfulAndNullResult()
4567
{
@@ -49,4 +71,4 @@ public async Task ShouldReturnUnsuccessfulAndNullResult()
4971
result.Result.Should().BeNull();
5072
}
5173
}
52-
}
74+
}

0 commit comments

Comments
 (0)