Skip to content

Commit

Permalink
Implemented FtpSource based on FluentFtp library
Browse files Browse the repository at this point in the history
  • Loading branch information
PurpleGray committed Aug 8, 2018
1 parent e00b096 commit 3180479
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/NAppUpdate.Framework/NAppUpdate.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@
<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" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -97,6 +101,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Sources\FtpSource.cs" />
<Compile Include="Sources\IUpdateSource.cs" />
<Compile Include="Sources\MemorySource.cs" />
<Compile Include="Sources\SimpleWebSource.cs" />
Expand All @@ -123,6 +128,7 @@
<SubType>Designer</SubType>
</EmbeddedResource>
<None Include="NAppUpdate.snk" />
<None Include="packages.config" />
<None Include="Updater\updater.exe" />
</ItemGroup>
<ItemGroup>
Expand Down
83 changes: 83 additions & 0 deletions src/NAppUpdate.Framework/Sources/FtpSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web.UI.WebControls;
using FluentFTP;
using NAppUpdate.Framework.Common;

namespace NAppUpdate.Framework.Sources
{
public class FtpSource : IUpdateSource
{
public string HostUrl { get; set; }

private string feedPath { get; }

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

private FtpClient ftpClient { 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);
}
}

private void TryConnectToHost()
{
try
{
ftpClient.Connect();
}
catch(Exception e)
{
throw new WebException($"Failed to connect to host: {HostUrl}. Error message: {e.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();
}
}

// Remove byteorder mark if necessary
int indexTagOpening = data.IndexOf('<');
if (indexTagOpening > 0)
{
data = data.Substring(indexTagOpening);
}

return data;
}

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

#endregion
}
}
4 changes: 4 additions & 0 deletions src/NAppUpdate.Framework/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentFTP" version="19.2.2" targetFramework="net35" />
</packages>

0 comments on commit 3180479

Please sign in to comment.