Skip to content

Commit

Permalink
Additional dependency FluentFTP replaced by CLR's FTP client implemen…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
PurpleGray committed Aug 10, 2018
1 parent 3180479 commit 150fdb3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
4 changes: 0 additions & 4 deletions src/NAppUpdate.Framework/NAppUpdate.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="FluentFTP, Version=19.2.2.0, Culture=neutral, PublicKeyToken=f4af092b1d8df44f, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\FluentFTP.19.2.2\lib\net35\FluentFTP.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
Expand Down Expand Up @@ -128,7 +125,6 @@
<SubType>Designer</SubType>
</EmbeddedResource>
<None Include="NAppUpdate.snk" />
<None Include="packages.config" />
<None Include="Updater\updater.exe" />
</ItemGroup>
<ItemGroup>
Expand Down
84 changes: 68 additions & 16 deletions src/NAppUpdate.Framework/Sources/FtpSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,102 @@ public class FtpSource : IUpdateSource

private string feedBasePath => Path.GetDirectoryName(feedPath);

private FtpClient ftpClient { get; }
private int bufferSize = 2048;

private NetworkCredential credentials { get; }

/// <param name="hostUrl">Url of ftp server ("ftp://somesite.com/")</param>
/// <param name="feedPath">Local ftp path to feed ("/path/to/feed.xml")</param>
/// <param name="login">Login of ftp user, if needed</param>
/// <param name="password">Password of ftp user, if needed</param>
public FtpSource(string hostUrl, string feedPath, string login = null, string password = null)
{
ftpClient = new FtpClient(hostUrl);
HostUrl = hostUrl;
this.feedPath = feedPath;

if (login != null && password != null)
{
ftpClient.Credentials = new NetworkCredential(login, password);
credentials = new NetworkCredential(login, password);
}
}

private void TryConnectToHost()
{
var ftpConnRequest = (FtpWebRequest) FtpWebRequest.Create(HostUrl);
ftpConnRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpConnRequest.UsePassive = true;
ftpConnRequest.KeepAlive = false;
ftpConnRequest.Credentials = credentials;

try
{
ftpClient.Connect();
ftpConnRequest.GetResponse();
}
catch(Exception e)
catch (WebException e)
{
throw new WebException($"Failed to connect to host: {HostUrl}. Error message: {e.Message}");
}
}

/// <summary>
/// Downloads remote file from ftp
/// </summary>
/// <param name="path">Path to file on server (example: "/path/to/file.txt")</param>
/// <returns>Path to saved on disk file (Temp folder)</returns>
private string DownloadRemoteFile(string path, string localPath = null)
{
try
{
string pathToSave = localPath ?? Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
// Create an FTP Request
var ftpRequest =
(FtpWebRequest) FtpWebRequest.Create(
$@"{HostUrl}{(HostUrl.EndsWith("/") ? string.Empty : "/")}{path}");
ftpRequest.Credentials = credentials;
// Set options
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
// Specify the Type of FTP Request
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;

// Establish Return Communication with the FTP Server
using (var ftpResponse = (FtpWebResponse) ftpRequest.GetResponse())
{
// Get the FTP Server's Response Stream
using (var ftpStream = ftpResponse.GetResponseStream())
{
using (var localFileStream = new FileStream(pathToSave, FileMode.Create))
{
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);

while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
}
}

return pathToSave;
}
catch (Exception ex)
{
throw new WebException(
$"An error occurred when trying to download file {Path.GetFileName(path)}: {ex.Message}");
}
}

#region IUpdateSource Members

public String GetUpdatesFeed()
{
TryConnectToHost();

string data = null;

using (var fileStream = ftpClient.OpenRead(feedPath, FtpDataType.ASCII, true))
{
using (var streamReader = new StreamReader(fileStream))
{
data = streamReader.ReadToEnd();
}
}
string feedFilePath = DownloadRemoteFile(feedPath);
string data = File.ReadAllText(feedFilePath);

// Remove byteorder mark if necessary
int indexTagOpening = data.IndexOf('<');
Expand All @@ -72,9 +123,10 @@ public String GetUpdatesFeed()
return data;
}

public Boolean GetData(String filePath, String basePath, Action<UpdateProgressInfo> onProgress, ref String tempLocation)
public Boolean GetData(String filePath, String basePath, Action<UpdateProgressInfo> onProgress,
ref String tempLocation)
{
ftpClient.DownloadFile(tempLocation, Path.Combine(feedBasePath, filePath));
DownloadRemoteFile(Path.Combine(feedBasePath, filePath), tempLocation);
return true;
}

Expand Down
4 changes: 0 additions & 4 deletions src/NAppUpdate.Framework/packages.config

This file was deleted.

0 comments on commit 150fdb3

Please sign in to comment.