Skip to content

Commit f0201dc

Browse files
Add download folder command
1 parent b86c30f commit f0201dc

10 files changed

+295
-242
lines changed

Commands/DownloadFolderCommand.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Linq;
3+
using CommandLine;
4+
using FluentFTP;
5+
6+
namespace Ftp.Client.Commands
7+
{
8+
[Verb("download-folder", HelpText = "Download folder content")]
9+
public class DownloadFolderCommand : FtpCommand
10+
{
11+
[Option('p', "path", Required = true)]
12+
public string Path
13+
{
14+
get;
15+
set;
16+
}
17+
18+
protected override void Execute(IFtpClient ftpClient)
19+
{
20+
var tempFolder = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetRandomFileName()));
21+
22+
var result = ftpClient.DownloadDirectory(tempFolder, Path);
23+
if (result.Any())
24+
{
25+
26+
result.ForEach(ftpResult =>
27+
{
28+
if (ftpResult.IsSuccess)
29+
{
30+
Console.BackgroundColor = ConsoleColor.DarkGreen;
31+
Console.WriteLine($"Download to: {tempFolder}");
32+
Console.WriteLine($"{ftpResult.Name}: Success: true");
33+
Console.ResetColor();
34+
35+
}
36+
else
37+
{
38+
Console.BackgroundColor = ConsoleColor.Red;
39+
Console.WriteLine($"Download to: {tempFolder}");
40+
Console.WriteLine($"{ftpResult.Name}: Success: false, Exception: {ftpResult.Exception}");
41+
Console.ResetColor();
42+
}
43+
});
44+
}
45+
else
46+
{
47+
Console.BackgroundColor = ConsoleColor.Red;
48+
Console.ForegroundColor = ConsoleColor.White;
49+
Console.WriteLine($"No files downloaded");
50+
Console.ResetColor();
51+
}
52+
}
53+
}
54+
}

Commands/FetchFolderCommand.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Linq;
3+
using CommandLine;
4+
using FluentFTP;
5+
6+
namespace Ftp.Client.Commands
7+
{
8+
[Verb("fetch-folder", HelpText = "Fetch folder content list")]
9+
public class FetchFolderCommand : FtpCommand
10+
{
11+
[Option('p', "path", Required = true)]
12+
public string Path
13+
{
14+
get;
15+
set;
16+
}
17+
18+
protected override void Execute(IFtpClient ftpClient)
19+
{
20+
var listing = ftpClient.GetNameListing(Path);
21+
22+
Console.WriteLine("Result: ");
23+
foreach (var s in listing)
24+
{
25+
Console.WriteLine(s);
26+
}
27+
}
28+
}
29+
}

Commands/FileUploadCommand.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Linq;
3+
using CommandLine;
4+
using FluentFTP;
5+
6+
namespace Ftp.Client.Commands
7+
{
8+
[Verb("upload-file", HelpText = "Upload file from local path to a remote path")]
9+
public class FileUploadCommand : FtpCommand
10+
{
11+
[Option('p', "path", Required = true)]
12+
public string Path
13+
{
14+
get;
15+
set;
16+
}
17+
18+
[Option('f', "local-path", Required = true, HelpText = "Local path of the file to upload")]
19+
public string LocalFile
20+
{
21+
get;
22+
set;
23+
}
24+
25+
protected override void Execute(IFtpClient ftpClient)
26+
{
27+
var result = ftpClient.UploadFile(LocalFile, Path);
28+
29+
Console.WriteLine("Result: ");
30+
Console.WriteLine(result);
31+
}
32+
}
33+
}

Commands/FtpCommand.cs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using System;
2+
using System.Linq;
3+
using CommandLine;
4+
using FluentFTP;
5+
6+
namespace Ftp.Client.Commands
7+
{
8+
public abstract class FtpCommand : ICommand
9+
{
10+
[Option('h', "Host", Required = true)]
11+
public string Host
12+
{
13+
get;
14+
set;
15+
}
16+
17+
[Option('u', "username", Required = true)]
18+
public string UserName
19+
{
20+
get;
21+
set;
22+
}
23+
24+
[Option('s', "password", Required = false)]
25+
public string Password
26+
{
27+
get;
28+
set;
29+
}
30+
31+
[Option('l', "ssl", Required = false, HelpText = "Enable ssl support")]
32+
public bool EnableSsl
33+
{
34+
get;
35+
set;
36+
}
37+
38+
[Option('v', "passive", Required = false, HelpText = "Set true to enable passive mode")]
39+
public bool EnablePassive
40+
{
41+
get;
42+
set;
43+
}
44+
45+
[Option('e', "external-ip", Required = false, HelpText = "Set this parameter to your NAT outgoing IP if you are operating within a NAT")]
46+
public string ExternalIp
47+
{
48+
get;
49+
set;
50+
}
51+
52+
53+
[Option('r', "certificate", Required = false, HelpText = "Certificate string. If this parameter has been set then the validation will occur.")]
54+
public string Certificate
55+
{
56+
get;
57+
set;
58+
}
59+
60+
[Option('d', "debug", Required = false, HelpText = "Enables detailed logging.")]
61+
public bool DebugMode
62+
{
63+
get;
64+
set;
65+
}
66+
67+
[Option('a', "active_ports", Required = false, HelpText = "Comma separated active ports")]
68+
public string ActivePorts
69+
{
70+
get;
71+
set;
72+
}
73+
74+
public void Execute()
75+
{
76+
var ftpClient = new FtpClient(Host, UserName, Password);
77+
ConfigureFtpClient(ftpClient);
78+
ftpClient.Connect();
79+
try
80+
{
81+
Execute(ftpClient);
82+
Console.WriteLine("Press any key to exit...");
83+
Console.ReadLine();
84+
}
85+
catch (Exception ex)
86+
{
87+
Console.WriteLine(ex);
88+
}
89+
finally
90+
{
91+
ftpClient?.Disconnect();
92+
}
93+
94+
}
95+
96+
protected abstract void Execute(IFtpClient ftpClient);
97+
98+
protected virtual void ConfigureFtpClient(FtpClient ftpClient)
99+
{
100+
if (DebugMode)
101+
ftpClient.OnLogEvent += (level, s) => Console.WriteLine($"{level}: {s}");
102+
103+
if (EnableSsl)
104+
{
105+
ftpClient.EncryptionMode = FtpEncryptionMode.Explicit;
106+
ftpClient.DataConnectionType = FtpDataConnectionType.PORT;
107+
}
108+
109+
if (!string.IsNullOrEmpty(ExternalIp))
110+
ftpClient.AddressResolver = () => ExternalIp;
111+
112+
if (EnablePassive)
113+
ftpClient.DataConnectionType = FtpDataConnectionType.AutoPassive;
114+
115+
if (!string.IsNullOrEmpty(ActivePorts))
116+
{
117+
ftpClient.ActivePorts = ActivePorts.Split(",").Select(s => int.Parse(s.Trim())).ToArray();
118+
}
119+
120+
ftpClient.ValidateCertificate += (control, args) =>
121+
{
122+
Console.BackgroundColor = ConsoleColor.DarkGreen;
123+
Console.WriteLine("Certificate to validate: " + args?.Certificate?.GetCertHashString());
124+
Console.ResetColor();
125+
126+
if (!string.IsNullOrEmpty(Certificate))
127+
{
128+
var isValid = args.Certificate.GetCertHashString()?.ToUpper() == Certificate.Replace(":", "").ToUpper();
129+
if (!isValid)
130+
throw new InvalidOperationException();
131+
}
132+
};
133+
134+
}
135+
}
136+
}

FetchFolderCommand.cs

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)