Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 49abd6f

Browse files
committed
Rewrite FTP util and add SFTP support
1 parent 1ea9c90 commit 49abd6f

File tree

4 files changed

+47
-63
lines changed

4 files changed

+47
-63
lines changed

Spedit.csproj

+14-10
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
<None Include="Deploy\DotNetChecker.nsh" />
6565
<None Include="Deploy\FileAssociation.nsh" />
6666
<None Include="Deploy\SPEdit.nsi" />
67-
<None Include="packages.config" />
6867
<None Include="Resources\Misc\Templates\CSSCSGOTemplate.sp" />
6968
<None Include="Resources\Misc\Templates\EmptyScriptTemplate.sp" />
7069
<None Include="Resources\Misc\Templates\OldSyntaxCSSCSGOTemplate.sp" />
@@ -75,15 +74,6 @@
7574
<None Include="Resources\Misc\Templates\TFTemplate.sp" />
7675
</ItemGroup>
7776
<ItemGroup>
78-
<Reference Include="ControlzEx, Version=3.0.2.4, Culture=neutral, processorArchitecture=MSIL">
79-
<HintPath>packages\ControlzEx.3.0.2.4\lib\net45\ControlzEx.dll</HintPath>
80-
</Reference>
81-
<Reference Include="ICSharpCode.AvalonEdit, Version=5.0.3.0, Culture=neutral, PublicKeyToken=9cc39be672370310, processorArchitecture=MSIL">
82-
<HintPath>packages\AvalonEdit.5.0.4\lib\Net40\ICSharpCode.AvalonEdit.dll</HintPath>
83-
</Reference>
84-
<Reference Include="MahApps.Metro, Version=1.6.5.1, Culture=neutral, processorArchitecture=MSIL">
85-
<HintPath>packages\MahApps.Metro.1.6.5\lib\net45\MahApps.Metro.dll</HintPath>
86-
</Reference>
8777
<Reference Include="PresentationCore" />
8878
<Reference Include="PresentationFramework" />
8979
<Reference Include="QueryMaster">
@@ -649,6 +639,20 @@
649639
<Content Include="Resources\lang_0_spedit.xml" />
650640
<Content Include="Resources\lang_0_spedit_entryTemplate.xml" />
651641
</ItemGroup>
642+
<ItemGroup>
643+
<PackageReference Include="AvalonEdit">
644+
<Version>6.0.1</Version>
645+
</PackageReference>
646+
<PackageReference Include="Extended.Wpf.Toolkit">
647+
<Version>3.8.0</Version>
648+
</PackageReference>
649+
<PackageReference Include="MahApps.Metro">
650+
<Version>1.6.5</Version>
651+
</PackageReference>
652+
<PackageReference Include="SSH.NET">
653+
<Version>2016.1.0</Version>
654+
</PackageReference>
655+
</ItemGroup>
652656
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
653657
<PropertyGroup>
654658
<PreBuildEvent>

UI/MainWindowSPCompiler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public void FTPUpload_Plugins()
299299
}
300300
try
301301
{
302-
ftp.upload(uploadDir, nonUploadedFiles[i]);
302+
ftp.Upload(uploadDir, nonUploadedFiles[i]);
303303
stringOutput.AppendLine($"{Program.Translations.Uploaded}: " + nonUploadedFiles[i]);
304304
}
305305
catch (Exception e)

Utils/FTP.cs

+32-45
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,46 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Net;
34
using System.Text;
5+
using Renci.SshNet;
46

57
namespace Spedit.Utils
68
{
79
public class FTP
810
{
9-
private string host = null;
10-
private string user = null;
11-
private string pass = null;
12-
private FtpWebRequest ftpRequest = null;
13-
private Stream ftpStream = null;
14-
private int bufferSize = 2048;
11+
private readonly string _host;
12+
private readonly string _user;
13+
private readonly string _pass;
1514

16-
public FTP(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }
15+
public FTP(string host, string user, string password) { _host = host; _user = user; _pass = password; }
1716

18-
//thanks to: http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class
19-
public void upload(string remoteFile, string localFile)
17+
public void Upload(string remoteFile, string localFile)
2018
{
21-
StringBuilder requestUri = new StringBuilder(host);
22-
if (host[host.Length - 1] == '/')
23-
{
24-
if (remoteFile[0] == '/')
25-
{ requestUri.Append(remoteFile.Substring(1)); }
26-
else
27-
{ requestUri.Append(remoteFile); }
28-
}
29-
else
30-
{
31-
if (remoteFile[0] == '/')
32-
{ requestUri.Append(remoteFile); }
33-
else
34-
{
35-
requestUri.Append("/");
36-
requestUri.Append(remoteFile);
37-
}
38-
}
39-
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(requestUri.ToString());
40-
ftpRequest.Credentials = new NetworkCredential(user, pass);
41-
ftpRequest.UseBinary = true;
42-
ftpRequest.UsePassive = true;
43-
ftpRequest.KeepAlive = true;
44-
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
45-
ftpStream = ftpRequest.GetRequestStream();
46-
FileStream localFileStream = new FileStream(localFile, FileMode.Open);
47-
byte[] byteBuffer = new byte[bufferSize];
48-
int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
49-
while (bytesSent != 0)
19+
var requestUri = new UriBuilder(_host) { Path = remoteFile }.Uri;
20+
21+
if (requestUri.Scheme == "sftp")
22+
{
23+
var connectionInfo = new ConnectionInfo(requestUri.Host, requestUri.Port == -1 ? 22 : requestUri.Port, _user, new PasswordAuthenticationMethod(_user, _pass));
24+
using (var sftp = new SftpClient(connectionInfo))
25+
{
26+
sftp.Connect();
27+
28+
using (var stream = File.OpenRead(localFile))
29+
{
30+
sftp.UploadFile(stream, remoteFile, true);
31+
}
32+
33+
sftp.Disconnect();
34+
}
35+
}
36+
else
5037
{
51-
ftpStream.Write(byteBuffer, 0, bytesSent);
52-
bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
38+
using (var client = new WebClient())
39+
{
40+
client.Credentials = new NetworkCredential(_user, _pass);
41+
client.UploadFile(requestUri, WebRequestMethods.Ftp.UploadFile, localFile);
42+
}
5343
}
54-
localFileStream.Close();
55-
ftpStream.Close();
56-
ftpRequest = null;
5744
}
58-
}
45+
}
5946
}

packages.config

-7
This file was deleted.

0 commit comments

Comments
 (0)