mirror of
https://github.com/cyanfish/naps2.git
synced 2024-10-03 19:07:17 +03:00
Merge 2b08aae77a
into 70eb67695c
This commit is contained in:
commit
f5b44e2e9c
@ -405,6 +405,150 @@ public class CommandLineIntegrationTests : ContextualTests
|
||||
AssertRecoveryCleanedUp();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistingFile_NoOverwrite_SplitWithPlaceholder()
|
||||
{
|
||||
var path = $"{FolderPath}/test$(n).pdf";
|
||||
await _automationHelper.RunCommand(
|
||||
new AutomatedScanningOptions
|
||||
{
|
||||
OutputPath = path,
|
||||
ProfileName = string.Empty,
|
||||
Split = true,
|
||||
Verbose = true
|
||||
},
|
||||
new[] { Image1, Image2, Image3 });
|
||||
await _automationHelper.WithContainer(container =>
|
||||
{
|
||||
var profileManager = container.Resolve<IProfileManager>();
|
||||
profileManager.Profiles.Remove(profileManager.Profiles.Where(p => p.IsDefault).First());
|
||||
}).RunCommand(
|
||||
new AutomatedScanningOptions
|
||||
{
|
||||
OutputPath = path,
|
||||
ProfileName = string.Empty,
|
||||
Split = true,
|
||||
Verbose = true
|
||||
},
|
||||
new[] { Image1, Image2, Image3 });
|
||||
PdfAsserts.AssertImages($"{FolderPath}/test1.pdf", Image1);
|
||||
PdfAsserts.AssertImages($"{FolderPath}/test2.pdf", Image2);
|
||||
PdfAsserts.AssertImages($"{FolderPath}/test3.pdf", Image3);
|
||||
Assert.True(File.Exists($"{FolderPath}/test4.pdf"));
|
||||
Assert.True(File.Exists($"{FolderPath}/test5.pdf"));
|
||||
Assert.True(File.Exists($"{FolderPath}/test6.pdf"));
|
||||
AssertRecoveryCleanedUp();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistingFile_ForceOverwrite_SplitWithPlaceholder()
|
||||
{
|
||||
var path = $"{FolderPath}/test$(n).pdf";
|
||||
await _automationHelper.RunCommand(
|
||||
new AutomatedScanningOptions
|
||||
{
|
||||
OutputPath = path,
|
||||
ForceOverwrite = true,
|
||||
ProfileName = string.Empty,
|
||||
Split = true,
|
||||
Verbose = true
|
||||
},
|
||||
new[] { Image1, Image2, Image3 });
|
||||
await _automationHelper.WithContainer(container =>
|
||||
{
|
||||
var profileManager = container.Resolve<IProfileManager>();
|
||||
profileManager.Profiles.Remove(profileManager.Profiles.Where(p => p.IsDefault).First());
|
||||
}).RunCommand(
|
||||
new AutomatedScanningOptions
|
||||
{
|
||||
OutputPath = path,
|
||||
ForceOverwrite = true,
|
||||
ProfileName = string.Empty,
|
||||
Split = true,
|
||||
Verbose = true
|
||||
},
|
||||
new[] { Image1, Image2, Image3 });
|
||||
PdfAsserts.AssertImages($"{FolderPath}/test1.pdf", Image1);
|
||||
PdfAsserts.AssertImages($"{FolderPath}/test2.pdf", Image2);
|
||||
PdfAsserts.AssertImages($"{FolderPath}/test3.pdf", Image3);
|
||||
Assert.False(File.Exists($"{FolderPath}/test4.pdf"));
|
||||
Assert.False(File.Exists($"{FolderPath}/test5.pdf"));
|
||||
Assert.False(File.Exists($"{FolderPath}/test6.pdf"));
|
||||
AssertRecoveryCleanedUp();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistingFile_NoOverwrite_SplitWithNoPlaceholder()
|
||||
{
|
||||
var path = $"{FolderPath}/test.pdf";
|
||||
await _automationHelper.RunCommand(
|
||||
new AutomatedScanningOptions
|
||||
{
|
||||
OutputPath = path,
|
||||
ProfileName = string.Empty,
|
||||
Split = true,
|
||||
Verbose = true
|
||||
},
|
||||
new[] { Image1, Image2, Image3 });
|
||||
await _automationHelper.WithContainer(container =>
|
||||
{
|
||||
var profileManager = container.Resolve<IProfileManager>();
|
||||
profileManager.Profiles.Remove(profileManager.Profiles.Where(p => p.IsDefault).First());
|
||||
}).RunCommand(
|
||||
new AutomatedScanningOptions
|
||||
{
|
||||
OutputPath = path,
|
||||
ProfileName = string.Empty,
|
||||
Split = true,
|
||||
Verbose = true
|
||||
},
|
||||
new[] { Image1, Image2, Image3 });
|
||||
PdfAsserts.AssertPageCount(1, $"{FolderPath}/test.1.pdf");
|
||||
PdfAsserts.AssertPageCount(1, $"{FolderPath}/test.2.pdf");
|
||||
PdfAsserts.AssertPageCount(1, $"{FolderPath}/test.3.pdf");
|
||||
Assert.True(File.Exists($"{FolderPath}/test.4.pdf"));
|
||||
Assert.True(File.Exists($"{FolderPath}/test.5.pdf"));
|
||||
Assert.True(File.Exists($"{FolderPath}/test.6.pdf"));
|
||||
AssertRecoveryCleanedUp();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistingFile_ForceOverwrite_SplitWithNoPlaceholder()
|
||||
{
|
||||
var path = $"{FolderPath}/test.pdf";
|
||||
await _automationHelper.RunCommand(
|
||||
new AutomatedScanningOptions
|
||||
{
|
||||
OutputPath = path,
|
||||
ForceOverwrite = true,
|
||||
ProfileName = string.Empty,
|
||||
Split = true,
|
||||
Verbose = true
|
||||
},
|
||||
new[] { Image1, Image2, Image3 });
|
||||
await _automationHelper.WithContainer(container =>
|
||||
{
|
||||
var profileManager = container.Resolve<IProfileManager>();
|
||||
profileManager.Profiles.Remove(profileManager.Profiles.Where(p => p.IsDefault).First());
|
||||
}).RunCommand(
|
||||
new AutomatedScanningOptions
|
||||
{
|
||||
OutputPath = path,
|
||||
ForceOverwrite = true,
|
||||
ProfileName = string.Empty,
|
||||
Split = true,
|
||||
Verbose = true
|
||||
},
|
||||
new[] { Image1, Image2, Image3 });
|
||||
PdfAsserts.AssertPageCount(1, $"{FolderPath}/test.1.pdf");
|
||||
PdfAsserts.AssertPageCount(1, $"{FolderPath}/test.2.pdf");
|
||||
PdfAsserts.AssertPageCount(1, $"{FolderPath}/test.3.pdf");
|
||||
Assert.False(File.Exists($"{FolderPath}/test.4.pdf"));
|
||||
Assert.False(File.Exists($"{FolderPath}/test.5.pdf"));
|
||||
Assert.False(File.Exists($"{FolderPath}/test.6.pdf"));
|
||||
AssertRecoveryCleanedUp();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistingPdf_ForceOverwrite_InUse()
|
||||
{
|
||||
|
@ -697,7 +697,8 @@ internal class AutomatedScanning
|
||||
}
|
||||
};
|
||||
int digits = (int) Math.Floor(Math.Log10(_scanList.Count)) + 1;
|
||||
string actualPath = _placeholders.Substitute(path, true, scanIndex++, _scanList.Count > 1 ? digits : 0);
|
||||
string actualPath = _placeholders.Substitute(path, !_options.ForceOverwrite,
|
||||
scanIndex++, _scanList.Count > 1 ? digits : 0, !_options.ForceOverwrite);
|
||||
op.Start(actualPath, _placeholders, fileContents, _config.Get(c => c.PdfSettings), _ocrParams);
|
||||
if (!await op.Success)
|
||||
{
|
||||
|
@ -74,8 +74,8 @@ internal class SavePdfOperation : OperationBase
|
||||
int i = 0;
|
||||
foreach (var imagesForFile in imagesByFile)
|
||||
{
|
||||
var currentFileName = placeholders.Substitute(fileName, true, i, singleFile ? 0 : digits);
|
||||
// TODO: Overwrite prompt non-single file?
|
||||
var currentFileName = placeholders.Substitute(fileName, true, i, singleFile ? 0 : digits);
|
||||
// TODO: Overwrite prompt non-single file?
|
||||
Status.StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(currentFileName));
|
||||
InvokeStatusChanged();
|
||||
if (singleFile && IsFileInUse(currentFileName, out var ex))
|
||||
|
@ -46,21 +46,22 @@ internal abstract class Placeholders
|
||||
/// <param name="incrementIfExists">Whether to use an auto-incrementing file number to make the file name unique.</param>
|
||||
/// <param name="numberSkip">The file number will be at least one bigger than this value.</param>
|
||||
/// <param name="autoNumberDigits">The minimum number of digits in the file number. Only has an effect if the path does not contain a numeric placeholder like $(n) or $(nnn).</param>
|
||||
/// <param name="incrementPlaceholderIfExists">Whether to increment the placeholder number to make the file name unique.</param>
|
||||
/// <returns>The file path with substitutions.</returns>
|
||||
[return: NotNullIfNotNull("filePath")]
|
||||
public abstract string? Substitute(string? filePath, bool incrementIfExists = true, int numberSkip = 0,
|
||||
int autoNumberDigits = 0);
|
||||
int autoNumberDigits = 0, bool incrementPlaceholderIfExists = true);
|
||||
|
||||
public class StubPlaceholders : Placeholders
|
||||
{
|
||||
public override string? Substitute(string? filePath, bool incrementIfExists = true, int numberSkip = 0,
|
||||
int autoNumberDigits = 0) => filePath;
|
||||
int autoNumberDigits = 0, bool incrementPlaceholderIfExists = true) => filePath;
|
||||
}
|
||||
|
||||
public class EnvironmentPlaceholders : Placeholders
|
||||
{
|
||||
public override string? Substitute(string? filePath, bool incrementIfExists = true, int numberSkip = 0,
|
||||
int autoNumberDigits = 0)
|
||||
int autoNumberDigits = 0, bool incrementPlaceholderIfExists = true)
|
||||
{
|
||||
if (filePath == null) return null;
|
||||
return Environment.ExpandEnvironmentVariables(filePath);
|
||||
@ -99,7 +100,7 @@ internal abstract class Placeholders
|
||||
|
||||
[return: NotNullIfNotNull("filePath")]
|
||||
public override string? Substitute(string? filePath, bool incrementIfExists = true, int numberSkip = 0,
|
||||
int autoNumberDigits = 0)
|
||||
int autoNumberDigits = 0, bool incrementPlaceholderIfExists = true)
|
||||
{
|
||||
if (filePath == null)
|
||||
{
|
||||
@ -116,7 +117,7 @@ internal abstract class Placeholders
|
||||
if (match.Success)
|
||||
{
|
||||
result = NumberPlaceholderPattern.Replace(result, "");
|
||||
result = SubstituteNumber(result, match.Index, match.Length - 3, numberSkip, true);
|
||||
result = SubstituteNumber(result, match.Index, match.Length - 3, numberSkip, incrementPlaceholderIfExists);
|
||||
}
|
||||
else if (autoNumberDigits > 0)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user