Move TempFile to own class
This commit is contained in:
@ -10,7 +10,7 @@ using WGneu.Helpers;
|
|||||||
namespace WGneu.Documents {
|
namespace WGneu.Documents {
|
||||||
public abstract class Document : IDisposable {
|
public abstract class Document : IDisposable {
|
||||||
|
|
||||||
private Utils.TemporaryFile? PdfFile = null;
|
private TempFile? PdfFile = null;
|
||||||
|
|
||||||
public Document(string title) {
|
public Document(string title) {
|
||||||
Title = title;
|
Title = title;
|
||||||
@ -51,8 +51,8 @@ namespace WGneu.Documents {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task Generate() {
|
public async Task Generate() {
|
||||||
var pdf = new Utils.TemporaryFile("pdf");
|
var pdf = new TempFile("pdf");
|
||||||
using (var tmpHtml = new Utils.TemporaryFile("html")) {
|
using (var tmpHtml = new TempFile("html")) {
|
||||||
await File.WriteAllTextAsync(tmpHtml.FilePath, await Render());
|
await File.WriteAllTextAsync(tmpHtml.FilePath, await Render());
|
||||||
await Pdf.Convert(tmpHtml.FilePath, pdf.FilePath);
|
await Pdf.Convert(tmpHtml.FilePath, pdf.FilePath);
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ namespace WGneu.Documents {
|
|||||||
doc.Save(path);
|
doc.Save(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Show(Utils.TemporaryFile file, string title) {
|
public static void Show(TempFile file, string title) {
|
||||||
App.MainDispatcher.BeginInvoke(() => {
|
App.MainDispatcher.BeginInvoke(() => {
|
||||||
var w = new DocumentViewerWindow(title, file);
|
var w = new DocumentViewerWindow(title, file);
|
||||||
w.Show();
|
w.Show();
|
||||||
|
45
WGneu/Helpers/TempFile.cs
Normal file
45
WGneu/Helpers/TempFile.cs
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.IO;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace WGneu.Helpers {
|
namespace WGneu.Helpers {
|
||||||
@ -63,46 +61,5 @@ namespace WGneu.Helpers {
|
|||||||
UseShellExecute = true,
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
|
|
||||||
namespace WGneu.Helpers {
|
namespace WGneu.Helpers {
|
||||||
|
@ -1,9 +1,4 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using WGneu.Models;
|
using WGneu.Models;
|
||||||
|
|
||||||
namespace WGneu.Helpers {
|
namespace WGneu.Helpers {
|
||||||
|
@ -16,7 +16,7 @@ using WGneu.Helpers;
|
|||||||
namespace WGneu.Windows {
|
namespace WGneu.Windows {
|
||||||
public partial class DocumentViewerWindow : Window {
|
public partial class DocumentViewerWindow : Window {
|
||||||
|
|
||||||
private Utils.TemporaryFile? PdfFile = null;
|
private TempFile? PdfFile = null;
|
||||||
|
|
||||||
public DocumentViewerWindow(string title, string path) {
|
public DocumentViewerWindow(string title, string path) {
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
@ -24,7 +24,7 @@ namespace WGneu.Windows {
|
|||||||
WebView.Source = new($"file://{path}#view=FitH");
|
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;
|
PdfFile = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user