Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to extract PDF file from URL #165

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions taskt/Core/Automation/Commands/PDFTextExtractionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,30 @@

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Serializable]
[Attributes.ClassAttributes.Group("Data Commands")]
[Attributes.ClassAttributes.Description("")]
[Attributes.ClassAttributes.UsesDescription("")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class PDFTextExtractionCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the PDF file path")]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the PDF file path or PDF file URL")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the applicable file.")]
[Attributes.PropertyAttributes.SampleUsage(@"C:\temp\myfile.pdf or [vFilePath]")]
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the applicable file or enter file URL.")]
[Attributes.PropertyAttributes.SampleUsage(@"C:\temp\myfile.pdf , [vFilePath] or https://temp.com/myfile.pdf")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_FilePath { get; set; }

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

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the PDF text")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
Expand All @@ -42,10 +51,44 @@ public PDFTextExtractionCommand()
public override void RunCommand(object sender)
{

//get variable path to source file
//get variable path or URL to source file
var vSourceFilePath = v_FilePath.ConvertToUserVariable(sender);
// get source type of file either from a physical file or from a URL
var vSourceFileType = v_FileSourceType.ConvertToUserVariable(sender);

if (vSourceFileType == "File URL")
{
//create temp directory
var tempDir = Core.IO.Folders.GetFolder(Folders.FolderType.TempFolder);
var tempFile = System.IO.Path.Combine(tempDir, $"{ Guid.NewGuid()}.pdf");

//check if directory does not exist then create directory
if (!System.IO.Directory.Exists(tempDir))
{
System.IO.Directory.CreateDirectory(tempDir);
}

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

// check if file is downloaded successfully
if (System.IO.File.Exists(tempFile))
{
vSourceFilePath = tempFile;
}

// Free not needed resources
uri = null;
if (webclient != null)
{
webclient.Dispose();
webclient = null;
}
}

// Check if file exists before proceeding
if (!System.IO.File.Exists(vSourceFilePath))
{
throw new System.IO.FileNotFoundException("Could not find file: " + vSourceFilePath);
Expand All @@ -68,6 +111,8 @@ public override List<Control> Render(frmCommandEditor editor)
base.Render(editor);

//create standard group controls
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_FileSourceType", this, editor));

RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_FilePath", this, editor));


Expand Down
9 changes: 6 additions & 3 deletions taskt/Core/IO/Folders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace taskt.Core.IO
{
public static class Folders
public static class Folders
{
public static string GetFolder(FolderType folderType)
{
Expand All @@ -26,6 +26,9 @@ public static string GetFolder(FolderType folderType)
case FolderType.LogFolder:
//return logs folder
return System.IO.Path.Combine(GetFolder(FolderType.RootFolder), "Logs\\");
case FolderType.TempFolder:
//return temp folder
return System.IO.Path.GetTempPath() + "\\taskt\\";
default:
//enum is not implemented
throw new NotImplementedException("FolderType " + folderType.ToString() + " Not Supported");
Expand All @@ -34,13 +37,13 @@ public static string GetFolder(FolderType folderType)
}


public enum FolderType
public enum FolderType
{
RootFolder,
SettingsFolder,
ScriptsFolder,
LogFolder,

TempFolder
}
}
}