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
|
2022-01-12 05:32:17 +13:00
|
|
|
|
Copyright (c) 2007-2022 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)
|
|
|
|
|
|
|
|
|
|
using Renci.SshNet;
|
|
|
|
|
using Renci.SshNet.Common;
|
|
|
|
|
using Renci.SshNet.Sftp;
|
2014-12-11 12:19:28 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2017-10-02 02:14:05 +13:00
|
|
|
|
using ShareX.UploadersLib.Properties;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public sealed class SFTP : FileUploader, IDisposable
|
|
|
|
|
{
|
|
|
|
|
public FTPAccount Account { get; private set; }
|
|
|
|
|
|
2017-03-24 05:10:05 +13:00
|
|
|
|
public bool IsValidAccount => (!string.IsNullOrEmpty(Account.Keypath) && File.Exists(Account.Keypath)) || !string.IsNullOrEmpty(Account.Password);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2017-03-24 05:10:05 +13:00
|
|
|
|
public bool IsConnected => client != null && client.IsConnected;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
private SftpClient client;
|
|
|
|
|
|
|
|
|
|
public SFTP(FTPAccount account)
|
|
|
|
|
{
|
|
|
|
|
Account = account;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 09:26:00 +12:00
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
|
|
|
|
UploadResult result = new UploadResult();
|
|
|
|
|
|
2014-08-10 10:59:26 +12:00
|
|
|
|
string subFolderPath = Account.GetSubFolderPath();
|
2017-04-25 02:01:35 +12:00
|
|
|
|
string path = URLHelpers.CombineURL(subFolderPath, fileName);
|
2015-12-10 07:45:55 +13:00
|
|
|
|
string url = Account.GetUriPath(fileName, subFolderPath);
|
|
|
|
|
|
|
|
|
|
OnEarlyURLCopyRequested(url);
|
2014-06-25 09:26:00 +12:00
|
|
|
|
|
2014-06-25 09:41:55 +12:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IsUploading = true;
|
2017-03-24 05:10:05 +13:00
|
|
|
|
|
|
|
|
|
bool uploadResult = UploadStream(stream, path, true);
|
2015-12-10 07:45:55 +13:00
|
|
|
|
|
|
|
|
|
if (uploadResult && !StopUploadRequested && !IsError)
|
|
|
|
|
{
|
|
|
|
|
result.URL = url;
|
|
|
|
|
}
|
2014-06-25 09:41:55 +12:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Dispose();
|
2017-03-24 05:10:05 +13:00
|
|
|
|
|
2014-06-25 09:41:55 +12:00
|
|
|
|
IsUploading = false;
|
|
|
|
|
}
|
2014-06-25 09:26:00 +12:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void StopUpload()
|
|
|
|
|
{
|
|
|
|
|
if (IsUploading && !StopUploadRequested)
|
|
|
|
|
{
|
|
|
|
|
StopUploadRequested = true;
|
2014-09-13 19:36:16 +12:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Disconnect();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
2014-06-25 09:26:00 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public bool Connect()
|
|
|
|
|
{
|
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
2016-01-09 04:52:31 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(Account.Keypath))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-01-09 04:52:31 +13:00
|
|
|
|
if (!File.Exists(Account.Keypath))
|
|
|
|
|
{
|
2017-10-02 02:14:05 +13:00
|
|
|
|
throw new FileNotFoundException(Resources.UploadersConfigForm_ConnectSFTPAccount_Key_file_not_found, Account.Keypath);
|
2016-01-09 04:52:31 +13:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
PrivateKeyFile keyFile;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(Account.Passphrase))
|
|
|
|
|
{
|
|
|
|
|
keyFile = new PrivateKeyFile(Account.Keypath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
keyFile = new PrivateKeyFile(Account.Keypath, Account.Passphrase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client = new SftpClient(Account.Host, Account.Port, Account.Username, keyFile);
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(Account.Password))
|
|
|
|
|
{
|
|
|
|
|
client = new SftpClient(Account.Host, Account.Port, Account.Username, Account.Password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (client != null)
|
|
|
|
|
{
|
|
|
|
|
client.BufferSize = (uint)BufferSize;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (client != null && !client.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
client.Connect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return IsConnected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Disconnect()
|
|
|
|
|
{
|
|
|
|
|
if (client != null && client.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
client.Disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 05:10:05 +13:00
|
|
|
|
public void ChangeDirectory(string path, bool autoCreateDirectory = false)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (Connect())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.ChangeDirectory(path);
|
|
|
|
|
}
|
2017-03-24 05:10:05 +13:00
|
|
|
|
catch (SftpPathNotFoundException) when (autoCreateDirectory)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2017-03-24 05:10:05 +13:00
|
|
|
|
CreateDirectory(path, true);
|
2017-03-24 23:31:25 +13:00
|
|
|
|
ChangeDirectory(path);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 05:10:05 +13:00
|
|
|
|
public bool DirectoryExists(string path)
|
|
|
|
|
{
|
|
|
|
|
if (Connect())
|
|
|
|
|
{
|
|
|
|
|
return client.Exists(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreateDirectory(string path, bool createMultiDirectory = false)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (Connect())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.CreateDirectory(path);
|
2017-03-24 05:10:05 +13:00
|
|
|
|
|
|
|
|
|
DebugHelper.WriteLine($"SFTP directory created: {path}");
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2017-03-24 05:10:05 +13:00
|
|
|
|
catch (SftpPathNotFoundException) when (createMultiDirectory)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
CreateMultiDirectory(path);
|
|
|
|
|
}
|
|
|
|
|
catch (SftpPermissionDeniedException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<string> CreateMultiDirectory(string path)
|
|
|
|
|
{
|
|
|
|
|
List<string> directoryList = new List<string>();
|
|
|
|
|
|
2017-05-02 22:45:57 +12:00
|
|
|
|
List<string> paths = URLHelpers.GetPaths(path);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
foreach (string directory in paths)
|
|
|
|
|
{
|
|
|
|
|
if (!DirectoryExists(directory))
|
|
|
|
|
{
|
|
|
|
|
CreateDirectory(directory);
|
|
|
|
|
directoryList.Add(directory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return directoryList;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 05:10:05 +13:00
|
|
|
|
private bool UploadStream(Stream stream, string remotePath, bool autoCreateDirectory = false)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-06-25 09:41:55 +12:00
|
|
|
|
if (Connect())
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-06-25 09:41:55 +12:00
|
|
|
|
try
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-06-29 03:40:45 +12:00
|
|
|
|
using (SftpFileStream sftpStream = client.Create(remotePath))
|
2014-06-25 09:41:55 +12:00
|
|
|
|
{
|
|
|
|
|
return TransferData(stream, sftpStream);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2017-03-24 05:10:05 +13:00
|
|
|
|
catch (SftpPathNotFoundException) when (autoCreateDirectory)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-06-29 00:32:37 +12:00
|
|
|
|
// Happens when directory not exist, create directory and retry uploading
|
|
|
|
|
|
2017-03-24 05:10:05 +13:00
|
|
|
|
CreateDirectory(URLHelpers.GetDirectoryPath(remotePath), true);
|
2017-04-01 10:30:38 +13:00
|
|
|
|
return UploadStream(stream, remotePath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2015-06-29 00:32:37 +12:00
|
|
|
|
catch (NullReferenceException)
|
|
|
|
|
{
|
2017-03-24 23:31:25 +13:00
|
|
|
|
// Happens when disconnect while uploading
|
2015-06-29 00:32:37 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2014-06-25 09:41:55 +12:00
|
|
|
|
|
|
|
|
|
return false;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (client != null)
|
|
|
|
|
{
|
2014-06-25 09:26:00 +12:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-05 12:17:29 +13:00
|
|
|
|
}
|