mirror of
https://github.com/aliasvault/aliasvault.git
synced 2026-03-19 23:28:23 -04:00
Add sample CSV import mapping logic (#542)
This commit is contained in:
committed by
Leendert de Borst
parent
423fe00692
commit
55ee3bfd4a
@@ -20,20 +20,71 @@
|
||||
{
|
||||
Logger.LogInformation($"Processing KeePass file: {e.File.Name}");
|
||||
|
||||
await Task.Delay(500);
|
||||
var importCredentials = new List<ImportCredential>();
|
||||
|
||||
// TODO: Implement actual KeePass file parsing
|
||||
// For now, return a sample credential
|
||||
var importCredentials = new List<ImportCredential>
|
||||
try
|
||||
{
|
||||
new()
|
||||
{
|
||||
ServiceName = "Sample Service",
|
||||
Username = "sample_user",
|
||||
Password = "sample_password",
|
||||
Notes = "Imported from KeePass"
|
||||
using var stream = e.File.OpenReadStream();
|
||||
using var reader = new StreamReader(stream);
|
||||
|
||||
// Read and parse header row
|
||||
var headerLine = await reader.ReadLineAsync();
|
||||
if (headerLine == null) {
|
||||
throw new InvalidDataException("CSV file header not found");
|
||||
}
|
||||
};
|
||||
|
||||
var headers = headerLine.Split(',')
|
||||
.Select((header, index) => (header.Trim('"'), index))
|
||||
.ToDictionary(h => h.Item1, h => h.index);
|
||||
|
||||
// Validate required headers
|
||||
var requiredHeaders = new[] { "Title", "Username", "Password" };
|
||||
var missingHeaders = requiredHeaders.Where(h => !headers.ContainsKey(h)).ToList();
|
||||
if (missingHeaders.Any())
|
||||
{
|
||||
throw new InvalidDataException(
|
||||
$"Missing required headers: {string.Join(", ", missingHeaders)}");
|
||||
}
|
||||
|
||||
string? line;
|
||||
while ((line = await reader.ReadLineAsync()) != null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var values = line.Split(',').Select(v => v.Trim('"')).ToArray();
|
||||
if (values.Length < headers.Count) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: check how TOTP code should be parsed: can we store the full otpauth:// string
|
||||
// or do we need to extract the secret key only?
|
||||
// TODO: currently the TOTP auth component errors with full otpauth:// string.
|
||||
var credential = new ImportCredential
|
||||
{
|
||||
ServiceName = values[headers["Title"]],
|
||||
ServiceUrl = headers.ContainsKey("URL") && !string.IsNullOrEmpty(values[headers["URL"]])
|
||||
? values[headers["URL"]]
|
||||
: string.Empty,
|
||||
Username = headers.ContainsKey("Email") && !string.IsNullOrEmpty(values[headers["Email"]])
|
||||
? values[headers["Email"]]
|
||||
: values[headers["Username"]],
|
||||
Password = values[headers["Password"]],
|
||||
TwoFactorSecret = headers.ContainsKey("OTPAuth") && !string.IsNullOrEmpty(values[headers["OTPAuth"]])
|
||||
? values[headers["OTPAuth"]]
|
||||
: string.Empty,
|
||||
Notes = headers.ContainsKey("Notes") ? values[headers["Notes"]] : string.Empty
|
||||
};
|
||||
|
||||
importCredentials.Add(credential);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error processing KeePass CSV file");
|
||||
throw;
|
||||
}
|
||||
|
||||
_importServiceCard.SetImportedCredentials(importCredentials);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user