diff --git a/WGneu/Documents/Document.cshtml.cs b/WGneu/Documents/Document.cshtml.cs index d31e3b8..efb9f3f 100644 --- a/WGneu/Documents/Document.cshtml.cs +++ b/WGneu/Documents/Document.cshtml.cs @@ -10,7 +10,7 @@ using WGneu.Helpers; namespace WGneu.Documents { public abstract class Document : IDisposable { - private Utils.TemporaryFile? PdfFile = null; + private TempFile? PdfFile = null; public Document(string title) { Title = title; @@ -51,8 +51,8 @@ namespace WGneu.Documents { } public async Task Generate() { - var pdf = new Utils.TemporaryFile("pdf"); - using (var tmpHtml = new Utils.TemporaryFile("html")) { + var pdf = new TempFile("pdf"); + using (var tmpHtml = new TempFile("html")) { await File.WriteAllTextAsync(tmpHtml.FilePath, await Render()); await Pdf.Convert(tmpHtml.FilePath, pdf.FilePath); } diff --git a/WGneu/Documents/Pdf.cs b/WGneu/Documents/Pdf.cs index 1c2b404..ffbb64f 100644 --- a/WGneu/Documents/Pdf.cs +++ b/WGneu/Documents/Pdf.cs @@ -66,7 +66,7 @@ namespace WGneu.Documents { doc.Save(path); } - public static void Show(Utils.TemporaryFile file, string title) { + public static void Show(TempFile file, string title) { App.MainDispatcher.BeginInvoke(() => { var w = new DocumentViewerWindow(title, file); w.Show(); diff --git a/WGneu/Helpers/TempFile.cs b/WGneu/Helpers/TempFile.cs new file mode 100644 index 0000000..e2f614b --- /dev/null +++ b/WGneu/Helpers/TempFile.cs @@ -0,0 +1,45 @@ +using System; +using System.IO; + +namespace WGneu.Helpers { + public sealed class TempFile : IDisposable { + private int Usages = 0; + public string FilePath { get; private set; } + + public TempFile() : this(null) { } + + public TempFile(string? ext) : this(Path.Combine(Path.GetTempPath(), "Elwig"), ext) { } + + public TempFile(string dir, string? ext) { + FilePath = Path.Combine(dir, Path.GetRandomFileName().Replace(".", "") + (ext != null ? $".{ext}" : "")); + Usages++; + Create(); + } + + ~TempFile() { + Delete(); + } + + public void Dispose() { + if (--Usages == 0) { + Delete(); + GC.SuppressFinalize(this); + } + } + + public TempFile NewReference() { + Usages++; + return this; + } + + private void Create() { + using (File.Create(FilePath)) { }; + } + + private void Delete() { + if (FilePath == null) return; + File.Delete(FilePath); + FilePath = null; + } + } +} diff --git a/WGneu/Helpers/Utils.cs b/WGneu/Helpers/Utils.cs index f080449..f27a78f 100644 --- a/WGneu/Helpers/Utils.cs +++ b/WGneu/Helpers/Utils.cs @@ -1,12 +1,10 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; using System.Windows.Media; using System.Windows; using System.Windows.Controls; -using System.IO; using System.Diagnostics; namespace WGneu.Helpers { @@ -63,46 +61,5 @@ namespace WGneu.Helpers { UseShellExecute = true, }); } - - public sealed class TemporaryFile : IDisposable { - private int Usages = 0; - public string FilePath { get; private set; } - - public TemporaryFile() : this(null) { } - - public TemporaryFile(string? ext) : this(Path.Combine(Path.GetTempPath(), "Kelwin"), ext) { } - - public TemporaryFile(string dir, string? ext) { - FilePath = Path.Combine(dir, Path.GetRandomFileName().Replace(".", "") + (ext != null ? $".{ext}" : "")); - Usages++; - Create(); - } - - ~TemporaryFile() { - Delete(); - } - - public void Dispose() { - if (--Usages == 0) { - Delete(); - GC.SuppressFinalize(this); - } - } - - public TemporaryFile NewReference() { - Usages++; - return this; - } - - private void Create() { - using (File.Create(FilePath)) { }; - } - - private void Delete() { - if (FilePath == null) return; - File.Delete(FilePath); - FilePath = null; - } - } } } diff --git a/WGneu/Helpers/Validator.cs b/WGneu/Helpers/Validator.cs index cc9e615..315faa3 100644 --- a/WGneu/Helpers/Validator.cs +++ b/WGneu/Helpers/Validator.cs @@ -1,9 +1,6 @@ using System; using System.Collections.Generic; -using System.Drawing; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows.Controls; namespace WGneu.Helpers { diff --git a/WGneu/Helpers/WgContext.cs b/WGneu/Helpers/WgContext.cs index 73af0ed..9373050 100644 --- a/WGneu/Helpers/WgContext.cs +++ b/WGneu/Helpers/WgContext.cs @@ -1,9 +1,4 @@ using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using WGneu.Models; namespace WGneu.Helpers { diff --git a/WGneu/Windows/DocumentViewerWindow.xaml.cs b/WGneu/Windows/DocumentViewerWindow.xaml.cs index 5376eff..13d3ae4 100644 --- a/WGneu/Windows/DocumentViewerWindow.xaml.cs +++ b/WGneu/Windows/DocumentViewerWindow.xaml.cs @@ -16,7 +16,7 @@ using WGneu.Helpers; namespace WGneu.Windows { public partial class DocumentViewerWindow : Window { - private Utils.TemporaryFile? PdfFile = null; + private TempFile? PdfFile = null; public DocumentViewerWindow(string title, string path) { InitializeComponent(); @@ -24,7 +24,7 @@ namespace WGneu.Windows { WebView.Source = new($"file://{path}#view=FitH"); } - public DocumentViewerWindow(string title, Utils.TemporaryFile file) : this(title, file.FilePath) { + public DocumentViewerWindow(string title, TempFile file) : this(title, file.FilePath) { PdfFile = file; }