mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 17:56:51 +13:00
Fixed SFTP directory error when using absolute path
This commit is contained in:
parent
1733968f4e
commit
a991572ad3
1 changed files with 33 additions and 43 deletions
|
@ -30,7 +30,6 @@ You should have received a copy of the GNU General Public License
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace ShareX.UploadersLib.FileUploaders
|
namespace ShareX.UploadersLib.FileUploaders
|
||||||
{
|
{
|
||||||
|
@ -38,21 +37,9 @@ public sealed class SFTP : FileUploader, IDisposable
|
||||||
{
|
{
|
||||||
public FTPAccount Account { get; private set; }
|
public FTPAccount Account { get; private set; }
|
||||||
|
|
||||||
public bool IsValidAccount
|
public bool IsValidAccount => (!string.IsNullOrEmpty(Account.Keypath) && File.Exists(Account.Keypath)) || !string.IsNullOrEmpty(Account.Password);
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return (!string.IsNullOrEmpty(Account.Keypath) && File.Exists(Account.Keypath)) || !string.IsNullOrEmpty(Account.Password);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsConnected
|
public bool IsConnected => client != null && client.IsConnected;
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return client != null && client.IsConnected;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private SftpClient client;
|
private SftpClient client;
|
||||||
|
|
||||||
|
@ -74,7 +61,8 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
IsUploading = true;
|
IsUploading = true;
|
||||||
bool uploadResult = UploadStream(stream, path);
|
|
||||||
|
bool uploadResult = UploadStream(stream, path, true);
|
||||||
|
|
||||||
if (uploadResult && !StopUploadRequested && !IsError)
|
if (uploadResult && !StopUploadRequested && !IsError)
|
||||||
{
|
{
|
||||||
|
@ -84,6 +72,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
Dispose();
|
Dispose();
|
||||||
|
|
||||||
IsUploading = false;
|
IsUploading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,7 +147,7 @@ public void Disconnect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ChangeDirectory(string path)
|
public void ChangeDirectory(string path, bool autoCreateDirectory = false)
|
||||||
{
|
{
|
||||||
if (Connect())
|
if (Connect())
|
||||||
{
|
{
|
||||||
|
@ -166,28 +155,10 @@ public void ChangeDirectory(string path)
|
||||||
{
|
{
|
||||||
client.ChangeDirectory(path);
|
client.ChangeDirectory(path);
|
||||||
}
|
}
|
||||||
catch (SftpPathNotFoundException)
|
catch (SftpPathNotFoundException) when (autoCreateDirectory)
|
||||||
{
|
|
||||||
CreateDirectory(path);
|
|
||||||
ChangeDirectory(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CreateDirectory(string path)
|
|
||||||
{
|
|
||||||
if (Connect())
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
client.CreateDirectory(path);
|
|
||||||
}
|
|
||||||
catch (SftpPathNotFoundException)
|
|
||||||
{
|
|
||||||
CreateMultiDirectory(path);
|
|
||||||
}
|
|
||||||
catch (SftpPermissionDeniedException)
|
|
||||||
{
|
{
|
||||||
|
CreateDirectory(path, true);
|
||||||
|
client.ChangeDirectory(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -202,18 +173,37 @@ public bool DirectoryExists(string path)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void CreateDirectory(string path, bool createMultiDirectory = false)
|
||||||
|
{
|
||||||
|
if (Connect())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.CreateDirectory(path);
|
||||||
|
|
||||||
|
DebugHelper.WriteLine($"SFTP directory created: {path}");
|
||||||
|
}
|
||||||
|
catch (SftpPathNotFoundException) when (createMultiDirectory)
|
||||||
|
{
|
||||||
|
CreateMultiDirectory(path);
|
||||||
|
}
|
||||||
|
catch (SftpPermissionDeniedException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public List<string> CreateMultiDirectory(string path)
|
public List<string> CreateMultiDirectory(string path)
|
||||||
{
|
{
|
||||||
List<string> directoryList = new List<string>();
|
List<string> directoryList = new List<string>();
|
||||||
|
|
||||||
IEnumerable<string> paths = URLHelpers.GetPaths(path).Select(x => x.TrimStart('/'));
|
IEnumerable<string> paths = URLHelpers.GetPaths(path);
|
||||||
|
|
||||||
foreach (string directory in paths)
|
foreach (string directory in paths)
|
||||||
{
|
{
|
||||||
if (!DirectoryExists(directory))
|
if (!DirectoryExists(directory))
|
||||||
{
|
{
|
||||||
CreateDirectory(directory);
|
CreateDirectory(directory);
|
||||||
DebugHelper.WriteLine("FTP directory created: " + path);
|
|
||||||
directoryList.Add(directory);
|
directoryList.Add(directory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -221,7 +211,7 @@ public List<string> CreateMultiDirectory(string path)
|
||||||
return directoryList;
|
return directoryList;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool UploadStream(Stream stream, string remotePath)
|
private bool UploadStream(Stream stream, string remotePath, bool autoCreateDirectory = false)
|
||||||
{
|
{
|
||||||
if (Connect())
|
if (Connect())
|
||||||
{
|
{
|
||||||
|
@ -232,11 +222,11 @@ private bool UploadStream(Stream stream, string remotePath)
|
||||||
return TransferData(stream, sftpStream);
|
return TransferData(stream, sftpStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (SftpPathNotFoundException)
|
catch (SftpPathNotFoundException) when (autoCreateDirectory)
|
||||||
{
|
{
|
||||||
// Happens when directory not exist, create directory and retry uploading
|
// Happens when directory not exist, create directory and retry uploading
|
||||||
|
|
||||||
CreateDirectory(URLHelpers.GetDirectoryPath(remotePath));
|
CreateDirectory(URLHelpers.GetDirectoryPath(remotePath), true);
|
||||||
|
|
||||||
using (SftpFileStream sftpStream = client.Create(remotePath))
|
using (SftpFileStream sftpStream = client.Create(remotePath))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue