Using System.Net.FtpClient library instead Starksoft.Net.Ftp library.

This commit is contained in:
Jaex 2014-05-24 23:43:58 +03:00
parent 9aef38f576
commit 285662f052
13 changed files with 149 additions and 262 deletions

View file

@ -59,9 +59,6 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Starksoft.Net.Proxy">
<HintPath>..\Lib\Starksoft.Net.Proxy.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Design" />

View file

@ -23,7 +23,6 @@
#endregion License Information (GPL v3)
using Starksoft.Net.Proxy;
using System;
using System.Net;
using System.Reflection;
@ -100,39 +99,6 @@ public IWebProxy GetWebProxy()
return null;
}
// Proxy for FTP
public IProxyClient GetProxyClient()
{
if (IsValidProxy())
{
Starksoft.Net.Proxy.ProxyType proxyType;
switch (ProxyType)
{
case ProxyType.HTTP:
proxyType = Starksoft.Net.Proxy.ProxyType.Http;
break;
case ProxyType.SOCKS4:
proxyType = Starksoft.Net.Proxy.ProxyType.Socks4;
break;
case ProxyType.SOCKS4a:
proxyType = Starksoft.Net.Proxy.ProxyType.Socks4a;
break;
case ProxyType.SOCKS5:
proxyType = Starksoft.Net.Proxy.ProxyType.Socks5;
break;
default:
proxyType = Starksoft.Net.Proxy.ProxyType.None;
break;
}
ProxyClientFactory proxy = new ProxyClientFactory();
return proxy.CreateProxyClient(proxyType, Host, Port, Username, Password);
}
return null;
}
private WebProxy GetDefaultWebProxy()
{
try

Binary file not shown.

Binary file not shown.

View file

@ -557,7 +557,7 @@ public static void OpenFTPClient()
if (Program.UploadersConfig != null && Program.UploadersConfig.FTPAccountList.IsValidIndex(Program.UploadersConfig.FTPSelectedImage))
{
FTPAccount account = Program.UploadersConfig.FTPAccountList[Program.UploadersConfig.FTPSelectedImage];
new FTPClientForm(account).Show();
//new FTPClientForm(account).Show();
}
}
}

View file

@ -837,20 +837,21 @@ public UploadResult UploadFile(Stream stream, string fileName)
}
break;
case FileDestination.FTP:
int index = Info.TaskSettings.OverrideFTP ? Info.TaskSettings.FTPIndex.BetweenOrDefault(0, Program.UploadersConfig.FTPAccountList.Count - 1) : Program.UploadersConfig.GetFTPIndex(Info.DataType);
int index = Info.TaskSettings.OverrideFTP ? Info.TaskSettings.FTPIndex.BetweenOrDefault(0, Program.UploadersConfig.FTPAccountList.Count - 1) :
Program.UploadersConfig.GetFTPIndex(Info.DataType);
FTPAccount account = Program.UploadersConfig.FTPAccountList.ReturnIfValidIndex(index);
if (account != null)
{
if (account.Protocol == FTPProtocol.SFTP)
if (account.Protocol == FTPProtocol.FTP || account.Protocol == FTPProtocol.FTPS)
{
fileUploader = new FTP(account);
}
else if (account.Protocol == FTPProtocol.SFTP)
{
fileUploader = new SFTP(account);
}
else
{
fileUploader = new FTPUploader(account);
}
}
break;
case FileDestination.SharedFolder:

View file

@ -1,6 +1,6 @@
namespace UploadersLib
{
partial class FTPClientForm
/*partial class FTPClientForm
{
/// <summary>
/// Required designer variable.
@ -511,5 +511,5 @@ private void InitializeComponent()
private System.Windows.Forms.RichTextBox rtbConsole;
private System.Windows.Forms.SplitContainer scConsole;
private System.Windows.Forms.TextBox txtConsoleWrite;
}
}*/
}

View file

