using HKCU for printer output path, starting file watcher when receiving first job and restart file watcher, if output path in registry has changed

This commit is contained in:
Marco Batzinger 2021-04-14 14:52:51 +02:00
parent 4a23f4ffb8
commit 2811a2ea29
13 changed files with 94 additions and 46 deletions

View file

@ -1,7 +1,10 @@
namespace AmagnoVirtualPrinter.Agent.Core.Interfaces
using JetBrains.Annotations;
namespace AmagnoVirtualPrinter.Agent.Core.Interfaces
{
public interface IDirectoryHelper
{
string GetOutputDirectory(IExConfig config);
[NotNull]
string GetOutputDirectory([NotNull] IUserConfig config);
}
}

View file

@ -20,12 +20,6 @@ namespace AmagnoVirtualPrinter.Agent.Core.Interfaces
[NotNull]
Tuple<string, string> ResolvedPostconverter { get; }
/// <summary>
/// The full path of the output directory.
/// </summary>
[NotNull]
string ResolvedOutputDirectory { get; }
/// <summary>
/// An intermediate format which is read by the printer or similar.
/// </summary>

View file

@ -43,5 +43,12 @@ namespace AmagnoVirtualPrinter.Agent.Core.Interfaces
/// <param name="iniPath">The path to the ini file</param>
/// <returns><see cref="JobStatus"/></returns>
JobStatus ReadJobStatus(string iniPath);
/// <summary>
/// Gets the <see cref="SessionInfo" from ini file./>
/// </summary>
/// <param name="iniFile">The path to the ini file</param>
/// <returns><see cref="SessionInfo"/></returns>
SessionInfo GetSessionInfo(string iniFile);
}
}

View file

@ -22,5 +22,11 @@ namespace AmagnoVirtualPrinter.Agent.Core.Interfaces
/// <remarks>Intital value is PDF</remarks>
[NotNull]
string Format { get; }
/// <summary>
/// The full path of the output directory.
/// </summary>
[NotNull]
string ResolvedOutputDirectory { get; }
}
}

View file

@ -13,8 +13,6 @@ namespace AmagnoVirtualPrinter.Agent.Core.Model
public string Preconverter { get; set; }
public string OutputDirectory { get; set; }
public string FileNameMask { get; set; }
public short PrinterPort { get; set; }
@ -29,11 +27,6 @@ namespace AmagnoVirtualPrinter.Agent.Core.Model
get { return GetResolvedArgs(Postconverter); }
}
public string ResolvedOutputDirectory
{
get { return string.IsNullOrWhiteSpace(OutputDirectory) ? "" : Path.GetFullPath(OutputDirectory); }
}
public IntermediateFormat IntermediateFormat { get; set; }
[NotNull]

View file

@ -1,4 +1,5 @@
using AmagnoVirtualPrinter.Agent.Core.Interfaces;
using System.IO;
using AmagnoVirtualPrinter.Agent.Core.Interfaces;
namespace AmagnoVirtualPrinter.Agent.Core.Model
{
@ -11,5 +12,12 @@ namespace AmagnoVirtualPrinter.Agent.Core.Model
public double? UserRenderDpi { get; set; }
public string Format { get; set; }
public string OutputDirectory { get; set; }
public string ResolvedOutputDirectory
{
get { return string.IsNullOrWhiteSpace(OutputDirectory) ? "" : Path.GetFullPath(OutputDirectory); }
}
}
}

View file

@ -66,8 +66,7 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
try
{
var now = DateTime.Now;
var config = _registryRepository.GetRegistryConfig();
var root = _directoryHelper.GetOutputDirectory(config);
var jobInfo = GetCurrentPrintJobs(printerName).FirstOrDefault();
if (jobInfo == null)
{
@ -75,6 +74,9 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
}
var session = GetCurrentSessions(jobInfo).FirstOrDefault();
var config = _registryRepository.GetRegistryConfig();
var userConfig = _registryRepository.GetUserRegistryConfig(session.Sid);
var root = _directoryHelper.GetOutputDirectory(userConfig);
var iniName = GenerateFileName(now, jobInfo.JobId, 0, config.FileNameMask, "ini");
var iniPath = Path.Combine(root, iniName);
var extension = GetRawFileExtension(config.IntermediateFormat);

View file

@ -92,7 +92,7 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
}
var targetFile = $"{Path.GetFileNameWithoutExtension(job.RawDataPath)}";
var config = _registryRepository.GetRegistryConfig();
var config = _registryRepository.GetUserRegistryConfig(job.SessionInfo.Sid);
var dir = _directoryHelper.GetOutputDirectory(config);
targetFile = Path.Combine(dir, targetFile);

View file

