Skip to content

Commit

Permalink
Merge branch 'wfnet9' of gitlab.com:cyanfish/NAPS2 into wfnet9
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Aug 28, 2024
2 parents 61d3be9 + adf6f46 commit 5f452d2
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 27 deletions.
32 changes: 21 additions & 11 deletions NAPS2.Lib.Tests/Config/FileConfigScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public class FileConfigScopeTests : ContextualTests
public void FileScope()
{
var configPath = Path.Combine(FolderPath, "config.xml");
var scope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite);

var scope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite,
TimeSpan.FromMilliseconds(100));

// Nothing should be created yet
Assert.False(File.Exists(configPath));

Expand All @@ -28,7 +30,7 @@ public void FileScope()

var doc = XDocument.Load(configPath);
Assert.Equal("UserConfig", doc.Root?.Name);

var docValue = doc.Descendants("Culture").Single().Value;
Assert.Equal("fr", docValue);

Expand Down Expand Up @@ -88,7 +90,8 @@ public void ReadWithBadXml()
{
var configPath = Path.Combine(FolderPath, "config.xml");
File.WriteAllText(configPath, @"blah");
var scope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite);
var scope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite);

Assert.False(scope.Has(c => c.Culture));
}
Expand All @@ -98,7 +101,8 @@ public void ReadWithBadConfig()
{
var configPath = Path.Combine(FolderPath, "config.xml");
File.WriteAllText(configPath, @"<?xml version=""1.0"" encoding=""utf-8""?><Blah><Culture>fr</Culture></Blah>");
var scope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite);
var scope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite);

Assert.False(scope.Has(c => c.Culture));
}
Expand All @@ -107,7 +111,8 @@ public void ReadWithBadConfig()
public void ReadWithMissingFile()
{
var configPath = Path.Combine(FolderPath, "config.xml");
var scope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite);
var scope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadWrite);

Assert.False(scope.Has(c => c.Culture));
}
Expand All @@ -117,8 +122,10 @@ public void ReadAppSettings()
{
var configPath = Path.Combine(FolderPath, "appsettings.xml");
File.WriteAllText(configPath, ConfigData.AppSettings);
var defaultsScope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var lockedScope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var defaultsScope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var lockedScope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);

Assert.False(lockedScope.Has(c => c.AlwaysRememberDevice));
Assert.True(lockedScope.TryGet(c => c.PdfSettings.Compat, out var pdfCompat));
Expand All @@ -138,8 +145,10 @@ public void ReadNewAppSettings()
{
var configPath = Path.Combine(FolderPath, "appsettings.xml");
File.WriteAllText(configPath, ConfigData.NewAppSettings);
var defaultsScope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var lockedScope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var defaultsScope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.DefaultOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);
var lockedScope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.LockedOnly, ConfigRootName.AppConfig), ConfigScopeMode.ReadOnly);

Assert.False(lockedScope.Has(c => c.SingleInstance));
Assert.True(lockedScope.TryGet(c => c.DeleteAfterSaving, out var deleteAfterSaving));
Expand All @@ -155,7 +164,8 @@ public void ReadWithOldConfig()
{
var configPath = Path.Combine(FolderPath, "config.xml");
File.WriteAllText(configPath, ConfigData.OldUserConfig);
var scope = new FileConfigScope<CommonConfig>(configPath, new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite);
var scope = new FileConfigScope<CommonConfig>(configPath,
new ConfigSerializer(ConfigReadMode.All, ConfigRootName.UserConfig), ConfigScopeMode.ReadWrite);

Assert.False(scope.Has(c => c.LockSystemProfiles));
Assert.True(scope.TryGet(c => c.OcrMode, out var ocrMode));
Expand Down
47 changes: 47 additions & 0 deletions NAPS2.Lib.Tests/Scan/AutoSaverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,53 @@ public async Task PdfSplitPatchT()
PdfAsserts.AssertImages(Path.Combine(FolderPath, "test2.pdf"), ImageResources.dog_h_n300);
}