@ -24,7 +24,6 @@
#endregion License Information (GPL v3)
using HelpersLib;
using Starksoft.Net.Ftp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -32,10 +31,11 @@
using System.IO;
using System.Linq;
using System.Windows.Forms;
using UploadersLib.FileUploaders;
namespace UploadersLib
{
public partial class FTPClientForm : Form
/*public partial class FTPClientForm : Form
{
private const string Root = "/";
@ -56,15 +56,15 @@ public FTPClientForm(FTPAccount account)
Account = account;
FTPAdapter = new FTP(account);
FTPAdapter.Client.ClientRequest += Client_ClientRequest;
FTPAdapter.Client.ServerResponse += Client_ServerResponse;
FTPAdapter.Client.OpenAsyncCompleted += Client_OpenAsyncCompleted;
FTPAdapter.client.ClientRequest += Client_ClientRequest;
FTPAdapter.client.ServerResponse += Client_ServerResponse;
FTPAdapter.client.OpenAsyncCompleted += Client_OpenAsyncCompleted;
pgAccount.SelectedObject = FTPAdapter.Account;
Text = "FTP Client - " + account.Name;
lblConnecting.Text = "Connecting to " + account.FTPAddress;
FTPAdapter.Client.OpenAsync(account.Username, account.Password);
FTPAdapter.client.OpenAsync(account.Username, account.Password);
}
#region Methods
@ -609,5 +609,5 @@ private void FTPClient2_FormClosing(object sender, FormClosingEventArgs e)
}
#endregion Events
}
}*/
}

View file

