Skip to content

Commit a30b75d

Browse files
committed
Organize variables
1 parent 19cc190 commit a30b75d

File tree

1 file changed

+29
-42
lines changed

1 file changed

+29
-42
lines changed

src/EasySign/Bundle.cs

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,30 @@
1212
using System.Text.Json.Serialization;
1313
using System.Threading.Tasks;
1414

15+
using Microsoft.Extensions.Logging;
16+
1517
namespace SAPTeam.EasySign
1618
{
1719
/// <summary>
1820
/// Represents a bundle that holds file hashes and signatures.
1921
/// </summary>
2022
public class Bundle
2123
{
22-
private readonly string bundleName = ".eSign";
23-
private byte[]? rawZipContents = null;
24+
private readonly string _bundleName = ".eSign";
25+
private byte[] _rawZipContents = [];
26+
27+
private readonly Dictionary<string, X509Certificate2> _certCache = new();
28+
private readonly ConcurrentDictionary<string, byte[]> _newEmbeddedFiles = new();
29+
private readonly ConcurrentDictionary<string, byte[]> _fileCache = new();
30+
31+
/// <summary>
32+
/// Gets the JSON serializer options.
33+
/// </summary>
34+
protected readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions()
35+
{
36+
WriteIndented = false,
37+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
38+
};
2439

2540
/// <summary>
2641
/// Gets the root path of the bundle. This path used for relative path resolution.
@@ -30,18 +45,7 @@ public class Bundle
3045
/// <summary>
3146
/// Gets the name of the bundle file.
3247
/// </summary>
33-
public string BundleName
34-
{
35-
get
36-
{
37-
if (IsLoadedFromMemory)
38-
{
39-
throw new InvalidOperationException("Bundle is loaded from memory");
40-
}
41-
42-
return bundleName;
43-
}
44-
}
48+
public string BundleName => IsLoadedFromMemory ? throw new InvalidOperationException("Bundle is loaded from memory") : _bundleName;
4549

4650
/// <summary>
4751
/// Gets the full path of the bundle file.
@@ -58,21 +62,6 @@ public string BundleName
5862
/// </summary>
5963
public Signatures Signatures { get; private set; } = new();
6064

61-
private readonly Dictionary<string, X509Certificate2> certCache = new();
62-
63-
private readonly ConcurrentDictionary<string, byte[]> newEmbeddedFiles = new();
64-
65-
private readonly ConcurrentDictionary<string, byte[]> fileCache = new();
66-
67-
/// <summary>
68-
/// Gets the JSON serializer options.
69-
/// </summary>
70-
protected readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions()
71-
{
72-
WriteIndented = false,
73-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
74-
};
75-
7665
/// <summary>
7766
/// Gets a value indicating whether the bundle is read-only.
7867
/// </summary>
@@ -81,7 +70,7 @@ public string BundleName
8170
/// <summary>
8271
/// Gets a value indicating whether the bundle is loaded from memory.
8372
/// </summary>
84-
public bool IsLoadedFromMemory => rawZipContents != null && rawZipContents.Length > 0;
73+
public bool IsLoadedFromMemory => _rawZipContents != null && _rawZipContents.Length > 0;
8574

8675
/// <summary>
8776
/// Gets a value indicating whether the bundle is loaded.
@@ -100,7 +89,7 @@ public string BundleName
10089
/// <param name="bundleName">The name of the bundle.</param>
10190
public Bundle(string rootPath, string bundleName) : this(rootPath)
10291
{
103-
this.bundleName = bundleName;
92+
_bundleName = bundleName;
10493
}
10594

10695
/// <summary>
@@ -133,9 +122,7 @@ public ZipArchive OpenZipArchive(ZipArchiveMode mode = ZipArchiveMode.Read)
133122

134123
if (IsLoadedFromMemory)
135124
{
136-
#pragma warning disable CS8604 // Possible null reference argument.
137-
var ms = new MemoryStream(rawZipContents);
138-
#pragma warning restore CS8604 // Possible null reference argument.
125+
var ms = new MemoryStream(_rawZipContents);
139126
return new ZipArchive(ms, mode);
140127
}
141128
else
@@ -174,7 +161,7 @@ public void LoadFromBytes(byte[] bundleContent)
174161
}
175162

176163
IsReadOnly = true;
177-
rawZipContents = bundleContent;
164+
_rawZipContents = bundleContent;
178165

179166
using var zip = OpenZipArchive();
180167
ReadBundle(zip);
@@ -227,7 +214,7 @@ public void AddEntry(string path, string destinationPath = "./", string? rootPat
227214

228215
if (Manifest.StoreOriginalFiles)
229216
{
230-
newEmbeddedFiles[name] = File.ReadAllBytes(path);
217+
_newEmbeddedFiles[name] = File.ReadAllBytes(path);
231218
}
232219
}
233220

@@ -251,7 +238,7 @@ public void Sign(X509Certificate2 certificate, RSA privateKey)
251238
pemBuilder.AppendLine("-----END CERTIFICATE-----");
252239
string pemContents = pemBuilder.ToString();
253240

254-
newEmbeddedFiles[name] = Encoding.UTF8.GetBytes(pemContents);
241+
_newEmbeddedFiles[name] = Encoding.UTF8.GetBytes(pemContents);
255242
Signatures.Entries[name] = signature;
256243
}
257244

@@ -357,11 +344,11 @@ public bool VerifyCertificate(X509Certificate2 certificate, X509ChainPolicy? pol
357344
/// <returns>The certificate.</returns>
358345
public X509Certificate2 GetCertificate(string certificateHash)
359346
{
360-
if (!certCache.TryGetValue(certificateHash, out X509Certificate2? certificate))
347+
if (!_certCache.TryGetValue(certificateHash, out X509Certificate2? certificate))
361348
{
362349
using var zip = OpenZipArchive();
363350
var certData = ReadEntry(zip, certificateHash);
364-
certCache[certificateHash] = certificate = new X509Certificate2(certData);
351+
_certCache[certificateHash] = certificate = new X509Certificate2(certData);
365352
}
366353

367354
return certificate;
@@ -452,7 +439,7 @@ public void Update()
452439
var signatureData = Export(Signatures, SourceGenerationSignaturesContext.Default);
453440
WriteEntry(zip, ".signatures.ec", signatureData);
454441

455-
foreach (var newFile in newEmbeddedFiles)
442+
foreach (var newFile in _newEmbeddedFiles)
456443
{
457444
WriteEntry(zip, newFile.Key, newFile.Value);
458445
}
@@ -467,14 +454,14 @@ public void Update()
467454
/// <returns>A byte array containing the entry data.</returns>
468455
protected byte[] ReadEntry(ZipArchive zip, string entryName)
469456
{
470-
if (!fileCache.TryGetValue(entryName, out var data))
457+
if (!_fileCache.TryGetValue(entryName, out var data))
471458
{
472459
var entry = zip.GetEntry(entryName) ?? throw new FileNotFoundException("Entry not found", entryName);
473460
using var stream = entry.Open();
474461
data = ReadStream(stream);
475462

476463
if (IsReadOnly)
477-
fileCache[entryName] = data;
464+
_fileCache[entryName] = data;
478465
}
479466

480467
return data;

0 commit comments

Comments
 (0)