[Fact]
public async Task PromptForFilePath()
{
var settings = new AutoSaveSettings
{
FilePath = Path.Combine(FolderPath, "test_a_$(n).pdf"),
PromptForFilePath = true
};
_dialogHelper.PromptToSavePdfOrImage(Arg.Any<string>(), out Arg.Any<string>()).Returns(x =>
{
x[1] = Path.Combine(FolderPath, "test_b_$(n).pdf");
return true;
});

var scanned = CreateScannedImages(ImageResources.dog);
var output = await _autoSaver.Save(settings, scanned.ToAsyncEnumerable()).ToListAsync();

Assert.Single(output);
Assert.False(IsDisposed(output[0]));
Assert.True(IsDisposed(scanned[0]));
Assert.Single(Folder.GetFiles());
PdfAsserts.AssertImages(Path.Combine(FolderPath, "test_b_1.pdf"), ImageResources.dog);
}

[Fact]
public async Task CancelPromptForFilePath()
{
var settings = new AutoSaveSettings
{
FilePath = Path.Combine(FolderPath, "test$(n).pdf"),
PromptForFilePath = true
};
_dialogHelper.PromptToSavePdfOrImage(Arg.Any<string>(), out Arg.Any<string>()).Returns(x =>
{
x[1] = Path.Combine(FolderPath, "test$(n).pdf");
return false;
});

var scanned = CreateScannedImages(ImageResources.dog);
var output = await _autoSaver.Save(settings, scanned.ToAsyncEnumerable()).ToListAsync();

Assert.Single(output);
Assert.False(IsDisposed(output[0]));
Assert.True(IsDisposed(scanned[0]));
Assert.Empty(Folder.GetFiles());
}

// TODO: Finish out tests

