dimanche 2 avril 2017

ftp ,sftp or fileSystem operations adapter , bridge or strategy

I want to write a generic library for download or upload file operations.Files can be download or upload from ftp, sftp or network.I think adapter pattern fits for this solution but when I search on stack-overflow or internet different patterns advised. Some of them say that using adapter pattern is incorrect. As a result of search alternatives to adapters are bridge and strategy. Am i using wrong pattern for this solution or all of them possible?

My sample code using adapter pattern:

public interface IFileTransferAdapter
{
    void DownloadFile(string sourcePath, string targetPath);
}

 public class SftpAdapter : IFileTransferAdapter
{
    private readonly string _host;
    private readonly string _password;
    private readonly int _port;
    private readonly string _userName;

    /// <summary>
    ///     Sftp Adapter default constructor
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    /// <param name="host"></param>
    public SftpAdapter(string userName, string password, string host)
    {
        _userName = userName;
        _password = password;
        _port = 22;
        _host = host;
    }

    /// <summary>
    ///     Sftp adapter constructor
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    /// <param name="host"></param>
    /// <param name="port"></param>
    public SftpAdapter(string userName, string password, string host, int port)
    {
        _userName = userName;
        _password = password;
        _host = host;
        _port = port;
    }


    /// <summary>
    /// Download file from sftp
    /// </summary>
    /// <param name="sourcePath"></param>
    /// <param name="targetPath"></param>
    public void DownloadFile(string sourcePath, string targetPath)
    {
        using (var sftp = new SftpClient(_host, _port, _userName, _password))
        {
            sftp.Connect();

            using (var file = File.OpenWrite(targetPath))
            {
                sftp.DownloadFile(sourcePath, file);
            }

            sftp.Disconnect();
        }
    }
}

  public class FtpAdapter : IFileTransferAdapter
{
    private readonly string _userName;
    private readonly string _password;
    private readonly string _host;

    /// <summary>
    /// FtpAdapter constructor
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    /// <param name="host"></param>
    public FtpAdapter(string userName, string password, string host)
    {
        this._userName = userName;
        this._password = password;
        this._host = host;
    }


    /// <summary>
    /// Download file from ftp
    /// </summary>
    /// <param name="sourcePath"></param>
    /// <param name="targetPath"></param>
    public void DownloadFile(string sourcePath, string targetPath)
    {
        string formatedPath = $"http://ift.tt/2oxiFoi}";
        var request = (FtpWebRequest) WebRequest.Create(formatedPath);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential(this._userName, this._password);
        request.UseBinary = true;

        using (var response = (FtpWebResponse) request.GetResponse())
        {
            using (var ftpResponse = response.GetResponseStream())
            {
                using (var fileStream = new FileStream(targetPath, FileMode.Create))
                {
                    var buffer = new byte[2048];
                    var bytesRead = ftpResponse.Read(buffer, 0, buffer.Length);

                    while (bytesRead > 0)
                    {
                        fileStream.Write(buffer, 0, bytesRead);
                        bytesRead = ftpResponse.Read(buffer, 0, buffer.Length);
                    }
                }
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire