Skip to content

Commit 8d1163a

Browse files
committed
Adding support to extract PDF file from URL
1 parent 608db13 commit 8d1163a

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

taskt/Core/Automation/Commands/PDFTextExtractionCommand.cs

+50-5
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,30 @@
88

99
namespace taskt.Core.Automation.Commands
1010
{
11-
[Serializable]
11+
[Serializable]
1212
[Attributes.ClassAttributes.Group("Data Commands")]
1313
[Attributes.ClassAttributes.Description("")]
1414
[Attributes.ClassAttributes.UsesDescription("")]
1515
[Attributes.ClassAttributes.ImplementationDescription("")]
1616
public class PDFTextExtractionCommand : ScriptCommand
1717
{
1818
[XmlAttribute]
19-
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the PDF file path")]
19+
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the PDF file path or PDF file URL")]
2020
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
21-
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the applicable file.")]
22-
[Attributes.PropertyAttributes.SampleUsage(@"C:\temp\myfile.pdf or [vFilePath]")]
21+
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the applicable file or enter file URL.")]
22+
[Attributes.PropertyAttributes.SampleUsage(@"C:\temp\myfile.pdf , [vFilePath] or https://temp.com/myfile.pdf")]
2323
[Attributes.PropertyAttributes.Remarks("")]
2424
public string v_FilePath { get; set; }
2525

26+
[XmlAttribute]
27+
[Attributes.PropertyAttributes.PropertyDescription("Please select source type of PDF file")]
28+
[Attributes.PropertyAttributes.PropertyUISelectionOption("File Path")]
29+
[Attributes.PropertyAttributes.PropertyUISelectionOption("File URL")]
30+
[Attributes.PropertyAttributes.InputSpecification("Select source type of PDF file")]
31+
[Attributes.PropertyAttributes.SampleUsage("Select **File Path**, **File URL**")]
32+
[Attributes.PropertyAttributes.Remarks("")]
33+
public string v_FileSourceType { get; set; }
34+
2635
[XmlAttribute]
2736
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the PDF text")]
2837
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
@@ -42,10 +51,44 @@ public PDFTextExtractionCommand()
4251
public override void RunCommand(object sender)
4352
{
4453

45-
//get variable path to source file
54+
//get variable path or URL to source file
4655
var vSourceFilePath = v_FilePath.ConvertToUserVariable(sender);
56+
// get source type of file either from a physical file or from a URL
57+
var vSourceFileType = v_FileSourceType.ConvertToUserVariable(sender);
4758

59+
if (vSourceFileType == "File URL")
60+
{
61+
//create temp directory
62+
var tempDir = Core.IO.Folders.GetFolder(Folders.FolderType.TempFolder);
63+
var tempFile = System.IO.Path.Combine(tempDir, $"{ Guid.NewGuid()}.pdf");
64+
65+
//check if directory does not exist then create directory
66+
if (!System.IO.Directory.Exists(tempDir))
67+
{
68+
System.IO.Directory.CreateDirectory(tempDir);
69+
}
70+
71+
// Create webClient to download the file for extraction
72+
var webclient = new System.Net.WebClient();
73+
var uri = new Uri(vSourceFilePath);
74+
webclient.DownloadFile(uri, tempFile);
75+
76+
// check if file is downloaded successfully
77+
if (System.IO.File.Exists(tempFile))
78+
{
79+
vSourceFilePath = tempFile;
80+
}
81+
82+
// Free not needed resources
83+
uri = null;
84+
if (webclient != null)
85+
{
86+
webclient.Dispose();
87+
webclient = null;
88+
}
89+
}
4890

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

70113
//create standard group controls
114+
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_FileSourceType", this, editor));
115+
71116
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_FilePath", this, editor));
72117

73118

taskt/Core/IO/Folders.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace taskt.Core.IO
88
{
9-
public static class Folders
9+
public static class Folders
1010
{
1111
public static string GetFolder(FolderType folderType)
1212
{
@@ -26,6 +26,9 @@ public static string GetFolder(FolderType folderType)
2626
case FolderType.LogFolder:
2727
//return logs folder
2828
return System.IO.Path.Combine(GetFolder(FolderType.RootFolder), "Logs\\");
29+
case FolderType.TempFolder:
30+
//return temp folder
31+
return System.IO.Path.GetTempPath() + "\\taskt\\";
2932
default:
3033
//enum is not implemented
3134
throw new NotImplementedException("FolderType " + folderType.ToString() + " Not Supported");
@@ -34,13 +37,13 @@ public static string GetFolder(FolderType folderType)
3437
}
3538

3639

37-
public enum FolderType
40+
public enum FolderType
3841
{
3942
RootFolder,
4043
SettingsFolder,
4144
ScriptsFolder,
4245
LogFolder,
43-
46+
TempFolder
4447
}
4548
}
4649
}

0 commit comments

Comments
 (0)