//
Expand Down
7 changes: 4 additions & 3 deletions NAPS2.Lib/Config/Model/FileConfigScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ namespace NAPS2.Config.Model;
public class FileConfigScope<TConfig> : ConfigScope<TConfig>
{
private static readonly TimeSpan READ_INTERVAL = TimeSpan.FromMilliseconds(5000);

private readonly string _filePath;
private readonly ISerializer<ConfigStorage<TConfig>> _serializer;
private ConfigStorage<TConfig> _cache = new();
private ConfigStorage<TConfig> _changes = new();
private readonly TimedThrottle _readHandshakeThrottle;

public FileConfigScope(string filePath, ISerializer<ConfigStorage<TConfig>> serializer, ConfigScopeMode mode) : base(mode)
public FileConfigScope(string filePath, ISerializer<ConfigStorage<TConfig>> serializer, ConfigScopeMode mode,
TimeSpan? readInterval = null) : base(mode)
{
_filePath = filePath;
_serializer = serializer;
_readHandshakeThrottle = new TimedThrottle(ReadHandshake, READ_INTERVAL);
_readHandshakeThrottle = new TimedThrottle(ReadHandshake, readInterval ?? READ_INTERVAL);
}

protected override bool TryGetInternal(ConfigLookup lookup, out object? value)
Expand Down
14 changes: 8 additions & 6 deletions NAPS2.Lib/ImportExport/AutoSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ private async Task<bool> InternalSave(AutoSaveSettings settings, List<ProcessedI
string subPath = placeholders.Substitute(settings.FilePath, true, i);
if (settings.PromptForFilePath)
{
Invoker.Current.Invoke(() =>
string? newPath = null!;
if (Invoker.Current.InvokeGet(() => _dialogHelper.PromptToSavePdfOrImage(subPath, out newPath)))
{
if (_dialogHelper.PromptToSavePdfOrImage(subPath, out string? newPath))
{
subPath = placeholders.Substitute(newPath!, true, i);
}
});
subPath = placeholders.Substitute(newPath!, true, i);
}
else
{
return (false, null);
}
}
// TODO: This placeholder handling is complex and wrong in some cases (e.g. FilePerScan with ext = "jpg")
// TODO: Maybe have initial placeholders that replace date, then rely on the ops to increment the file num
Expand Down
29 changes: 22 additions & 7 deletions NAPS2.Tools/Project/Releasing/UploadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class UploadCommand : ICommand<UploadOptions>

public int Run(UploadOptions opts)
{
string version = opts.Version ?? ProjectHelper.GetCurrentVersion();
string version = opts.Version ?? ProjectHelper.GetCurrentVersionName();
if (opts.Target != "sdk")
{
// Validate all package files
Expand Down Expand Up @@ -58,7 +58,7 @@ public int Run(UploadOptions opts)
if (opts.Target is "all" or "apt")
{
Output.Info("Updating Apt metadata on downloads.naps2.com");
UpdateAptMetadata();
UpdateAptMetadata(version);
didSomething = true;
}
if (opts.Target is "sdk")
Expand All @@ -83,7 +83,8 @@ private async Task UploadToNuget()
var repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
var resource = await repository.GetResourceAsync<PackageUpdateResource>();
var v = ProjectHelper.GetSdkVersion();
var packagePaths = ProjectHelper.GetSdkProjects().Select(x => $"{GetProjectFolder(x)}/bin/Release/{x}.{v}.nupkg").ToList();
var packagePaths = ProjectHelper.GetSdkProjects()
.Select(x => $"{GetProjectFolder(x)}/bin/Release/{x}.{v}.nupkg").ToList();
var key = await File.ReadAllTextAsync(Path.Combine(Paths.Naps2UserFolder, "nuget"));
await resource.Push(
packagePaths,
Expand Down Expand Up @@ -179,6 +180,11 @@ private async Task UploadToSourceForge(string version)
client.UploadFile(stream, $"/home/frs/project/naps2/{version}/{Path.GetFileName(path)}");
}
}
if (version.Contains('b'))
{
Output.Verbose("Skipping default downloads for beta");
return;
}
Output.Verbose($"Setting default downloads");
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
Expand Down Expand Up @@ -232,8 +238,14 @@ private void UploadToStaticSite(string version, string? packageType)
Output.Info($"Uploaded files.");
}

private void UpdateAptMetadata()
private void UpdateAptMetadata(string version)
{
if (version.Contains('b'))
{
Output.Verbose("Skipping APT metadata for beta");
return;
}

var aptTemp = Path.Combine(Paths.SetupObj, "apt");
if (Directory.Exists(aptTemp))
{
Expand All @@ -243,9 +255,12 @@ private void UpdateAptMetadata()

Cli.Run("ssh", "user@downloads.naps2.com \"mkdir -p /home/user/apt-temp-packages/\"");
Cli.Run("ssh", "user@downloads.naps2.com \"mkdir -p /home/user/apt-temp-release/\"");
Cli.Run("ssh", "user@downloads.naps2.com \"cd /var/www/html/ ; apt-ftparchive packages . > /home/user/apt-temp-packages/Packages\" ; apt-ftparchive release /home/user/apt-temp-packages/ > /home/user/apt-temp-release/Release\"");
Cli.Run("scp", $"user@downloads.naps2.com:/home/user/apt-temp-packages/Packages {Path.Combine(aptTemp, "Packages")}");
Cli.Run("scp", $"user@downloads.naps2.com:/home/user/apt-temp-release/Release {Path.Combine(aptTemp, "Release")}");
Cli.Run("ssh",
"user@downloads.naps2.com \"cd /var/www/html/ ; apt-ftparchive packages . > /home/user/apt-temp-packages/Packages\" ; apt-ftparchive release /home/user/apt-temp-packages/ > /home/user/apt-temp-release/Release\"");
Cli.Run("scp",
$"user@downloads.naps2.com:/home/user/apt-temp-packages/Packages {Path.Combine(aptTemp, "Packages")}");
Cli.Run("scp",
$"user@downloads.naps2.com:/home/user/apt-temp-release/Release {Path.Combine(aptTemp, "Release")}");
Cli.Run("gpg", $"--output {Path.Combine(aptTemp, "Release.gpg")} --sign {Path.Combine(aptTemp, "Release")}");
Cli.Run("gpg", $"--output {Path.Combine(aptTemp, "InRelease")} --clearsign {Path.Combine(aptTemp, "Release")}");
Cli.Run("scp", $"{Path.Combine(aptTemp, "Packages")} user@downloads.naps2.com:/var/www/html/Packages");
Expand Down

0 comments on commit 5f452d2

Please sign in to comment.