Skip to content

Commit

Permalink
v3.0-p2
Browse files Browse the repository at this point in the history
- Added help information to CommandLine
- General code cleanup in CommandLine
- Removed UserSettings.xml dependency in DataBuilder
- Added pattern argument to CommandLine
- Added option for randomized names to CommandLine
- Cleaned up some code in frmMain and integrated into DataBuilder
- Added UserSettingsModel (partially implemented) for frmMain_BackgroundWorker and Program.cs for CommandLine so that multiple CommandLine instances can run without the UserSettings.xml file being overwritten every time
- Added ReleasesModel for Update Checking (not implemented)
- Added Updater.cs for Update Checking (not implemented)
  • Loading branch information
Laim committed Nov 8, 2023
1 parent 420ea07 commit 752f577
Show file tree
Hide file tree
Showing 12 changed files with 576 additions and 135 deletions.
215 changes: 167 additions & 48 deletions Snap2HTML-NG.CommandLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,121 @@
using Snap2HTMLNG.Shared.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using static System.Net.Mime.MediaTypeNames;

namespace Snap2HTMLNG.CommandLine
{
internal class Program
{

private static bool _validationCheck = false;
static void Main(string[] args)
{
List<CommandLineModel> normalizedArgs = Shared.Utils.CommandLine.Helpers.CommandLineSplit(args);

InitialValidation(normalizedArgs);
#if DEBUG
// Information
Shared.Utils.CommandLine.Helpers.WriteDebug($" THIS IS A PREVIEW BUILD.");
// New Lines
Console.WriteLine("");
#endif

string scanPath = normalizedArgs.Find(x => x.Name == "path").Value;
string savePath = normalizedArgs.Find(x => x.Name == "output").Value;

// Validate that the Scan Path actually exists
if(!Directory.Exists(scanPath))
{
Shared.Utils.CommandLine.Helpers.WriteError($"Your scan path {scanPath} does not exist or Snap2HTML-NG does not have access.");
return;
}
List<CommandLineModel> normalizedArgs = Shared.Utils.CommandLine.Helpers.CommandLineSplit(args);

// Check if the Save location actually exists
if(!Directory.Exists(Path.GetDirectoryName(savePath)))
if(normalizedArgs.Exists(x => x.Name == "help"))
{
Shared.Utils.CommandLine.Helpers.WriteError($"Your save path {savePath} does not exist or Snap2HTML-NG does not have access.");
HelpInformation();
return;
}

// Check if the rest of the settigns have been passed and assign
bool skipHidden = !normalizedArgs.Exists(x => x.Name == "hidden"); // if found, do not skip
bool skipSystem = !normalizedArgs.Exists(x => x.Name == "system"); // if found, do not skip

// Check if the user wants to link files to the root which allows them to be easily selected when viewing in a browser
bool linkFilesToRoot = false;
if(normalizedArgs.Exists(x => x.Name == "link"))
{
linkFilesToRoot = true;
string linkPath = normalizedArgs.Find(x => x.Name == "link").Value;
}
InitialValidation(normalizedArgs);

string title = $"Snapshot of {savePath}";
if(normalizedArgs.Exists(x => x.Name == "title"))
// Check if the initial validation checks have passed
if(_validationCheck)
{
title = normalizedArgs.Find(x => x.Name == "title").Value;
}
string rootDirectory = normalizedArgs.Find(x => x.Name == "path").Value;// + @"\";
string saveDirectory = normalizedArgs.Find(x => x.Name == "output").Value;

// Check if the user has requested a randomized file name and set if they have
if(normalizedArgs.Exists(x =>x.Name == "randomize"))
{
Shared.Utils.CommandLine.Helpers.WriteInformation($"Randomized file name requested...");

string randomFileName = $"{Guid.NewGuid()}.html";

saveDirectory = $@"{saveDirectory}\{randomFileName}";

Shared.Utils.CommandLine.Helpers.WriteInformation($"Randomized file set to {randomFileName}");

Shared.Utils.CommandLine.Helpers.WriteInformation($"Output path is now: {saveDirectory}");

}

// Validate that the Scan Path actually exists
if (!Directory.Exists(rootDirectory))
{
Shared.Utils.CommandLine.Helpers.WriteError($"Your scan path {rootDirectory} does not exist or Snap2HTML-NG does not have access.");
return;
}

// Check if the Save location actually exists
if (!Directory.Exists(Path.GetDirectoryName(saveDirectory)))
{
Shared.Utils.CommandLine.Helpers.WriteError($"Your save path {saveDirectory} does not exist or Snap2HTML-NG does not have access.");
return;
}

// Check if the rest of the settigns have been passed and assign
bool skipHidden = !normalizedArgs.Exists(x => x.Name == "hidden"); // if found, do not skip
bool skipSystem = !normalizedArgs.Exists(x => x.Name == "system"); // if found, do not skip

// Check if the user wants to link files to the root which allows them to be easily selected when viewing in a browser
bool linkFilesToRoot = false;
string linkDirectory = "";
if (normalizedArgs.Exists(x => x.Name == "link"))
{
linkFilesToRoot = true;
linkDirectory = normalizedArgs.Find(x => x.Name == "link").Value;
}

string title = $"Snapshot of {saveDirectory}";
if (normalizedArgs.Exists(x => x.Name == "title"))
{
title = normalizedArgs.Find(x => x.Name == "title").Value;
}

string searchPattern = "*"; // default is all
if (normalizedArgs.Exists(x => x.Name == "pattern"))
{
searchPattern = normalizedArgs.Find(x => x.Name == "pattern").Value;
}

#if DEBUG
foreach (var normalizedArg in normalizedArgs)
{
Console.WriteLine($"Name: {normalizedArg.Name}, Value: {normalizedArg.Value}");
}
foreach (var normalizedArg in normalizedArgs)
{
Shared.Utils.CommandLine.Helpers.WriteDebug($"Name: {normalizedArg.Name}, Value: {normalizedArg.Value}");
}
#endif
// Create the settings model and assign the relevant information to each property
UserSettingsModel usm = new UserSettingsModel
{
RootDirectory = rootDirectory,
Title = title,
OutputFile = saveDirectory,
SkipHiddenItems = skipHidden,
SkipSystemItems = skipSystem,
OpenInBrowserAfterCapture = false, // this will always be false in console mode
LinkFiles = linkFilesToRoot,
LinkRoot = linkDirectory,
SearchPattern = searchPattern
};

// Get product name etc.
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
var productName = fvi.ProductName;
var productVersion = fvi.ProductVersion;

// TODO: Need to change DataBuilder.cs in Shared to not read from the settings file and to accept
// data passed through from the commandLine.
DataBuilder.Build(productName, productVersion);
DataBuilder.Build(usm, ApplicationInformation().ProductName, ApplicationInformation().ProductVersion);


Console.ReadKey();
Console.ReadKey();
}
}


Expand All @@ -86,22 +131,96 @@ static void InitialValidation(List<CommandLineModel> normalizedArgs)
// Check if we have included any arguments at all, otherwise exit out.
if (normalizedArgs.Count == 0)
{
Shared.Utils.CommandLine.Helpers.WriteError("No arguments have been supplied that are recognized.");
Shared.Utils.CommandLine.Helpers.WriteError("No arguments have been supplied that are recognized. Use -h for help.");
_validationCheck = false;
return;
}

// Check if we have included the REQUIRED argument -path:, otherwise exit out.
if (!normalizedArgs.Exists(x => x.Name == "path"))
{
Shared.Utils.CommandLine.Helpers.WriteError("You are missing the required argument '-path:'");
Shared.Utils.CommandLine.Helpers.WriteError("You are missing the required argument '-path:'. Use -h for help.");
_validationCheck = false;
return;
}

// Check if we have included the REQUIRED argument -output:, otherwise exit out.
if (!normalizedArgs.Exists(x => x.Name == "output"))
{
Shared.Utils.CommandLine.Helpers.WriteError("You are missing the required argument '-output:'");
Shared.Utils.CommandLine.Helpers.WriteError("You are missing the required argument '-output:'. Use -h for help.");
_validationCheck = false;
return;
}

_validationCheck = true;
}

