-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathVaultReader.cs
More file actions
104 lines (89 loc) · 4.46 KB
/
VaultReader.cs
File metadata and controls
104 lines (89 loc) · 4.46 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using OwlCore.Storage;
using SecureFolderFS.Core.DataModels;
using SecureFolderFS.Shared.ComponentModel;
using SecureFolderFS.Shared.Extensions;
using SecureFolderFS.Storage.Extensions;
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
namespace SecureFolderFS.Core.VaultAccess
{
// TODO: Needs docs
public sealed class VaultReader
{
private readonly IFolder _vaultFolder;
private readonly IAsyncSerializer<Stream> _serializer;
public VaultReader(IFolder vaultFolder, IAsyncSerializer<Stream> serializer)
{
_vaultFolder = vaultFolder;
_serializer = serializer;
}
/// <summary>
/// Reads the keystore as the specified type.
/// </summary>
public async Task<TKeystore> ReadKeystoreAsync<TKeystore>(CancellationToken cancellationToken)
where TKeystore : class
{
// Get the keystore file
if (await _vaultFolder.GetFirstByNameAsync(Constants.Vault.Names.VAULT_KEYSTORE_FILENAME, cancellationToken) is not IFile keystoreFile)
throw new FileNotFoundException("The keystore file was not found.");
return await ReadDataAsync<TKeystore>(keystoreFile, _serializer, cancellationToken);
}
public async Task<VaultConfigurationDataModel> ReadConfigurationAsync(CancellationToken cancellationToken)
{
// Get configuration file
if (await _vaultFolder.GetFirstByNameAsync(Constants.Vault.Names.VAULT_CONFIGURATION_FILENAME, cancellationToken) is not IFile configFile)
throw new FileNotFoundException("The configuration file was not found.");
return await ReadDataAsync<VaultConfigurationDataModel>(configFile, _serializer, cancellationToken);
}
public async Task<V4VaultConfigurationDataModel> ReadV4ConfigurationAsync(CancellationToken cancellationToken)
{
if (await _vaultFolder.GetFirstByNameAsync(Constants.Vault.Names.VAULT_CONFIGURATION_FILENAME, cancellationToken) is not IFile configFile)
throw new FileNotFoundException("The configuration file was not found.");
return await ReadDataAsync<V4VaultConfigurationDataModel>(configFile, _serializer, cancellationToken);
}
public async Task<VaultSharesDataModel?> ReadComplementationAsync(CancellationToken cancellationToken)
{
try
{
var complementFile = await _vaultFolder.GetFileByNameAsync(Constants.Vault.Names.VAULT_COMPLEMENTATION_FILENAME, cancellationToken);
return await ReadDataAsync<VaultSharesDataModel?>(complementFile, _serializer, cancellationToken);
}
catch (Exception)
{
return null;
}
}
public async Task<VersionDataModel> ReadVersionAsync(CancellationToken cancellationToken)
{
// Get configuration file
if (await _vaultFolder.GetFirstByNameAsync(Constants.Vault.Names.VAULT_CONFIGURATION_FILENAME, cancellationToken) is not IFile configFile)
throw new FileNotFoundException("The configuration file was not found.");
return await ReadDataAsync<VersionDataModel>(configFile, _serializer, cancellationToken);
}
public async Task<TCapability?> ReadAuthenticationAsync<TCapability>(string fileName, CancellationToken cancellationToken)
where TCapability : VaultCapabilityDataModel
{
try
{
// Try to get authentication file
var authFile = await _vaultFolder.GetFileByNameAsync(fileName, cancellationToken);
return await ReadDataAsync<TCapability?>(authFile, _serializer, cancellationToken);
}
catch (Exception)
{
return null;
}
}
private static async Task<TData> ReadDataAsync<TData>(IFile file, IAsyncSerializer<Stream> serializer, CancellationToken cancellationToken)
{
// Open a stream to the data file
await using var dataStream = await file.OpenStreamAsync(FileAccess.Read, cancellationToken);
var data = await serializer.DeserializeAsync<Stream, TData?>(dataStream, cancellationToken);
_ = data ?? throw new SerializationException($"Data could not be deserialized into {typeof(TData).Name}.");
return data;
}
}
}