00001 using System;
00002 using Tamir.SharpSsh.jsch;
00003 using System.Collections;
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 namespace Tamir.SharpSsh
00037 {
00038 public class Sftp : SshTransferProtocolBase
00039 {
00040 private MyProgressMonitor m_monitor;
00041 private bool cancelled = false;
00042
00043 public Sftp(string sftpHost, string user, string password)
00044 : base(sftpHost, user, password)
00045 {
00046 Init();
00047 }
00048
00049 public Sftp(string sftpHost, string user)
00050 : base(sftpHost, user)
00051 {
00052 Init();
00053 }
00054
00055 private void Init()
00056 {
00057 m_monitor = new MyProgressMonitor(this);
00058 }
00059
00060 protected override string ChannelType
00061 {
00062 get { return "sftp"; }
00063 }
00064
00065 private ChannelSftp SftpChannel
00066 {
00067 get { return (ChannelSftp)m_channel; }
00068 }
00069
00070 public override void Cancel()
00071 {
00072 cancelled = true;
00073 }
00074
00075
00076
00077 public void Get(string fromFilePath)
00078 {
00079 Get(fromFilePath, ".");
00080 }
00081
00082 public void Get(string[] fromFilePaths)
00083 {
00084 for (int i = 0; i < fromFilePaths.Length; i++)
00085 {
00086 Get(fromFilePaths[i]);
00087 }
00088 }
00089
00090 public void Get(string[] fromFilePaths, string toDirPath)
00091 {
00092 for (int i = 0; i < fromFilePaths.Length; i++)
00093 {
00094 Get(fromFilePaths[i], toDirPath);
00095 }
00096 }
00097
00098 public override void Get(string fromFilePath, string toFilePath)
00099 {
00100 cancelled=false;
00101 SftpChannel.get(fromFilePath, toFilePath, m_monitor, ChannelSftp.OVERWRITE);
00102 }
00103
00104
00105
00106 public void Put(string fromFilePath)
00107 {
00108 Put(fromFilePath, ".");
00109 }
00110
00111 public void Put(string[] fromFilePaths)
00112 {
00113 for (int i = 0; i < fromFilePaths.Length; i++)
00114 {
00115 Put(fromFilePaths[i]);
00116 }
00117 }
00118
00119 public void Put(string[] fromFilePaths, string toDirPath)
00120 {
00121 for (int i = 0; i < fromFilePaths.Length; i++)
00122 {
00123 Put(fromFilePaths[i], toDirPath);
00124 }
00125 }
00126
00127 public override void Put(string fromFilePath, string toFilePath)
00128 {
00129 cancelled=false;
00130 SftpChannel.put(fromFilePath, toFilePath, m_monitor, ChannelSftp.OVERWRITE);
00131 }
00132
00133
00134
00135 public override void Mkdir(string directory)
00136 {
00137 SftpChannel.mkdir(directory);
00138 }
00139
00140
00141
00142 public ArrayList GetFileList(string path)
00143 {
00144 ArrayList list = new ArrayList();
00145 foreach(Tamir.SharpSsh.jsch.ChannelSftp.LsEntry entry in SftpChannel.ls(path))
00146 {
00147 list.Add(entry.getFilename().ToString());
00148 }
00149 return list;
00150 }
00151
00152 #region ProgressMonitor Implementation
00153
00154 private class MyProgressMonitor : SftpProgressMonitor
00155 {
00156 private long transferred = 0;
00157 private long total = 0;
00158 private int elapsed = -1;
00159 private Sftp m_sftp;
00160 private string src;
00161 private string dest;
00162
00163 System.Timers.Timer timer;
00164
00165 public MyProgressMonitor(Sftp sftp)
00166 {
00167 m_sftp = sftp;
00168 }
00169
00170 public override void init(int op, String src, String dest, long max)
00171 {
00172 this.src=src;
00173 this.dest=dest;
00174 this.elapsed = 0;
00175 this.total = max;
00176 timer = new System.Timers.Timer(1000);
00177 timer.Start();
00178 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
00179
00180 string note;
00181 if (op.Equals(GET))
00182 {
00183 note = "Downloading " + System.IO.Path.GetFileName( src ) + "...";
00184 }
00185 else
00186 {
00187 note = "Uploading " + System.IO.Path.GetFileName( src ) + "...";
00188 }
00189 m_sftp.SendStartMessage(src, dest, (int)total, note);
00190 }
00191 public override bool count(long c)
00192 {
00193 this.transferred += c;
00194 string note = ("Transfering... [Elapsed time: " + elapsed + "]");
00195 m_sftp.SendProgressMessage(src, dest, (int)transferred, (int)total, note);
00196 return !m_sftp.cancelled;
00197 }
00198 public override void end()
00199 {
00200 timer.Stop();
00201 timer.Dispose();
00202 string note = ("Done in " + elapsed + " seconds!");
00203 m_sftp.SendEndMessage(src, dest, (int)transferred, (int)total, note);
00204 transferred = 0;
00205 total = 0;
00206 elapsed = -1;
00207 src=null;
00208 dest=null;
00209 }
00210
00211 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
00212 {
00213 this.elapsed++;
00214 }
00215 }
00216
00217 #endregion ProgressMonitor Implementation
00218 }
00219 }