forked from leetreveil/.NET-Auto-Update
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented FtpSource based on FluentFtp library
- Loading branch information
1 parent
e00b096
commit 3180479
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |