Skip to content

Commit

Permalink
Updated File Extraction saucepleez#219
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepleez committed Jun 7, 2020
1 parent 95b3035 commit d955a00
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 92 deletions.
168 changes: 76 additions & 92 deletions taskt/Core/Automation/Commands/ExtractFileCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using SharpCompress.Common;
using SharpCompress.Readers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -17,47 +19,33 @@ namespace taskt.Core.Automation.Commands
[Attributes.ClassAttributes.ImplementationDescription("")]
public class ExtractFileCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please select file type")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("zip")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("7z")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("xz")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("bzip2")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("tar")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("wim")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("iso")]
[Attributes.PropertyAttributes.InputSpecification("Select source file type")]
[Attributes.PropertyAttributes.SampleUsage("Select **Type file**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_FileType { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the file path or file URL to be extract")]
[Attributes.PropertyAttributes.PropertyDescription("Please enter the file location (https:// is supported)")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the applicable file or enter file URL.")]
[Attributes.PropertyAttributes.SampleUsage(@"C:\temp\myfile.zip , [vFilePath] or https://temp.com/myfile.zip")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_FilePathOrigin { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please select source file")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("File Path")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("File URL")]
[Attributes.PropertyAttributes.InputSpecification("Select source path")]
[Attributes.PropertyAttributes.SampleUsage("Select **File Path**, **File URL**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_FileSourceType { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the file path to be send after extract")]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the extraction folder (ex. C:\\temp\\myzip\\")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the applicable file or enter file URL.")]
[Attributes.PropertyAttributes.SampleUsage(@"C:\temp\ or [vFilePath]")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_PathDestination { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the list of extract files")]
[Attributes.PropertyAttributes.PropertyDescription("Optional - Indicate the archive password")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("L.")]
[Attributes.PropertyAttributes.SampleUsage(@"mypass or {vPass}")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_Password { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Optional - Indicate the variable to receive a list of extracted file names")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
Expand All @@ -67,25 +55,29 @@ public class ExtractFileCommand : ScriptCommand
public ExtractFileCommand()
{
this.CommandName = "ExtractFileCommand";
this.SelectionName = "File Extraction";
this.SelectionName = "Extract File";
this.CommandEnabled = true;
this.CustomRendering = true;
}

public override void RunCommand(object sender)
{
// get type of file either from a physical file or from a URL
var vFileType = v_FileType.ConvertToUserVariable(sender);
//get variable path or URL to source file
var vSourceFilePathOrigin = v_FilePathOrigin.ConvertToUserVariable(sender);
// get source type of file either from a physical file or from a URL
var vSourceFileType = v_FileSourceType.ConvertToUserVariable(sender);
// get file path to destination files
var vFilePathDestination = v_PathDestination.ConvertToUserVariable(sender);
// get file path to destination files
var v_VariableName = v_applyToVariableName.ConvertToUserVariable(sender);

if (vSourceFileType == "File URL")
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;

//get absolute variable path or URL to source file
var vSourceFile = v_FilePathOrigin.ConvertToUserVariable(sender);
//track local file location
string vLocalSourceFile = vSourceFile;
//get file path to destination files
var vExtractionFolder = v_PathDestination.ConvertToUserVariable(sender);
//get optional password
var vPassword = v_Password.ConvertToUserVariable(sender);
//auto-detect extension
var vFileType = Path.GetExtension(vSourceFile);
//create tracking list
var fileList = new List<string>();

if (vSourceFile.StartsWith("http://") || vSourceFile.StartsWith("https://") || vSourceFile.StartsWith("www."))
{
//create temp directory
var tempDir = Core.IO.Folders.GetFolder(Folders.FolderType.TempFolder);
Expand All @@ -99,13 +91,14 @@ public override void RunCommand(object sender)

// Create webClient to download the file for extraction
var webclient = new System.Net.WebClient();
var uri = new Uri(vSourceFilePathOrigin);
var uri = new Uri(vSourceFile);
webclient.DownloadFile(uri, tempFile);

// check if file is downloaded successfully
if (System.IO.File.Exists(tempFile))
if (File.Exists(tempFile))
{
vSourceFilePathOrigin = tempFile;
//override source file location
vLocalSourceFile = tempFile;
}

// Free not needed resources
Expand All @@ -117,80 +110,71 @@ public override void RunCommand(object sender)
}
}


// Check if file exists before proceeding
if (!System.IO.File.Exists(vSourceFilePathOrigin))
if (!System.IO.File.Exists(vLocalSourceFile))
{
throw new System.IO.FileNotFoundException("Could not find file: " + vSourceFilePathOrigin);
throw new FileNotFoundException($"Could not find file: {vLocalSourceFile}");
}

// Get 7Z app
var zPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Resources", "7z.exe");


// If the directory doesn't exist, create it.
if (!Directory.Exists(vFilePathDestination))
Directory.CreateDirectory(vFilePathDestination);

var result = "";
System.Diagnostics.Process process = new System.Diagnostics.Process();
if (!Directory.Exists(vExtractionFolder))
Directory.CreateDirectory(vExtractionFolder);

try
{
var temp = Guid.NewGuid();
//Extract in temp to get list files and directories and delete
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.UseShellExecute = false;
pro.FileName = zPath;
pro.RedirectStandardOutput = true;
pro.Arguments = "x " + vSourceFilePathOrigin + " -o" + vFilePathDestination + "/" + temp + " -aoa";
process.StartInfo = pro;
process.Start();
process.WaitForExit();
string[] dirPaths = Directory.GetDirectories(vFilePathDestination + "/" + temp, "*", SearchOption.TopDirectoryOnly);
string[] filePaths = Directory.GetFiles(vFilePathDestination + "/" + temp, "*", SearchOption.TopDirectoryOnly);

foreach (var item in dirPaths)

using (Stream stream = File.OpenRead(vLocalSourceFile))
{
result = result + item + Environment.NewLine;
IReader reader;

//check if password is needed
if (string.IsNullOrEmpty(vPassword))
{
//password not required
reader = ReaderFactory.Open(stream);
}
else
{
//password required
reader = ReaderFactory.Open(stream, new ReaderOptions() { Password = vPassword });
}


while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
Console.WriteLine(reader.Entry.Key);
reader.WriteEntryToDirectory(vExtractionFolder, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
fileList.Add(vExtractionFolder + reader.Entry.Key.Replace("/", "\\"));

}
}
}
foreach (var item in filePaths)

if (!string.IsNullOrEmpty(v_applyToVariableName))
{
result = result + item + Environment.NewLine;
engine.StoreComplexObjectInVariable(v_applyToVariableName, fileList);
}
result = result.Replace("/" + temp, "");
Directory.Delete(vFilePathDestination + "/" + temp, true);

//Extract
pro.Arguments = "x " + vSourceFilePathOrigin + " -o" + vFilePathDestination + " -aoa";
process.StartInfo = pro;
process.Start();
process.WaitForExit();


result.StoreInUserVariable(sender, v_applyToVariableName);
}
catch (System.Exception Ex)
catch (Exception)
{
process.Kill();
v_applyToVariableName = Ex.Message;
throw;
}







}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

//create standard group controls
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_FileType", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_FilePathOrigin", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_FileSourceType", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_PathDestination", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_Password", this, editor));

RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_applyToVariableName", this));
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_applyToVariableName", this).AddVariableNames(editor);
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_applyToVariableName", this, new Control[] { VariableNameControl }, editor));
Expand All @@ -202,7 +186,7 @@ public override List<Control> Render(frmCommandEditor editor)

public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Extract From '" + v_FilePathOrigin + " to " + v_PathDestination + "' and apply result to '" + v_applyToVariableName + "'";
return base.GetDisplayValue() + $" [Source File: '{v_FilePathOrigin}', Destination Folder: '{v_PathDestination}'";
}
}
}
22 changes: 22 additions & 0 deletions taskt/Core/Automation/Engine/AutomationEngineInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,28 @@ public void AddVariable(string variableName, object variableValue)
}

}