/// <summary>
/// Returns Help information for using the Command Line
/// </summary>
static void HelpInformation()
{
Console.ForegroundColor = ConsoleColor.White;

// Information
Console.WriteLine(" Application Information");
Console.WriteLine($" {ApplicationInformation().ProductName} v{ApplicationInformation().ProductVersion}");

// New Lines
Console.WriteLine("");

// Description
Console.WriteLine(" Description:");
Console.WriteLine(" Help information for Snap2HTML-NG.CommandLine");

// New Lines
Console.WriteLine("");

// Usage
Console.WriteLine(" Usage:");
Console.WriteLine(" Snap2HTML-NG.CommandLine [options]");

// New Lines
Console.WriteLine("");

// Options
Console.WriteLine(" Options:");
Console.WriteLine(" -path: [Required] The directory you want to scan");
Console.WriteLine(" -output: [Required] The directory where you want to save the file, including the filename unless using -randomize");
Console.WriteLine(" -link: [Optional] The directory where you want to link files in the html file");
Console.WriteLine(" -title: [Optional] The title of the file which appears at the top of the html file");
Console.WriteLine(" -hidden [Optional] Hides Hidden files from the scan, default is TRUE");
Console.WriteLine(" -system [Optional] Hides System files from the scan, default is TRUE");
Console.WriteLine(" -help, -h [Optional] Shows this information");
Console.WriteLine(" -pattern [Optional] Search pattern to only return certain files, default is *");
Console.WriteLine(" -randomize [Optional] Generates a random file name instead of needing to specify one in -output");

// New Lines
Console.WriteLine("");

// Examples
Console.WriteLine(" Examples:");
Console.WriteLine(" Snap2HTML-NG.CommandLine -path:\"C:\\John.Doe\\Downloads\" -output:\"C:\\John.Doe\\Desktop\\Downloads.html\" ");
Console.WriteLine(" Snap2HTML-NG.CommandLine -path:\"C:\\John.Doe\\Downloads\" -output:\"C:\\John.Doe\\Desktop\" -randomize ");
Console.WriteLine(" Snap2HTML-NG.CommandLine -path:\"C:\\John.Doe\\Downloads\" -output:\"C:\\John.Doe\\Desktop\" -link:\"C:\\John.Doe\\Downloads\" -randomize ");
Console.WriteLine(" Snap2HTML-NG.CommandLine -path:\"C:\\John.Doe\\Downloads\" -output:\"C:\\John.Doe\\Desktop\" -link:\"C:\\John.Doe\\Downloads\" -randomize -pattern:\"*.mp4\"");
Console.WriteLine(" Snap2HTML-NG.CommandLine -path:\"C:\\John.Doe\\Videos\" -output:\"C:\\John.Doe\\Desktop\\videos.html\" -link:\"C:\\John.Doe\\Videos\" -pattern:\"*.mp4\" -title:\"Home Videos\"");

}

/// <summary>
/// Gets the application information using <see cref="FileVersionInfo"/> and <see cref="Assembly"/>
/// </summary>
/// <returns>
/// <see cref="FileVersionInfo"/>
/// </returns>
static FileVersionInfo ApplicationInformation()
{
// Get product name etc.
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);

return fvi;
}
}
}
4 changes: 2 additions & 2 deletions Snap2HTML-NG.CommandLine/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("3.0.0.0")]
[assembly: AssemblyFileVersion("3.0.0.0")]
32 changes: 3 additions & 29 deletions Snap2HTML-NG.GUI/Forms/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public frmMain()
private void frmMain_Load(object sender, EventArgs e)
{

Shared.Updater.Updater updater = new Shared.Updater.Updater();
updater.CheckForUpdate();

LoadUserSettings();

Text = Application.ProductName + " (Press F1 for Help)";
Expand Down Expand Up @@ -156,35 +159,6 @@ private void cmdCreate_Click(object sender, EventArgs e)

private void StartProcessing()
{
// ensure source path format
var rootFolder = Path.GetFullPath(XmlConfigurator.Read("RootFolder"));

if (rootFolder.EndsWith(@"\")) rootFolder = rootFolder.Substring(0, rootFolder.Length - 1);
if (Shared.Utils.Legacy.Helpers.IsWildcardMatch("?:", rootFolder, false)) rootFolder += @"\"; // add backslash to path if only letter and colon eg "c:"

// add slash or backslash to end of link (in cases where it is clear that we we can)

bool linkFiles = bool.Parse(XmlConfigurator.Read("LinkFiles"));
string linkRoot = XmlConfigurator.Read("LinkRoot");
if (linkFiles)
{
if (!linkRoot.EndsWith(@"/"))
{
if (linkRoot.ToLower().StartsWith(@"http") || linkRoot.ToLower().StartsWith(@"https")) // web site
{
linkRoot += @"/";
}
if (Shared.Utils.Legacy.Helpers.IsWildcardMatch("?:*", linkRoot, false)) // local disk
{
linkRoot += @"\";
}
if (linkRoot.StartsWith(@"\\")) // unc path
{
linkRoot += @"\";
}
}
}

Cursor.Current = Cursors.WaitCursor;
Text = "Snap2HTMLNG (Working... Press Escape to Cancel)";
tabCtrl.Enabled = false;
Expand Down
19 changes: 18 additions & 1 deletion Snap2HTML-NG.GUI/Forms/frmMain_BackgroundWorker.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.ComponentModel;
using System.Windows.Forms;
using Snap2HTMLNG.Shared.Builder;
using Snap2HTMLNG.Shared.Models;
using Snap2HTMLNG.Shared.Settings;

namespace Snap2HTMLNG
{
Expand All @@ -9,7 +11,22 @@ public partial class frmMain : Form
// This runs on a separate thread from the GUI
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
DataBuilder.Build(Application.ProductName, Application.ProductVersion, backgroundWorker);

// Load the user settings from the configuration file as we're using the GUI here
UserSettingsModel usm = new UserSettingsModel
{
RootDirectory = XmlConfigurator.Read("RootFolder"),
Title = XmlConfigurator.Read("Title"),
OutputFile = XmlConfigurator.Read("OutputFile"),
SkipHiddenItems = bool.Parse(XmlConfigurator.Read("SkipHiddenItems")),
SkipSystemItems = bool.Parse(XmlConfigurator.Read("SkipSystemItems")),
OpenInBrowserAfterCapture = bool.Parse(XmlConfigurator.Read("OpenInBrowserAfterCapture")),
LinkFiles = bool.Parse(XmlConfigurator.Read("LinkFiles")),
LinkRoot = XmlConfigurator.Read("LinkRoot"),
SearchPattern = XmlConfigurator.Read("SearchPattern")
};

DataBuilder.Build(usm, Application.ProductName, Application.ProductVersion, backgroundWorker);
}

}
Expand Down
Loading

0 comments on commit 752f577

Please sign in to comment.