Skip to content

Commit

Permalink
simplify html backup converter
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-mh committed Feb 8, 2025
1 parent b497248 commit 749803b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
9 changes: 2 additions & 7 deletions Stratum.Core/src/Converter/HtmlBackupConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
Expand All @@ -21,20 +22,14 @@ public override Task<ConversionResult> ConvertAsync(byte[] data, string password
var document = new HtmlDocument();
document.LoadHtml(html);

var builder = new StringBuilder();
var nodes = document.DocumentNode.SelectNodes("//code");

if (nodes == null)
{
throw new ArgumentException("No URIs found in file");
}

foreach (var node in nodes)
{
builder.AppendLine(node.InnerText);
}

return Task.FromResult(ConvertText(builder.ToString()));
return Task.FromResult(ConvertLines(nodes.Select(n => n.InnerText)));
}
}
}
18 changes: 8 additions & 10 deletions Stratum.Core/src/Converter/UriListBackupConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Stratum.Core.Util;
Expand All @@ -23,23 +22,22 @@ public UriListBackupConverter(IIconResolver iconResolver) : base(iconResolver)
public override Task<ConversionResult> ConvertAsync(byte[] data, string password = null)
{
var text = Encoding.UTF8.GetString(data);
return Task.FromResult(ConvertText(text));
var lines = text.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
return Task.FromResult(ConvertLines(lines));
}

protected ConversionResult ConvertText(string text)
protected ConversionResult ConvertLines(IEnumerable<string> lines)
{
var lines = text.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);

if (!lines.All(l => l.StartsWith("otpauth") || l.StartsWith("motp")))
{
throw new ArgumentException("Invalid file");
}

var authenticators = new List<Authenticator>();
var failures = new List<ConversionFailure>();

foreach (var line in lines)
{
if (!line.StartsWith("otpauth") && !line.StartsWith("motp"))
{
throw new ArgumentException("Invalid file");
}

Authenticator auth;

try
Expand Down

0 comments on commit 749803b

Please sign in to comment.