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:
parent
4a23f4ffb8
commit
2811a2ea29
13 changed files with 94 additions and 46 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue