ShareX/ShareX.UploadersLib/FileUploaders/SFTP.cs

257 lines
7.5 KiB
C#
Raw Normal View History

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
2023-01-10 09:31:02 +13:00
Copyright (c) 2007-2023 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;
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; }
public bool IsValidAccount => (!string.IsNullOrEmpty(Account.Keypath) && File.Exists(Account.Keypath)) || !string.IsNullOrEmpty(Account.Password);
2013-11-03 23:53:49 +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;
}
public override UploadResult Upload(Stream stream, string fileName)
{
UploadResult result = new UploadResult();
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:41:55 +12:00
try
{
IsUploading = true;
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();
2014-06-25 09:41:55 +12:00
IsUploading = false;
}
return result;
}
public override void StopUpload()
{
if (IsUploading && !StopUploadRequested)
{
StopUploadRequested = true;
try
{
Disconnect();
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
}
}
2013-11-03 23:53:49 +13:00
public bool Connect()
{
if (client == null)
{
if (!string.IsNullOrEmpty(Account.Keypath))
2013-11-03 23:53:49 +13:00
{
if (!File.Exists(Account.Keypath))
{
throw new FileNotFoundException(Resources.UploadersConfigForm_ConnectSFTPAccount_Key_file_not_found, Account.Keypath);
}
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();
}
}
public void ChangeDirectory(string path, bool autoCreateDirectory = false)
2013-11-03 23:53:49 +13:00
{
if (Connect())
{
try
{
client.ChangeDirectory(path);
}
catch (SftpPathNotFoundException) when (autoCreateDirectory)
2013-11-03 23:53:49 +13:00
{
CreateDirectory(path, true);
ChangeDirectory(path);
2013-11-03 23:53:49 +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);
DebugHelper.WriteLine($"SFTP directory created: {path}");
2013-11-03 23:53:49 +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>();
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;
}
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
{
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
}
catch (SftpPathNotFoundException) when (autoCreateDirectory)
2013-11-03 23:53:49 +13:00
{
// Happens when directory not exist, create directory and retry uploading
CreateDirectory(URLHelpers.GetDirectoryPath(remotePath), true);
return UploadStream(stream, remotePath);
2013-11-03 23:53:49 +13:00
}
catch (NullReferenceException)
{
// Happens when disconnect while uploading
}
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)
{
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
}