-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.cs
More file actions
90 lines (78 loc) · 4.11 KB
/
Copy pathStorage.cs
File metadata and controls
90 lines (78 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Text.Json;
namespace FMPReceiptPrinter;
public static class Storage
{
private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true };
public static string Root => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ARCWorksSimpleReceiptPrinter");
public static string TemplateFolder => Path.Combine(Root, "Templates");
public static string ReceiptFolder => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ARCWorks Simple Receipt Printer", "Receipts");
public static string SignatureFolder => Path.Combine(Root, "Signatures");
private static string StatePath => Path.Combine(Root, "state.json");
public static void Initialize()
{
Directory.CreateDirectory(Root);
Directory.CreateDirectory(TemplateFolder);
Directory.CreateDirectory(ReceiptFolder);
Directory.CreateDirectory(SignatureFolder);
var bundled = Path.Combine(AppContext.BaseDirectory, "Templates");
if (Directory.Exists(bundled))
{
foreach (var file in Directory.GetFiles(bundled, "*.json"))
{
var destination = Path.Combine(TemplateFolder, Path.GetFileName(file));
if (!File.Exists(destination)) File.Copy(file, destination);
}
}
}
public static List<ReceiptTemplate> LoadTemplates() => LoadTemplatesFrom(TemplateFolder);
public static List<ReceiptTemplate> LoadTemplatesFrom(string folder)
{
var templates = new List<ReceiptTemplate>();
if (!Directory.Exists(folder)) return templates;
foreach (var path in Directory.GetFiles(folder, "*.json"))
{
try
{
var template = JsonSerializer.Deserialize<ReceiptTemplate>(File.ReadAllText(path), JsonOptions);
if (template is not null) templates.Add(template);
}
catch (JsonException) { /* Skip malformed user templates without blocking startup. */ }
catch (IOException) { /* Skip templates that are temporarily unreadable. */ }
catch (UnauthorizedAccessException) { /* Skip inaccessible templates. */ }
}
return templates.OrderBy(x => x.Name).ToList();
}
public static AppState LoadState()
{
try { return File.Exists(StatePath) ? JsonSerializer.Deserialize<AppState>(File.ReadAllText(StatePath), JsonOptions) ?? new() : new(); }
catch { return new(); }
}
public static void SaveState(AppState state) => File.WriteAllText(StatePath, JsonSerializer.Serialize(state, JsonOptions));
public static string SaveReceipt(ReceiptData receipt)
{
Directory.CreateDirectory(ReceiptFolder);
var safeCustomer = string.Concat(receipt.Customer.Select(c => Path.GetInvalidFileNameChars().Contains(c) ? '_' : c)).Trim();
if (string.IsNullOrWhiteSpace(safeCustomer)) safeCustomer = "Customer";
var path = Path.Combine(ReceiptFolder, $"DR-{receipt.ReceiptNumber:00000}_{receipt.Date:yyyy-MM-dd}_{safeCustomer}.json");
File.WriteAllText(path, JsonSerializer.Serialize(receipt, JsonOptions));
return path;
}
public static string ImportTemplate(string source)
{
Directory.CreateDirectory(TemplateFolder);
_ = JsonSerializer.Deserialize<ReceiptTemplate>(File.ReadAllText(source), JsonOptions)
?? throw new InvalidDataException("Template JSON is empty or invalid.");
var destination = Path.Combine(TemplateFolder, Path.GetFileName(source));
File.Copy(source, destination, true);
return destination;
}
public static string ImportSignature(string source)
{
Directory.CreateDirectory(SignatureFolder);
var extension = Path.GetExtension(source).ToLowerInvariant();
if (extension is not ".png" and not ".jpg" and not ".jpeg" and not ".bmp") throw new InvalidDataException("Signature must be a PNG, JPG, JPEG, or BMP image.");
var destination = Path.Combine(SignatureFolder, $"signature{extension}");
File.Copy(source, destination, true);
return destination;
}
}