From 31804794862a9bb73f40332e861fc2aae408083b Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 8 Aug 2018 13:09:12 +0300 Subject: [PATCH] Implemented FtpSource based on FluentFtp library --- .../NAppUpdate.Framework.csproj | 6 ++ src/NAppUpdate.Framework/Sources/FtpSource.cs | 83 +++++++++++++++++++ src/NAppUpdate.Framework/packages.config | 4 + 3 files changed, 93 insertions(+) create mode 100644 src/NAppUpdate.Framework/Sources/FtpSource.cs create mode 100644 src/NAppUpdate.Framework/packages.config diff --git a/src/NAppUpdate.Framework/NAppUpdate.Framework.csproj b/src/NAppUpdate.Framework/NAppUpdate.Framework.csproj index 2fb2acd1..32e5cf34 100644 --- a/src/NAppUpdate.Framework/NAppUpdate.Framework.csproj +++ b/src/NAppUpdate.Framework/NAppUpdate.Framework.csproj @@ -68,7 +68,11 @@ v4.0 + + ..\..\..\packages\FluentFTP.19.2.2\lib\net35\FluentFTP.dll + + @@ -97,6 +101,7 @@ True Resources.resx + @@ -123,6 +128,7 @@ Designer + diff --git a/src/NAppUpdate.Framework/Sources/FtpSource.cs b/src/NAppUpdate.Framework/Sources/FtpSource.cs new file mode 100644 index 00000000..312d7d5d --- /dev/null +++ b/src/NAppUpdate.Framework/Sources/FtpSource.cs @@ -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; } + + /// Url of ftp server ("ftp://somesite.com/") + /// Local ftp path to feed ("/path/to/feed.xml") + /// Login of ftp user, if needed + /// Password of ftp user, if needed + 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 onProgress, ref String tempLocation) + { + ftpClient.DownloadFile(tempLocation, Path.Combine(feedBasePath, filePath)); + return true; + } + + #endregion + } +} diff --git a/src/NAppUpdate.Framework/packages.config b/src/NAppUpdate.Framework/packages.config new file mode 100644 index 00000000..53dd6e94 --- /dev/null +++ b/src/NAppUpdate.Framework/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file