@ -24,123 +24,147 @@
#endregion License Information (GPL v3)
using HelpersLib;
using Starksoft.Net.Ftp;
using Starksoft.Net.Proxy;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.FtpClient;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using UploadersLib.HelperClasses;
namespace UploadersLib
namespace UploadersLib.FileUploaders
{
public sealed class FTP : IDisposable
public sealed class FTP : FileUploader, IDisposable
{
public event Uploader.ProgressEventHandler ProgressChanged;
public FTPAccount Account { get; private set; }
//public bool AutoReconnect { get; set; }
public FTPAccount Account { get; set; }
public FtpClient Client { get; set; }
public bool AutoReconnect { get; set; }
private FtpClient client;
private ProgressManager progress;
public FTP(FTPAccount account, int bufferSize = 8192)
public FTP(FTPAccount account)
{
Account = account;
Client = new FtpClient(account.Host, account.Port);
Client.TcpBufferSize = bufferSize;
if (account.Protocol == FTPProtocol.FTP || account.FtpsSecurityProtocol == FtpSecurityProtocol.None)
client = new FtpClient()
{
Client.SecurityProtocol = (Starksoft.Net.Ftp.FtpSecurityProtocol)FtpSecurityProtocol.None;
Host = Account.Host,
Port = Account.Port,
Credentials = new NetworkCredential(Account.Username, Account.Password)
};
if (account.IsActive)
{
client.DataConnectionType = FtpDataConnectionType.AutoActive;
}
else
{
Client.SecurityProtocol = (Starksoft.Net.Ftp.FtpSecurityProtocol)account.FtpsSecurityProtocol;
client.DataConnectionType = FtpDataConnectionType.AutoPassive;
}
if (account.Protocol == FTPProtocol.FTPS && account.FtpsSecurityProtocol != FtpSecurityProtocol.None)
{
client.EncryptionMode = FtpEncryptionMode.Explicit;
client.DataConnectionEncryption = true;
if (!string.IsNullOrEmpty(account.FtpsCertLocation) && File.Exists(account.FtpsCertLocation))
{
Client.SecurityCertificates.Add(X509Certificate.CreateFromSignedFile(account.FtpsCertLocation));
X509Certificate cert = X509Certificate2.CreateFromSignedFile(Account.FtpsCertLocation);
client.ClientCertificates.Add(cert);
}
else
{
Client.ValidateServerCertificate += (sender, e) => e.IsCertificateValid = true;
client.ValidateCertificate += (FtpClient control, FtpSslValidationEventArgs e) =>
{
if (e.PolicyErrors != SslPolicyErrors.None)
{
e.Accept = true;
}
};
}
}
Client.DataTransferMode = account.IsActive ? TransferMode.Active : TransferMode.Passive;
if (ProxyInfo.Current != null)
{
IProxyClient proxy = ProxyInfo.Current.GetProxyClient();
if (proxy != null)
{
Client.Proxy = proxy;
}
}
Client.TransferProgress += OnTransferProgressChanged;
Client.ConnectionClosed += Client_ConnectionClosed;
}
private void OnTransferProgressChanged(object sender, TransferProgressEventArgs e)
public override UploadResult Upload(Stream stream, string fileName)
{
if (ProgressChanged != null)
UploadResult result = new UploadResult();
fileName = Helpers.GetValidURL(fileName);
string path = Account.GetSubFolderPath(fileName);
try
{
progress.UpdateProgress(e.BytesTransferred);
ProgressChanged(progress);
IsUploading = true;
UploadData(stream, path);
}
finally
{
IsUploading = false;
}
if (!stopUpload && Errors.Count == 0)
{
result.URL = Account.GetUriPath(fileName);
}
return result;
}
public override void StopUpload()
{
if (IsUploading && !stopUpload)
{
stopUpload = true;
Disconnect();
}
}
private void Client_ConnectionClosed(object sender, ConnectionClosedEventArgs e)
private string GetRemotePath(string filename)
{
if (AutoReconnect)
{
Connect();
}
}
public bool Connect(string username, string password)
{
if (!Client.IsConnected && !string.IsNullOrEmpty(password))
{
Client.Open(username, password);
}
return Client.IsConnected;
filename = Helpers.GetValidURL(filename);
return Account.GetSubFolderPath(filename);
}
public bool Connect()
{
return Connect(Account.Username, Account.Password);
if (!client.IsConnected)
{
client.Connect();
}
return client.IsConnected;
}
public void Disconnect()
{
if (Client != null && Client.IsConnected)
if (client != null && client.IsConnected)
{
Client.Close();
client.Disconnect();
}
}
public void UploadData(Stream stream, string remotePath)
public void UploadData(Stream localStream, string remotePath)
{
if (Connect())
{
progress = new ProgressManager(stream.Length);
try
{
Client.PutFile(stream, remotePath, FileAction.Create);
using (Stream remoteStream = client.OpenWrite(remotePath))
{
TransferData(localStream, remoteStream);
}
}
catch (Exception e)
{
if (e.InnerException.Message.Contains("No such file or directory"))
{
MakeMultiDirectory(FTPHelpers.GetDirectoryName(remotePath));
Client.PutFile(stream, remotePath, FileAction.Create);
using (Stream remoteStream = client.OpenWrite(remotePath))
{
TransferData(localStream, remoteStream);
}
}
else
{
@ -190,6 +214,7 @@ public void UploadFiles(string[] localPaths, string remotePath)
if (!string.IsNullOrEmpty(file))
{
string filename = Path.GetFileName(file);
if (File.Exists(file))
{
UploadFile(file, Helpers.CombineURL(remotePath, filename));
@ -207,36 +232,39 @@ public void UploadFiles(string[] localPaths, string remotePath)
}
}
public void StopUpload()
{
if (Client != null && Client.IsConnected)
{
Client.Abort();
}
}
public void DownloadFile(string remotePath, string localPath)
{
Connect();
Client.GetFile(remotePath, localPath, FileAction.Create);
using (FileStream fs = new FileStream(localPath, FileMode.Create))
{
DownloadFile(remotePath, fs);
}
}
public void DownloadFile(string remotePath, Stream outStream)
public void DownloadFile(string remotePath, Stream localStream)
{
Connect();
Client.GetFile(remotePath, outStream, false);
using (Stream remoteStream = client.OpenRead(remotePath))
{
TransferData(remoteStream, localStream);
}
}
public void DownloadFiles(IEnumerable<FtpItem> files, string localPath)
public void DownloadFiles(IEnumerable<FtpListItem> files, string localPath, bool recursive = true)
{
foreach (FtpItem file in files)
Connect();
foreach (FtpListItem file in files)
{
if (file != null && !string.IsNullOrEmpty(file.Name))
{
if (file.ItemType == FtpItemType.Directory)
if (recursive && file.Type == FtpFileSystemObjectType.Directory)
{
FtpItemCollection newFiles = GetDirList(file.FullPath);
FtpListItem[] newFiles = client.GetListing(file.FullName);
string directoryPath = Path.Combine(localPath, file.Name);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
@ -244,20 +272,15 @@ public void DownloadFiles(IEnumerable<FtpItem> files, string localPath)
DownloadFiles(newFiles, directoryPath);
}
else if (file.ItemType == FtpItemType.File)
else if (file.Type == FtpFileSystemObjectType.File)
{
DownloadFile(file.FullPath, Path.Combine(localPath, file.Name));
string filePath = Path.Combine(localPath, file.Name);
DownloadFile(file.FullName, filePath);
}
}
}
}
public FtpItemCollection GetDirList(string remotePath)
{
Connect();
return Client.GetDirList(remotePath);
}
public bool ChangeDirectory(string remotePath, bool autoCreateDirectory = false)
{
if (Connect())
@ -266,7 +289,7 @@ public bool ChangeDirectory(string remotePath, bool autoCreateDirectory = false)
try
{
Client.ChangeDirectory(remotePath);
client.SetWorkingDirectory(remotePath);
return true;
}
catch (Exception e)
@ -274,7 +297,7 @@ public bool ChangeDirectory(string remotePath, bool autoCreateDirectory = false)
if (autoCreateDirectory && e.Message.StartsWith("Could not change working directory to"))
{
MakeMultiDirectory(remotePath);
Client.ChangeDirectory(remotePath);
client.SetWorkingDirectory(remotePath);
return true;
}
@ -291,7 +314,7 @@ public bool MakeDirectory(string remotePath)
{
try
{
Client.MakeDirectory(remotePath);
client.CreateDirectory(remotePath);
return true;
}
catch (Exception e)
@ -319,28 +342,28 @@ public void MakeMultiDirectory(string remotePath)
public void Rename(string fromRemotePath, string toRemotePath)
{
Connect();
Client.Rename(fromRemotePath, toRemotePath);
client.Rename(fromRemotePath, toRemotePath);
}
public void DeleteFile(string remotePath)
{
Connect();
Client.DeleteFile(remotePath);
client.DeleteFile(remotePath);
}
public void DeleteFiles(IEnumerable<FtpItem> files)
public void DeleteFiles(IEnumerable<FtpListItem> files)
{
foreach (FtpItem file in files)
foreach (FtpListItem file in files)
{
if (file != null && !string.IsNullOrEmpty(file.Name))
{
if (file.ItemType == FtpItemType.Directory)
if (file.Type == FtpFileSystemObjectType.Directory)
{
DeleteDirectory(file.FullPath);
DeleteDirectory(file.FullName);
}
else if (file.ItemType == FtpItemType.File)
else if (file.Type == FtpFileSystemObjectType.File)
{
DeleteFile(file.FullPath);
DeleteFile(file.FullName);
}
}
}
@ -356,21 +379,11 @@ public void DeleteDirectory(string remotePath)
return;
}
FtpItemCollection files = GetDirList(remotePath);
FtpListItem[] files = client.GetListing(remotePath);
foreach (FtpItem file in files)
{
if (file.ItemType == FtpItemType.Directory)
{
DeleteDirectory(file.FullPath);
}
else
{
DeleteFile(file.FullPath);
}
}
DeleteFiles(files);
Client.DeleteDirectory(remotePath);
client.DeleteDirectory(remotePath);
}
public bool SendCommand(string command)
@ -379,7 +392,7 @@ public bool SendCommand(string command)
try
{
Client.Quote(command);
client.Execute(command);
return true;
}
catch
@ -390,8 +403,11 @@ public bool SendCommand(string command)
public void Dispose()
{
Disconnect();
Client.Dispose();
if (client != null)
{
Disconnect();
client.Dispose();
}
}
}
}

View file

@ -1,88 +0,0 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2007-2014 ShareX Developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using HelpersLib;
using System.IO;
namespace UploadersLib.FileUploaders
{
public class FTPUploader : FileUploader
{
public FTPAccount Account { get; private set; }
private FTP ftpClient;
public FTPUploader(FTPAccount account)
{
Account = account;
}
public override UploadResult Upload(Stream stream, string fileName)
{
UploadResult result = new UploadResult();
fileName = Helpers.GetValidURL(fileName);
string path = Account.GetSubFolderPath(fileName);
using (ftpClient = new FTP(Account, BufferSize))
{
ftpClient.ProgressChanged += OnProgressChanged;
try
{
IsUploading = true;
ftpClient.UploadData(stream, path);
}
finally
{
IsUploading = false;
}
}
if (!stopUpload && Errors.Count == 0)
{
result.URL = Account.GetUriPath(fileName);
}
return result;
}
public override void StopUpload()
{
if (IsUploading && !stopUpload && ftpClient != null)
{
stopUpload = true;
TaskEx.Run(() => ftpClient.StopUpload());
}
}
protected string GetRemotePath(string filename)
{
filename = Helpers.GetValidURL(filename);
return Account.GetSubFolderPath(filename);
}
}
}

View file

@ -895,7 +895,7 @@ private void FTPOpenClient()
{
if (CheckFTPAccounts())
{
new FTPClientForm(Config.FTPAccountList[ucFTPAccounts.lbAccounts.SelectedIndex]).Show();
//new FTPClientForm(Config.FTPAccountList[ucFTPAccounts.lbAccounts.SelectedIndex]).Show();
}
}

View file

@ -80,16 +80,13 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\SSH.NET.2014.4.6-beta1\lib\net40\Renci.SshNet.dll</HintPath>
</Reference>
<Reference Include="Starksoft.Net.Ftp">
<HintPath>..\Lib\Starksoft.Net.Ftp.dll</HintPath>
</Reference>
<Reference Include="Starksoft.Net.Proxy">
<HintPath>..\Lib\Starksoft.Net.Proxy.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.FtpClient">
<HintPath>..\packages\System.Net.FtpClient.1.0.5200.21177\lib\net40\System.Net.FtpClient.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel.Web" />
<Reference Include="System.Web" />
@ -114,9 +111,7 @@
<Compile Include="FileUploaders\SFTP.cs" />
<Compile Include="FileUploaders\Minus.cs" />
<Compile Include="FileUploaders\SharedFolderUploader.cs" />
<Compile Include="FTPClient\FTPClientForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="FTPClient\FTPClientForm.cs" />
<Compile Include="FTPClient\FTPClientForm.Designer.cs">
<DependentUpon>FTPClientForm.cs</DependentUpon>
</Compile>
@ -261,7 +256,6 @@
<Compile Include="ImageUploader.cs" />
<Compile Include="FileUploaders\CustomFileUploader.cs" />
<Compile Include="ImageUploaders\FlickrUploader.cs" />
<Compile Include="FileUploaders\FTPUploader.cs" />
<Compile Include="ImageUploaders\ImageBin.cs" />
<Compile Include="ImageUploaders\ImageShackUploader.cs" />
<Compile Include="ImageUploaders\Img1Uploader.cs" />

View file

@ -3,4 +3,5 @@
<package id="MegaApiClient" version="1.0.4" targetFramework="net40" />
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net40" />
<package id="SSH.NET" version="2014.4.6-beta1" targetFramework="net40" />
<package id="System.Net.FtpClient" version="1.0.5200.21177" targetFramework="net40" />
</packages>