public void StoreComplexObjectInVariable(string variableName, object value)
{


Script.ScriptVariable storeVariable = VariableList.Where(x => x.VariableName == variableName).FirstOrDefault();

if (storeVariable == null)
{
//create and set variable
VariableList.Add(new Script.ScriptVariable
{
VariableName = variableName,
VariableValue = value
});
}
else
{
//set variable
storeVariable.VariableValue = value;
}
}
public void CancelScript()
{
IsCancellationPending = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Commands>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="e7e8c735-fb1a-4ef3-9117-b852981fc279" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="1" PauseBeforeExeucution="false" v_Comment="Identify a source file for extraction" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExtractFileCommand" CommandID="3220f15d-beff-48db-84a1-ffc3dec12ed8" CommandName="ExtractFileCommand" IsCommented="false" SelectionName="File Extraction" DefaultPause="0" LineNumber="2" PauseBeforeExeucution="false" v_Comment="" CommandEnabled="true" v_FilePathOrigin="https://file-examples.com/wp-content/uploads/2017/02/zip_5MB.zip" v_PathDestination="D:\Dropbox\Environment Folders\Desktop\onlinezip\" v_Password="" v_applyToVariableName="vFiles" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="eccc6920-a536-41a6-968e-f8a57475edec" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="3" PauseBeforeExeucution="false" v_Comment="Loop the variable which will contain all extracted file names" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="BeginListLoopCommand" CommandID="9ebe8d4b-68b6-4565-9c89-f9758822271c" CommandName="BeginListLoopCommand" IsCommented="false" SelectionName="Loop List" DefaultPause="0" LineNumber="4" PauseBeforeExeucution="false" CommandEnabled="true" v_LoopParameter="{vFiles}" />
<AdditionalScriptCommands>
<ScriptCommand xsi:type="CommentCommand" CommandID="964efcc5-c61e-4cb1-b4a2-16f442a0268e" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="5" PauseBeforeExeucution="false" v_Comment="Show File Name" CommandEnabled="true" />
</AdditionalScriptCommands>
<AdditionalScriptCommands>
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="4cdc83f4-c056-40cc-bc66-91b9791f7ee3" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="6" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vFiles}" v_AutoCloseAfter="0" />
</AdditionalScriptCommands>
<AdditionalScriptCommands>
<ScriptCommand xsi:type="EndLoopCommand" CommandID="9e0aba9a-e891-46fd-8708-0165d426d5ee" CommandName="EndLoopCommand" IsCommented="false" SelectionName="End Loop" DefaultPause="0" LineNumber="7" PauseBeforeExeucution="false" CommandEnabled="true" />
</AdditionalScriptCommands>
</ScriptAction>
</Commands>
<Variables />
</Script>
1 change: 1 addition & 0 deletions taskt/UI/CustomControls/CustomControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ public static Dictionary<string, Image> UIImageDictionary()
uiImages.Add("EndLoopCommand", taskt.Properties.Resources.command_endloop);
uiImages.Add("ClipboardGetTextCommand", taskt.Properties.Resources.command_files);
uiImages.Add("ClipboardSetTextCommand", taskt.Properties.Resources.command_files);
uiImages.Add("ExtractFileCommand", taskt.Properties.Resources.command_files);
uiImages.Add("ExcelCreateApplicationCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("ExcelOpenWorkbookCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("ExcelAddWorkbookCommand", taskt.Properties.Resources.command_spreadsheet);
Expand Down
5 changes: 5 additions & 0 deletions taskt/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ limitations under the License.
<package id="Serilog" version="2.7.1" targetFramework="net452" />
<package id="Serilog.Formatting.Compact" version="1.0.0" targetFramework="net452" />
<package id="Serilog.Sinks.File" version="4.0.0" targetFramework="net452" />
<package id="SharpCompress" version="0.25.1" targetFramework="net48" />
<package id="SharpSimpleNLG" version="1.2.0" targetFramework="net452" />
<package id="SuperSocket.ClientEngine.Core" version="0.8.0.13" targetFramework="net452" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net48" />
<package id="TaskScheduler" version="2.6.1" targetFramework="net452" />
<package id="WebSocket4Net" version="0.15.0" targetFramework="net452" />
</packages>
Loading

0 comments on commit d955a00

Please sign in to comment.