Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#region FTPFileExchange
[System.Net.WebPermission(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]
public class FTPFileExchange : BaseFileExchange, IFileExchange
{
public String Name { get { return Messages.ExchangeFTP; } }
public String Code { get { return Codes.ExchangeFTPCode; } }
public FTPFileExchange(String login, String password)
: base(login, password)
{ }
private FtpWebRequest GetFTPRequest(String path, String method, Boolean isFolder)
{
// Create FtpWebRequest object from the Uri provided
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(GetFileUri(path, isFolder));
if (_creds != null) request.Credentials = _creds;
request.KeepAlive = false;
request.UseBinary = true;
// Specify the command to be executed.
request.Method = method;
return request;
}
private Uri GetFileUri(String path, Boolean isFolder)
{
StringBuilder sb = new StringBuilder(path);
sb = sb.Replace("\\", "/");
if (!path.StartsWithCollation("ftp")) sb = sb.Insert(0, "ftp://");
if (isFolder && !path.EndsWithCollation("/")) sb = sb.Append("/");
return new Uri(sb.ToString());
}
public override ExternalFileInfo GetInfo(String path)
{
ExternalFileInfo result = new ExternalFileInfo(System.IO.Path.GetFileName(path));
result.FullName = path;
result.Date = GetTimeStamp(result.FullName);
return result;
}
public override IEnumerable<ExternalFileInfo> ListFiles(String path)
{
List<ExternalFileInfo> files = new List<ExternalFileInfo>();
FtpWebRequest request = GetFTPRequest(path, WebRequestMethods.Ftp.ListDirectoryDetails, true);
using (WebResponse response = request.GetResponse())
{
String line = null;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
while ((line = reader.ReadLine()) != null)
{
ExternalFileInfo result = RegExpHelper.ParseFtpLine(line);
if (result == null) continue;
result.FullName = new Uri(path.EndsWith("/") ? new Uri(path) : new Uri(path + "/"), result.Name).ToString();
result.Date = GetTimeStamp(result.FullName);
files.Add(result);
}
}
}
return files;
}
public override Stream UploadStream(String path)
{
FtpWebRequest request = GetFTPRequest(path, WebRequestMethods.Ftp.UploadFile, false);
Stream stream = request.GetRequestStream();
return stream;
}
public override Stream DownloadStream(String path)
{
FtpWebRequest request = GetFTPRequest(path, WebRequestMethods.Ftp.DownloadFile, false);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
return stream;
}
private DateTime GetTimeStamp(String path)
{
FtpWebRequest request = GetFTPRequest(path, WebRequestMethods.Ftp.GetDateTimestamp, false);
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
string dateString = response.StatusDescription.Substring(4,14);
return DateTime.ParseExact(dateString, "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);
}
}
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.