Skip to content

Commit

Permalink
Show progress indicator when finding files and decrypting
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-s committed Feb 19, 2021
1 parent 9f1ddb7 commit fe1d3a7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
66 changes: 46 additions & 20 deletions src/QnapBackupDecryptor.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CommandLine;
using QnapBackupDecryptor.Core;
using Spectre.Console;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -18,43 +20,67 @@ static void Main(string[] args)

private static void Run(Options options)
{
// Double check delete is wanted
if (Prompts.EnsureDeleteWanted(options) == false)
return;

// get the password byes
var password = Prompts.GetPassword(options);

var decryptResults = new ConcurrentBag<DecryptResult>();
var deleteResults = new ConcurrentBag<DeleteResult>();
var decryptJobs = new List<FileJob>();

var sw = Stopwatch.StartNew();

var decryptJobs = JobMaker.GetDecryptJobs(options.EncryptedSource, options.OutputDestination, options.Overwrite, options.IncludeSubfolders);
// get file list to process
AnsiConsole.Status()
.Start("Getting Files...", ctx =>
{
ctx.Spinner(Spinner.Known.SimpleDots);
ctx.SpinnerStyle(Style.Parse("green"));
var decryptResults = new ConcurrentBag<DecryptResult>();
var deleteResults = new ConcurrentBag<DeleteResult>();
decryptJobs = JobMaker.GetDecryptJobs(options.EncryptedSource, options.OutputDestination, options.Overwrite, options.IncludeSubfolders);
});

Parallel.ForEach(decryptJobs,
job =>
// decrypt & delete if requested
AnsiConsole.Progress()
.Columns(new ProgressColumn[]
{
new TaskDescriptionColumn(),
new ProgressBarColumn(),
new PercentageColumn(),
new SpinnerColumn(Spinner.Known.SimpleDots)
})
.Start(ctx =>
{
if (job.IsValid == false)
decryptResults.Add(new DecryptResult(job.EncryptedFile, job.OutputFile, job.IsValid, job.ErrorMessage));
else
{
var result = OpenSsl.Decrypt(new FileInfo(job.EncryptedFile.FullName), password, new FileInfo(job.OutputFile.FullName));
decryptResults.Add(new DecryptResult(job.EncryptedFile, job.OutputFile, result.IsSuccess, result.ErrorMessage));
// Delete encrypted file only if success and option chosen
if (result.IsSuccess && options.RemoveEncrypted)
deleteResults.Add(FileService.TryDelete(job.EncryptedFile));
}
var progressTask = ctx.AddTask("[green]Decrypting Files[/]");
Parallel.ForEach(
decryptJobs,
job =>
{
if (job.IsValid == false)
decryptResults.Add(new DecryptResult(job.EncryptedFile, job.OutputFile, job.IsValid, job.ErrorMessage));
else
{
var decryptionResult = OpenSsl.Decrypt(new FileInfo(job.EncryptedFile.FullName), password, new FileInfo(job.OutputFile.FullName));
decryptResults.Add(new DecryptResult(job.EncryptedFile, job.OutputFile, decryptionResult.IsSuccess, decryptionResult.ErrorMessage));
// Delete encrypted file only if success and option chosen
if (decryptionResult.IsSuccess && options.RemoveEncrypted)
deleteResults.Add(FileService.TryDelete(job.EncryptedFile));
progressTask.Increment(((double)decryptResults.Count / (double)decryptJobs.Count) * (double)100);
}
});
});

sw.Stop();

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


}




}
4 changes: 2 additions & 2 deletions src/QnapBackupDecryptor.Core/JobMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static FileJob GetJob(FileInfo encrytedFile, FileInfo outputFile, bool o
if (outputFile.Exists & overwrite == false)
return new FileJob(encrytedFile, outputFile, false, "Output file already exists, use --overwrite to overwrite files.");

if (outputFile.Exists & outputFile.Attributes == FileAttributes.ReadOnly)
if (outputFile.Exists & outputFile.Attributes.HasFlag(FileAttributes.ReadOnly))
return new FileJob(encrytedFile, outputFile, false, "Cannot write to output file - it's ReadOnly in the file system.");

if (OpenSsl.IsOpenSslEncrypted(encrytedFile) == false)
Expand All @@ -60,7 +60,7 @@ private static List<FileJob> GetJobs(DirectoryInfo encrytedFolder, DirectoryInfo
if (encrytedFolder.Exists == false)
return new List<FileJob> { new FileJob(encrytedFolder, outputFolder, false, "Encrypted folder doesn't exist") };

if (outputFolder.Exists & outputFolder.Attributes == FileAttributes.ReadOnly)
if (outputFolder.Exists & outputFolder.Attributes.HasFlag(FileAttributes.ReadOnly))
return new List<FileJob> { new FileJob(encrytedFolder, outputFolder, false, "Cannot write to output folder - it's ReadOnly in the file system.") };

var fileInfos = encrytedFolder.EnumerateFiles("*.*", includeSubfolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
Expand Down

0 comments on commit fe1d3a7

Please sign in to comment.