Skip to content

Commit

Permalink
Update nugets and minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-s committed May 5, 2024
1 parent b115da4 commit 06783fa
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 99 deletions.
21 changes: 11 additions & 10 deletions src/QnapBackupDecryptor.Console/Options.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
using CommandLine;
using System.Diagnostics.CodeAnalysis;
using CommandLine;
using CommandLine.Text;

namespace QnapBackupDecryptor.Console;

public class Options
internal class Options
{
[Option('p', "password", Required = false, HelpText = "Password")]
public string? Password { get; set; }
public string? Password { get; init; }

[Option('e', "encrypted", Required = true, HelpText = "Encrypted file or folder")]
public string EncryptedSource { get; set; } = null!;
public required string EncryptedSource { get; init; } = null!;

[Option('d', "decrypted", Required = true, HelpText = "Where to place the decrypted file(s)")]
public string OutputDestination { get; set; } = null!;
public string OutputDestination { get; init; } = null!;

[Option('s', "subfolders", Required = false, HelpText = "Include Subfolders (default: false)")]
public bool IncludeSubfolders { get; set; }
public bool IncludeSubfolders { get; init; }

[Option('v', "verbose", Required = false, HelpText = "Set output to verbose")]
public bool Verbose { get; set; }
public bool Verbose { get; init; }

[Option('o', "overwrite", Required = false, HelpText = "Overwrite file(s) in output (default: false)")]
public bool Overwrite { get; set; }
public bool Overwrite { get; init; }

[Option('r', "removeencrypted", Required = false, HelpText = "Delete encrypted files when decrypted (default: false)")]
public bool RemoveEncrypted { get; set; }
public bool RemoveEncrypted { get; init; }


[Usage(ApplicationAlias = "QnapBackupDecryptor")]
// ReSharper disable once UnusedMember.Global
// ReSharper disable once UnusedMember.Global // Used by the console
public static IEnumerable<Example> Examples
{
get
Expand Down
20 changes: 9 additions & 11 deletions src/QnapBackupDecryptor.Console/Output.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using QnapBackupDecryptor.Core;
using Spectre.Console;
using System.Collections.Concurrent;

namespace QnapBackupDecryptor.Console;

internal class Output
internal static class Output
{
public static void ShowResults(ConcurrentBag<DecryptResult> decryptResults, ConcurrentBag<DeleteResult> deleteResults, bool verbose, TimeSpan swElapsed)
public static void ShowResults(IReadOnlyList<DecryptResult> decryptResults, IReadOnlyList<DeleteResult> deleteResults, bool verbose, TimeSpan swElapsed)
{
if (verbose && decryptResults.Count > 1)
{
Expand All @@ -25,7 +24,7 @@ public static void ShowResults(ConcurrentBag<DecryptResult> decryptResults, Conc
Environment.ExitCode = 1;
}

private static void ShowSimpleResults(ConcurrentBag<DecryptResult> decryptResults, ConcurrentBag<DeleteResult> deleteResults)
private static void ShowSimpleResults(IReadOnlyList<DecryptResult> decryptResults, IReadOnlyList<DeleteResult> deleteResults)
{
var table = new Table();
table
Expand All @@ -46,7 +45,7 @@ private static void ShowSimpleResults(ConcurrentBag<DecryptResult> decryptResult
AnsiConsole.Write(table);
}

private static void ShowFileListResults(ConcurrentBag<DecryptResult> decryptResults, ConcurrentBag<DeleteResult> deleteResults)
private static void ShowFileListResults(IReadOnlyList<DecryptResult> decryptResults, IReadOnlyList<DeleteResult> deleteResults)
{
var table = new Table();
table
Expand Down Expand Up @@ -98,10 +97,10 @@ private static List<string> DecryptResultToRow(DecryptResult decryptResult)
return row;
}

private static List<string> DeleteResultToRow(DeleteResult deleteResult)
private static IEnumerable<string> DeleteResultToRow(DeleteResult? deleteResult)
{
if (deleteResult == null)
return new List<string>(0);
return [];

var colour = deleteResult.DeletedOk ? "green" : "red";
var status = deleteResult.DeletedOk ? "Deleted" : "Failed";
Expand Down Expand Up @@ -131,11 +130,10 @@ private static void ShowTiming(TimeSpan swElapsed)
}

public static ProgressColumn[] GetProgressColumns()
=> new ProgressColumn[]
{
=> [
new TaskDescriptionColumn(),
new ProgressBarColumn(),
new PercentageColumn(),
new SpinnerColumn(Spinner.Known.SimpleDots),
};
new SpinnerColumn(Spinner.Known.SimpleDots)
];
}
26 changes: 17 additions & 9 deletions src/QnapBackupDecryptor.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ private static void Run(Options options)

var decryptJobs = GetDecryptJobs(options);

DoDecrypt(decryptJobs, options, password, stopwatch);
var (decryptResults, deleteResults) = DoDecrypt(decryptJobs, options, password);

stopwatch.Stop();

Output.ShowResults(decryptResults, deleteResults, options.Verbose, stopwatch.Elapsed);
}

private static IReadOnlyList<FileJob> GetDecryptJobs(Options options)
Expand All @@ -41,18 +45,23 @@ private static IReadOnlyList<FileJob> GetDecryptJobs(Options options)
statusContext.Spinner(Spinner.Known.SimpleDots);
statusContext.SpinnerStyle(Style.Parse("green"));
decryptJobs = JobMaker.GetDecryptJobs(options.EncryptedSource, options.OutputDestination, options.Overwrite, options.IncludeSubfolders);
decryptJobs.AddRange(
JobMaker.GetDecryptJobs(
encryptedSource: options.EncryptedSource,
decryptedTarget: options.OutputDestination,
overwrite: options.Overwrite,
includeSubFolders: options.IncludeSubfolders)
);
});

return decryptJobs;
}

private static void DoDecrypt(IReadOnlyList<FileJob> decryptJobs, Options options, byte[] password, Stopwatch sw)
private static (IReadOnlyList<DecryptResult> DecryptResults, IReadOnlyList<DeleteResult> DeleteResults) DoDecrypt(IReadOnlyCollection<FileJob> decryptJobs, Options options, byte[] password)
{
var decryptResults = new ConcurrentBag<DecryptResult>();
var deleteResults = new ConcurrentBag<DeleteResult>();


AnsiConsole.Progress()
.Columns(Output.GetProgressColumns())
.AutoClear(true)
Expand All @@ -61,13 +70,12 @@ private static void DoDecrypt(IReadOnlyList<FileJob> decryptJobs, Options option
var progressTask = progressContext.AddTask("[green]Decrypting Files[/]");
progressTask.MaxValue = decryptJobs.Count;
Parallel.ForEach(decryptJobs, currentJob => DecryptSingleJob(options, password, currentJob, decryptResults, deleteResults, progressTask));
Parallel.ForEach(
decryptJobs,
currentJob => DecryptSingleJob(options, password, currentJob, decryptResults, deleteResults, progressTask));
});

sw.Stop();

Output.ShowResults(decryptResults, deleteResults, options.Verbose, sw.Elapsed);
return (decryptResults.ToList(), deleteResults.ToList());

}

