2013-11-03 23:53:49 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2015-08-13 13:07:38 +12:00
|
|
|
|
Copyright (c) 2007-2015 ShareX Team
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
2014-05-25 08:43:58 +12:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.FtpClient;
|
|
|
|
|
using System.Net.Security;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
public sealed class FTP : FileUploader, IDisposable
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
public FTPAccount Account { get; private set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-05-31 02:59:08 +12:00
|
|
|
|
public bool IsConnected
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return client != null && client.IsConnected;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
private FtpClient client;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
public FTP(FTPAccount account)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
Account = account;
|
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
client = new FtpClient()
|
|
|
|
|
{
|
|
|
|
|
Host = Account.Host,
|
|
|
|
|
Port = Account.Port,
|
|
|
|
|
Credentials = new NetworkCredential(Account.Username, Account.Password)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (account.IsActive)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
client.DataConnectionType = FtpDataConnectionType.AutoActive;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
client.DataConnectionType = FtpDataConnectionType.AutoPassive;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 10:24:51 +12:00
|
|
|
|
if (account.Protocol == FTPProtocol.FTPS)
|
2014-05-25 08:43:58 +12:00
|
|
|
|
{
|
2014-05-25 10:24:51 +12:00
|
|
|
|
switch (Account.FTPSEncryption)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
case FTPSEncryption.Explicit:
|
|
|
|
|
client.EncryptionMode = FtpEncryptionMode.Explicit;
|
|
|
|
|
break;
|
|
|
|
|
case FTPSEncryption.Implicit:
|
|
|
|
|
client.EncryptionMode = FtpEncryptionMode.Implicit;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
client.DataConnectionEncryption = true;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-05-25 10:24:51 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(account.FTPSCertificateLocation) && File.Exists(account.FTPSCertificateLocation))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 10:24:51 +12:00
|
|
|
|
X509Certificate cert = X509Certificate2.CreateFromSignedFile(Account.FTPSCertificateLocation);
|
2014-05-25 08:43:58 +12:00
|
|
|
|
client.ClientCertificates.Add(cert);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-10-19 10:48:47 +13:00
|
|
|
|
client.ValidateCertificate += (control, e) =>
|
2014-05-25 08:43:58 +12:00
|
|
|
|
{
|
|
|
|
|
if (e.PolicyErrors != SslPolicyErrors.None)
|
|
|
|
|
{
|
|
|
|
|
e.Accept = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-25 08:43:58 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-06-25 09:41:55 +12:00
|
|
|
|
#region FileUploader methods
|
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
|
|
|
|
UploadResult result = new UploadResult();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
Fix issue #372
Several changes were made to fix the encoding of ftp dirpaths and allow
uploads to folders with non-ascii characters:
-Fixed encoding of valid urls in ShareX.HelpersLib.NameParser.cs
-Fixed subfolder path generation and wrong url re-encoding in
FTPAccount.cs
-Fixed preview paths in FTP Destination settings (FTPAccount.cs)
-Fixed subfolder path generation in FTP.cs
This fixes issue #372, which was reproduced with the following account
config on a local ftp server (import from clipboard on destination
settings):
{
"Protocol": "FTP",
"Name": "New account",
"Host": "localhost",
"Port": 21,
"Username": "test",
"Password": "test",
"ServerProtocol": "ftp",
"SubFolderPath": "DEV/grhh本人%",
"BrowserProtocol": "http",
"HttpHomePath": "",
"HttpHomePathAutoAddSubFolderPath": true,
"HttpHomePathNoExtension": false,
"IsActive": false,
"FTPSEncryption": "Explicit",
"FTPSCertificateLocation": "",
"Passphrase": null,
"Keypath": null
}
2015-01-02 10:44:18 +13:00
|
|
|
|
string subFolderPath = Account.GetSubFolderPath(null, NameParserType.FolderPath);
|
2014-08-10 10:59:26 +12:00
|
|
|
|
string path = subFolderPath.CombineURL(fileName);
|
2014-06-25 09:41:55 +12:00
|
|
|
|
bool uploadResult;
|
2014-05-25 08:43:58 +12:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IsUploading = true;
|
2014-06-25 09:41:55 +12:00
|
|
|
|
uploadResult = UploadData(stream, path);
|
2014-05-25 08:43:58 +12:00
|
|
|
|
}
|
|
|
|
|
finally
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-06-25 09:26:00 +12:00
|
|
|
|
Dispose();
|
2014-05-25 08:43:58 +12:00
|
|
|
|
IsUploading = false;
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-06-25 09:41:55 +12:00
|
|
|
|
if (uploadResult && !StopUploadRequested && !IsError)
|
2014-05-25 08:43:58 +12:00
|
|
|
|
{
|
2014-08-10 10:59:26 +12:00
|
|
|
|
result.URL = Account.GetUriPath(fileName, subFolderPath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
return result;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
public override void StopUpload()
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-06-25 09:26:00 +12:00
|
|
|
|
if (IsUploading && !StopUploadRequested)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-06-25 09:26:00 +12:00
|
|
|
|
StopUploadRequested = true;
|
2014-09-13 19:36:16 +12:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Disconnect();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 09:41:55 +12:00
|
|
|
|
#endregion FileUploader methods
|
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
public bool Connect()
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
if (!client.IsConnected)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
client.Connect();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
return client.IsConnected;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Disconnect()
|
|
|
|
|
{
|
2014-06-25 09:26:00 +12:00
|
|
|
|
if (client != null)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
client.Disconnect();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 12:09:49 +12:00
|
|
|
|
public bool UploadData(Stream localStream, string remotePath)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (Connect())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
using (Stream remoteStream = client.OpenWrite(remotePath))
|
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
return TransferData(localStream, remoteStream);
|
2014-05-25 08:43:58 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2014-05-25 12:09:49 +12:00
|
|
|
|
catch (FtpCommandException e)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
// Probably directory not exist, try creating it
|
2014-07-14 20:27:40 +12:00
|
|
|
|
if (e.CompletionCode == "550" || e.CompletionCode == "553")
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-31 02:59:08 +12:00
|
|
|
|
CreateMultiDirectory(URLHelpers.GetDirectoryPath(remotePath));
|
2014-05-25 08:43:58 +12:00
|
|
|
|
|
|
|
|
|
using (Stream remoteStream = client.OpenWrite(remotePath))
|
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
return TransferData(localStream, remoteStream);
|
2014-05-25 08:43:58 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2014-05-25 12:09:49 +12:00
|
|
|
|
|
|
|
|
|
throw e;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-25 12:09:49 +12:00
|
|
|
|
|
|
|
|
|
return false;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UploadData(byte[] data, string remotePath)
|
|
|
|
|
{
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(data, false))
|
|
|
|
|
{
|
|
|
|
|
UploadData(stream, remotePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UploadFile(string localPath, string remotePath)
|
|
|
|
|
{
|
|
|
|
|
using (FileStream stream = new FileStream(localPath, FileMode.Open))
|
|
|
|
|
{
|
|
|
|
|
UploadData(stream, remotePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UploadImage(Image image, string remotePath)
|
|
|
|
|
{
|
|
|
|
|
using (MemoryStream stream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
image.Save(stream, image.RawFormat);
|
|
|
|
|
UploadData(stream, remotePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UploadText(string text, string remotePath)
|
|
|
|
|
{
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(text), false))
|
|
|
|
|
{
|
|
|
|
|
UploadData(stream, remotePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UploadFiles(string[] localPaths, string remotePath)
|
|
|
|
|
{
|
|
|
|
|
foreach (string file in localPaths)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(file))
|
|
|
|
|
{
|
|
|
|
|
string filename = Path.GetFileName(file);
|
2014-05-25 08:43:58 +12:00
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
if (File.Exists(file))
|
|
|
|
|
{
|
2014-06-10 08:00:59 +12:00
|
|
|
|
UploadFile(file, URLHelpers.CombineURL(remotePath, filename));
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
else if (Directory.Exists(file))
|
|
|
|
|
{
|
|
|
|
|
List<string> filesList = new List<string>();
|
|
|
|
|
filesList.AddRange(Directory.GetFiles(file));
|
|
|
|
|
filesList.AddRange(Directory.GetDirectories(file));
|
2014-06-10 08:00:59 +12:00
|
|
|
|
string path = URLHelpers.CombineURL(remotePath, filename);
|
2014-05-31 02:59:08 +12:00
|
|
|
|
CreateDirectory(path);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
UploadFiles(filesList.ToArray(), path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 12:09:49 +12:00
|
|
|
|
public void DownloadFile(string remotePath, Stream localStream)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
if (Connect())
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
using (Stream remoteStream = client.OpenRead(remotePath))
|
|
|
|
|
{
|
|
|
|
|
TransferData(remoteStream, localStream);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 12:09:49 +12:00
|
|
|
|
public void DownloadFile(string remotePath, string localPath)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
using (FileStream fs = new FileStream(localPath, FileMode.Create))
|
2014-05-25 08:43:58 +12:00
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
DownloadFile(remotePath, fs);
|
2014-05-25 08:43:58 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
public void DownloadFiles(IEnumerable<FtpListItem> files, string localPath, bool recursive = true)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
foreach (FtpListItem file in files)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (file != null && !string.IsNullOrEmpty(file.Name))
|
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
if (recursive && file.Type == FtpFileSystemObjectType.Directory)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 16:39:13 +12:00
|
|
|
|
FtpListItem[] newFiles = GetListing(file.FullName);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
string directoryPath = Path.Combine(localPath, file.Name);
|
2014-05-25 08:43:58 +12:00
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
if (!Directory.Exists(directoryPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(directoryPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DownloadFiles(newFiles, directoryPath);
|
|
|
|
|
}
|
2014-05-25 08:43:58 +12:00
|
|
|
|
else if (file.Type == FtpFileSystemObjectType.File)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
string filePath = Path.Combine(localPath, file.Name);
|
|
|
|
|
DownloadFile(file.FullName, filePath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 16:39:13 +12:00
|
|
|
|
public FtpListItem[] GetListing(string remotePath)
|
|
|
|
|
{
|
|
|
|
|
return client.GetListing(remotePath);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-31 02:59:08 +12:00
|
|
|
|
public bool DirectoryExists(string remotePath)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (Connect())
|
|
|
|
|
{
|
2014-05-31 02:59:08 +12:00
|
|
|
|
return client.DirectoryExists(remotePath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-31 02:59:08 +12:00
|
|
|
|
public bool CreateDirectory(string remotePath)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (Connect())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
client.CreateDirectory(remotePath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-31 02:59:08 +12:00
|
|
|
|
public List<string> CreateMultiDirectory(string remotePath)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-30 21:09:13 +12:00
|
|
|
|
List<string> paths = URLHelpers.GetPaths(remotePath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
foreach (string path in paths)
|
|
|
|
|
{
|
2014-05-31 02:59:08 +12:00
|
|
|
|
if (CreateDirectory(path))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
DebugHelper.WriteLine("FTP directory created: " + path);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-31 02:59:08 +12:00
|
|
|
|
|
|
|
|
|
return paths;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Rename(string fromRemotePath, string toRemotePath)
|
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
if (Connect())
|
|
|
|
|
{
|
|
|
|
|
client.Rename(fromRemotePath, toRemotePath);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteFile(string remotePath)
|
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
if (Connect())
|
|
|
|
|
{
|
|
|
|
|
client.DeleteFile(remotePath);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 08:43:58 +12:00
|
|
|
|
public void DeleteFiles(IEnumerable<FtpListItem> files)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
foreach (FtpListItem file in files)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (file != null && !string.IsNullOrEmpty(file.Name))
|
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
if (file.Type == FtpFileSystemObjectType.Directory)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
DeleteDirectory(file.FullName);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2014-05-25 08:43:58 +12:00
|
|
|
|
else if (file.Type == FtpFileSystemObjectType.File)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
DeleteFile(file.FullName);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteDirectory(string remotePath)
|
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
if (Connect())
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-30 21:09:13 +12:00
|
|
|
|
string filename = URLHelpers.GetFileName(remotePath);
|
2014-05-25 12:09:49 +12:00
|
|
|
|
if (filename == "." || filename == "..")
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-05-25 16:39:13 +12:00
|
|
|
|
FtpListItem[] files = GetListing(remotePath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-05-25 12:09:49 +12:00
|
|
|
|
DeleteFiles(files);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-05-25 12:09:49 +12:00
|
|
|
|
client.DeleteDirectory(remotePath);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SendCommand(string command)
|
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
if (Connect())
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-05-25 12:09:49 +12:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.Execute(command);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2014-05-25 12:09:49 +12:00
|
|
|
|
|
|
|
|
|
return false;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2014-05-25 08:43:58 +12:00
|
|
|
|
if (client != null)
|
|
|
|
|
{
|
2014-06-25 09:26:00 +12:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
2014-05-25 08:43:58 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|