@ -98,9 +98,11 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
public void Finish(IJob job)
{
var config = _registryRepository.GetRegistryConfig();
WriteJobFinishIni(job.IniDataPath, config);
var userConfig = _registryRepository.GetUserRegistryConfig(job.SessionInfo.Sid);
WriteJobFinishIni(job.IniDataPath, userConfig);
var iniFile = Path.GetFullPath(job.IniDataPath);
var config = _registryRepository.GetRegistryConfig();
var post = config.ResolvedPostconverter;
_shell.Execute(job.JobInfo, job.SessionInfo, post.Item1, $"{post.Item2} \"{iniFile}\"");
@ -122,7 +124,7 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
_shell.WriteIniEntry("Preconverting", "Status", status.ToIni(), job.IniDataPath);
}
private SessionInfo GetSessionInfo(string iniFile)
public SessionInfo GetSessionInfo(string iniFile)
{
var sessionInfo = new SessionInfo
{
@ -152,7 +154,7 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
return jobInfo;
}
private void WriteJobFinishIni(string iniPath, [NotNull]IExConfig config)
private void WriteJobFinishIni(string iniPath, [NotNull]IUserConfig config)
{
const PrintStatus status = PrintStatus.Complete;
const PrintJobStatus spoolerState = PrintJobStatus.Printed;

View file

@ -54,15 +54,29 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
public void Dispose()
{
_watcher.Dispose();
_watcher?.Dispose();
_socket.Stop();
}
public void Init()
{
var config = GetRegistryConfig();
try
{
var config = GetRegistryConfig();
_socket = new TcpListener(IPAddress.Loopback, config.PrinterPort);
_socket.Start();
_socket.BeginAcceptTcpClient(HandleClient, _socket);
var dir = _directoryHelper.GetOutputDirectory(config);
LogDebug($"Waiting on {_socket.LocalEndpoint}...");
}
catch (Exception e)
{
LogError(e, "Error initializing tcp input printer");
}
}
private void StartFileWatcher([NotNull] string dir)
{
_watcher = new FileSystemWatcher(dir, "*.ini")
{
IncludeSubdirectories = false,
@ -70,11 +84,7 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
EnableRaisingEvents = true
};
_watcher.Changed += IniFileChanged;
_socket = new TcpListener(IPAddress.Loopback, config.PrinterPort);
_socket.Start();
_socket.BeginAcceptTcpClient(HandleClient, _socket);
LogDebug($"Waiting on {_socket.LocalEndpoint}...");
LogDebug("Setting file watcher on folder @{dir}", dir);
}
private void HandleClient([NotNull]IAsyncResult ar)
@ -101,6 +111,19 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
socket.BeginAcceptTcpClient(HandleClient, ar.AsyncState);
_jobService.Start(job);
RestartFileWatcherIfNeeded(job.SessionInfo.Sid);
}
private void RestartFileWatcherIfNeeded(string sid)
{
var config = GetUserRegistryConfig(sid);
var dir = _directoryHelper.GetOutputDirectory(config);
if (_watcher == null || _watcher.Path != dir)
{
StartFileWatcher(dir);
}
}
private void IniFileChanged(object sender, [NotNull]FileSystemEventArgs e)
@ -112,7 +135,8 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
}
var rawName = $"{Path.GetFileNameWithoutExtension(ini)}.ps";
var config = GetRegistryConfig();
var sessionInfo = _jobService.GetSessionInfo(ini);
var config = GetUserRegistryConfig(sessionInfo.Sid);
var dir = _directoryHelper.GetOutputDirectory(config);
var rawFile = Path.Combine(dir, rawName);
var status = _jobService.ReadStatus(ini);
@ -195,6 +219,12 @@ namespace AmagnoVirtualPrinter.Agent.Lib.Misc
}
}
[NotNull]
private IUserConfig GetUserRegistryConfig(string sid)
{
return _registryRepository.GetUserRegistryConfig(sid);
}
[NotNull]
private IExConfig GetRegistryConfig()
{

View file

@ -1,13 +1,18 @@
using System.IO;
using System;
using System.IO;
using AmagnoVirtualPrinter.Agent.Core.Interfaces;
using AmagnoVirtualPrinter.Agent.Core;
namespace AmagnoVirtualPrinter.Utils
{
public class DirectoryHelper : IDirectoryHelper
{
public string GetOutputDirectory(IExConfig config)
public string GetOutputDirectory(IUserConfig config)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
if (string.IsNullOrWhiteSpace(config.ResolvedOutputDirectory))
{
var outputDir = Path.Combine(Path.GetTempPath(), "PrinterOutput");

View file

@ -8,8 +8,6 @@ using JetBrains.Annotations;
using Microsoft.Win32;
using AmagnoVirtualPrinter.Agent.Core;
namespace AmagnoVirtualPrinter.Utils
{
public class RegistryRepository : IRegistryRepository
@ -84,7 +82,6 @@ namespace AmagnoVirtualPrinter.Utils
using (var key = driver.OpenSubKey(Keys.CONVERTER_KEY))
{
CheckForNull(key, Keys.CONVERTER_KEY);
registryConfig.OutputDirectory = key.GetValue(KeyNames.OUTPUT_DIR).ToString();
registryConfig.FileNameMask = key.GetValue(KeyNames.FILE_NAME_MASK).ToString();
var portStr = key.GetValue(KeyNames.SERVER_PORT).ToString();
registryConfig.PrinterPort = short.TryParse(portStr, out var portVal) ? portVal : DefaultServerPort;
@ -120,6 +117,7 @@ namespace AmagnoVirtualPrinter.Utils
using (var converter = driver.OpenSubKey(Keys.CONVERTER_KEY))
{
CheckForNull(converter, Keys.CONVERTER_KEY);
userConfig.OutputDirectory = converter.GetValue(KeyNames.OUTPUT_DIR).ToString();
subKey = "Redirect";
using (var redirect = converter.OpenSubKey(subKey))

View file

@ -134,8 +134,8 @@ namespace AmagnoVirtualPrinter.WixSharpInstaller
@"%ProgramFiles%\AmagnoPrinterDriver\",
new DirFiles(feature, _filesDir + @"\*", s => !s.EndsWith(".exe")),
new File(new Id(SetupDriverId), feature, Path.Combine(_filesDir, Utils.Files.SETUP_DRIVER_EXE)),
new File(feature, Path.Combine(_filesDir, Utils.Files.DILIVERY_EXE)),
new File(feature, Path.Combine(_filesDir, Utils.Files.AGENT_PROGRESS_EXE)),
new File(feature, Path.Combine(_filesDir, Files.DILIVERY_EXE)),
new File(feature, Path.Combine(_filesDir, Files.AGENT_PROGRESS_EXE)),
printerServiceFile
)
};
@ -154,7 +154,7 @@ namespace AmagnoVirtualPrinter.WixSharpInstaller
[NotNull]
private static IEnumerable<RegValue> CreateRegValues(Feature feature)
{
var converterKey = $@"{Utils.Keys.PRINTER_DRIVER_KEY32}\{Utils.Keys.CONVERTER_KEY}";
var converterKey = $@"{Keys.PRINTER_DRIVER_KEY32}\{Keys.CONVERTER_KEY}";
var regValues = new List<RegValue>();
regValues.AddRange(CreateLocalMachineValues(feature, converterKey));
@ -166,10 +166,10 @@ namespace AmagnoVirtualPrinter.WixSharpInstaller
[NotNull]
private static IEnumerable<RegValue> CreateLocalMachineValues(Feature feature, string converterKey)
{
var postConverterKey = $@"{Utils.Keys.PRINTER_DRIVER_KEY32}\{Utils.Keys.POSTCONVERTER_KEY}";
var preConverterKey = $@"{Utils.Keys.PRINTER_DRIVER_KEY32}\{Utils.Keys.PRECONVERTER_KEY}";
var converterPdfKey = $@"{Utils.Keys.PRINTER_DRIVER_KEY32}\{Utils.Keys.CONVERTER_PDF_KEY}";
var converterTiffKey = $@"{Utils.Keys.PRINTER_DRIVER_KEY32}\{Utils.Keys.CONVERTER_TIFF_KEY}";
var postConverterKey = $@"{Keys.PRINTER_DRIVER_KEY32}\{Keys.POSTCONVERTER_KEY}";
var preConverterKey = $@"{Keys.PRINTER_DRIVER_KEY32}\{Keys.PRECONVERTER_KEY}";
var converterPdfKey = $@"{Keys.PRINTER_DRIVER_KEY32}\{Keys.CONVERTER_PDF_KEY}";
var converterTiffKey = $@"{Keys.PRINTER_DRIVER_KEY32}\{Keys.CONVERTER_TIFF_KEY}";
var registryHive = RegistryHive.LocalMachine;
var result = new List<RegValue>
@ -180,7 +180,6 @@ namespace AmagnoVirtualPrinter.WixSharpInstaller
new RegValue(feature, registryHive, converterKey, KeyNames.SHOW_PROGRESS, 1) {AttributesDefinition = "Type=integer"},
new RegValue(feature, registryHive, converterKey, KeyNames.PAGES_PER_SHEET, 1),
new RegValue(feature, registryHive, converterKey, KeyNames.FILE_NAME_MASK, "{yyyy}{MM}{DD}{hh}{mm}{ss}{job05}{page03}"),
new RegValue(feature, registryHive, converterKey, KeyNames.OUTPUT_DIR, string.Empty),
new RegValue(feature, registryHive, converterKey, KeyNames.FORMAT, "ps"),
new RegValue(feature, registryHive, converterPdfKey, KeyNames.ENABLED, 1) {AttributesDefinition = "Type=integer"},
new RegValue(feature, registryHive, converterPdfKey, KeyNames.MULTIPAGE, 1) {AttributesDefinition = "Type=integer"},
@ -221,6 +220,7 @@ namespace AmagnoVirtualPrinter.WixSharpInstaller
{
new RegValue(feature, registryHive, converterKey, KeyNames.PRINT_FORMAT, "PDF"),
new RegValue(feature, registryHive, converterKey, KeyNames.RENDER_DPI, 300) {AttributesDefinition = "Type=integer"},
new RegValue(feature, registryHive, converterKey, KeyNames.OUTPUT_DIR, string.Empty),
new RegValue(feature, registryHive, redirectKey, KeyNames.ENABLED, 1) {AttributesDefinition = "Type=integer"},
new RegValue(feature, registryHive, redirectKey, KeyNames.PRINTER, "")
};