Expand Down
2 changes: 1 addition & 1 deletion src/QnapBackupDecryptor.Console/Prompts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace QnapBackupDecryptor.Console;

internal class Prompts
internal static class Prompts
{
public static byte[] GetPassword(Options opts)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Spectre.Console" Version="0.44.0" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
</ItemGroup>

<ItemGroup>
Expand Down
40 changes: 19 additions & 21 deletions src/QnapBackupDecryptor.Core.Tests/DecryptorTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using NUnit.Framework;
using Shouldly;
using System.Text;
using System.Text;

namespace QnapBackupDecryptor.Core.Tests;

[TestFixture]
public class DecryptorTests
{
private const string PASSWORD = "wisLUBIMyBNcnvo3eDMS";
private const string TEST_PASSWORD = "wisLUBIMyBNcnvo3eDMS";

[Test]
public void OpenSSLDecrypt_ValidPassword_OkResult()
Expand All @@ -17,7 +15,7 @@ public void OpenSSLDecrypt_ValidPassword_OkResult()
var outputFile = new FileInfo("decrypted.txt");

// Act
var passwordBytes = Encoding.UTF8.GetBytes(PASSWORD);
var passwordBytes = Encoding.UTF8.GetBytes(TEST_PASSWORD);
var sslDecrypt = OpenSsl.Decrypt(encryptedFile, passwordBytes, outputFile);

// Assert
Expand All @@ -33,7 +31,7 @@ public void OpenSSLDecrypt_Text()
var outputFile = new FileInfo("decrypted.txt");

// Act
var passwordBytes = Encoding.UTF8.GetBytes(PASSWORD);
var passwordBytes = Encoding.UTF8.GetBytes(TEST_PASSWORD);
var sslDecrypt = OpenSsl.Decrypt(encryptedFile, passwordBytes, outputFile);

// Assert
Expand All @@ -43,20 +41,20 @@ public void OpenSSLDecrypt_Text()
decryptedText[1].ShouldStartWith("line2: End!");
}

[Test]
public void OpenSSLDecrypt_Binary()
{
// Arrange
var encryptedFile = new FileInfo(Path.Combine("TestFiles", "encrypted.jpg"));
var decryptedFile = new FileInfo(Path.Combine("TestFiles", "decrypted.jpg"));

// Act
var passwordBytes = Encoding.UTF8.GetBytes(PASSWORD);
var decrypted = OpenSsl.Decrypt(encryptedFile, passwordBytes, decryptedFile);

// Assert
//var decryptedText = File.ReadAllText(decrpyted.FullName);
//decryptedText.ShouldBe("line1: this is a plaintext file\r\nline2: End!\r\n");
}
//[Test]
//public void OpenSSLDecrypt_Binary()
//{
// // Arrange
// var encryptedFile = new FileInfo(Path.Combine("TestFiles", "encrypted.jpg"));
// var decryptedFile = new FileInfo(Path.Combine("TestFiles", "decrypted.jpg"));

// // Act
// var passwordBytes = Encoding.UTF8.GetBytes(TEST_PASSWORD);
// var decrypted = OpenSsl.Decrypt(encryptedFile, passwordBytes, decryptedFile);

// // Assert
// var decryptedText = File.ReadAllText(decrypted.Data.Name);
// decryptedText.ShouldBe("line1: this is a plaintext file\r\nline2: End!\r\n");
//}

}
5 changes: 1 addition & 4 deletions src/QnapBackupDecryptor.Core.Tests/FileHelpersTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using NUnit.Framework;
using Shouldly;

namespace QnapBackupDecryptor.Core.Tests;
namespace QnapBackupDecryptor.Core.Tests;

[TestFixture]
public class FileHelpersTests
Expand Down
5 changes: 1 addition & 4 deletions src/QnapBackupDecryptor.Core.Tests/JobMakerTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using NUnit.Framework;
using Shouldly;

namespace QnapBackupDecryptor.Core.Tests;
namespace QnapBackupDecryptor.Core.Tests;

[TestFixture]
public class JobMakerTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand Down Expand Up @@ -26,15 +26,22 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="JunitXml.TestLogger" Version="3.0.114" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="JunitXml.TestLogger" Version="3.1.12" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\QnapBackupDecryptor.Core\QnapBackupDecryptor.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit" />
<Using Include="Shouldly" />
<Using Include="NUnit.Framework" />
</ItemGroup>


</Project>
2 changes: 1 addition & 1 deletion src/QnapBackupDecryptor.Core/DecryptResult.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace QnapBackupDecryptor.Core;

public record DecryptResult(FileSystemInfo Source, FileSystemInfo Dest, bool Success, string ErrorMessage);
public sealed record DecryptResult(FileSystemInfo Source, FileSystemInfo Dest, bool Success, string ErrorMessage);
2 changes: 1 addition & 1 deletion src/QnapBackupDecryptor.Core/DeleteResult.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace QnapBackupDecryptor.Core;

public record DeleteResult(FileSystemInfo ToDelete, bool DeletedOk, string ErrorMessage);
public sealed record DeleteResult(FileSystemInfo ToDelete, bool DeletedOk, string ErrorMessage);
4 changes: 2 additions & 2 deletions src/QnapBackupDecryptor.Core/FileInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace QnapBackupDecryptor.Core;

public static class FileInfoExtensions
internal static class FileInfoExtensions
{
public static bool TryDelete(this FileInfo fileInfo)
internal static bool TryDelete(this FileInfo fileInfo)
{
try
{
Expand Down
9 changes: 1 addition & 8 deletions src/QnapBackupDecryptor.Core/FileJob.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
namespace QnapBackupDecryptor.Core;

public record FileJob(FileSystemInfo EncryptedFile, FileSystemInfo OutputFile, bool IsValid, string ErrorMessage);

public static class FileJobExtensions
{

public static List<FileJob> ToList(this FileJob job)
=> new() { job };
}
public sealed record FileJob(FileSystemInfo EncryptedFile, FileSystemInfo OutputFile, bool IsValid, string ErrorMessage);
7 changes: 7 additions & 0 deletions src/QnapBackupDecryptor.Core/FileJobExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace QnapBackupDecryptor.Core;

internal static class FileJobExtensions
{
internal static List<FileJob> ToList(this FileJob job)
=> [job];
}
4 changes: 2 additions & 2 deletions src/QnapBackupDecryptor.Core/JobMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ private static FileJob GetFileToFileJob(FileInfo encrytedFile, FileInfo outputFi
return new FileJob(encrytedFile, outputFile, true, string.Empty);
}

private static FileJob GetFileToFolderJob(FileInfo encrytedFile, DirectoryInfo outputFolder, bool overwrite)
private static FileJob GetFileToFolderJob(FileInfo encrytedFile, FileSystemInfo outputFolder, bool overwrite)
{
var outputFile = new FileInfo(Path.Combine(outputFolder.FullName, encrytedFile.Name));
return GetFileToFileJob(encrytedFile, outputFile, overwrite);
}

private static List<FileJob> GetFolderToFolderJobs(DirectoryInfo encrytedFolder, DirectoryInfo outputFolder, bool overwrite, bool includeSubfolders)
private static List<FileJob> GetFolderToFolderJobs(DirectoryInfo encrytedFolder, FileSystemInfo outputFolder, bool overwrite, bool includeSubfolders)
{
if (encrytedFolder.Exists == false)
return new FileJob(encrytedFolder, outputFolder, false, "Encrypted folder doesn't exist").ToList();
Expand Down
Loading

0 comments on commit 06783fa

Please sign in to comment.