|
1 | | -using System.IO; |
| 1 | +using System; |
| 2 | +using System.IO; |
2 | 3 | using System.Net; |
3 | 4 | using System.Text; |
| 5 | +using Renci.SshNet; |
4 | 6 |
|
5 | 7 | namespace Spedit.Utils |
6 | 8 | { |
7 | 9 | public class FTP |
8 | 10 | { |
9 | | - private string host = null; |
10 | | - private string user = null; |
11 | | - private string pass = null; |
12 | | - private FtpWebRequest ftpRequest = null; |
13 | | - private Stream ftpStream = null; |
14 | | - private int bufferSize = 2048; |
| 11 | + private readonly string _host; |
| 12 | + private readonly string _user; |
| 13 | + private readonly string _pass; |
15 | 14 |
|
16 | | - public FTP(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; } |
| 15 | + public FTP(string host, string user, string password) { _host = host; _user = user; _pass = password; } |
17 | 16 |
|
18 | | - //thanks to: http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class |
19 | | - public void upload(string remoteFile, string localFile) |
| 17 | + public void Upload(string remoteFile, string localFile) |
20 | 18 | { |
21 | | - StringBuilder requestUri = new StringBuilder(host); |
22 | | - if (host[host.Length - 1] == '/') |
23 | | - { |
24 | | - if (remoteFile[0] == '/') |
25 | | - { requestUri.Append(remoteFile.Substring(1)); } |
26 | | - else |
27 | | - { requestUri.Append(remoteFile); } |
28 | | - } |
29 | | - else |
30 | | - { |
31 | | - if (remoteFile[0] == '/') |
32 | | - { requestUri.Append(remoteFile); } |
33 | | - else |
34 | | - { |
35 | | - requestUri.Append("/"); |
36 | | - requestUri.Append(remoteFile); |
37 | | - } |
38 | | - } |
39 | | - ftpRequest = (FtpWebRequest)FtpWebRequest.Create(requestUri.ToString()); |
40 | | - ftpRequest.Credentials = new NetworkCredential(user, pass); |
41 | | - ftpRequest.UseBinary = true; |
42 | | - ftpRequest.UsePassive = true; |
43 | | - ftpRequest.KeepAlive = true; |
44 | | - ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; |
45 | | - ftpStream = ftpRequest.GetRequestStream(); |
46 | | - FileStream localFileStream = new FileStream(localFile, FileMode.Open); |
47 | | - byte[] byteBuffer = new byte[bufferSize]; |
48 | | - int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize); |
49 | | - while (bytesSent != 0) |
| 19 | + var requestUri = new UriBuilder(_host) { Path = remoteFile }.Uri; |
| 20 | + |
| 21 | + if (requestUri.Scheme == "sftp") |
| 22 | + { |
| 23 | + var connectionInfo = new ConnectionInfo(requestUri.Host, requestUri.Port == -1 ? 22 : requestUri.Port, _user, new PasswordAuthenticationMethod(_user, _pass)); |
| 24 | + using (var sftp = new SftpClient(connectionInfo)) |
| 25 | + { |
| 26 | + sftp.Connect(); |
| 27 | + |
| 28 | + using (var stream = File.OpenRead(localFile)) |
| 29 | + { |
| 30 | + sftp.UploadFile(stream, remoteFile, true); |
| 31 | + } |
| 32 | + |
| 33 | + sftp.Disconnect(); |
| 34 | + } |
| 35 | + } |
| 36 | + else |
50 | 37 | { |
51 | | - ftpStream.Write(byteBuffer, 0, bytesSent); |
52 | | - bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize); |
| 38 | + using (var client = new WebClient()) |
| 39 | + { |
| 40 | + client.Credentials = new NetworkCredential(_user, _pass); |
| 41 | + client.UploadFile(requestUri, WebRequestMethods.Ftp.UploadFile, localFile); |
| 42 | + } |
53 | 43 | } |
54 | | - localFileStream.Close(); |
55 | | - ftpStream.Close(); |
56 | | - ftpRequest = null; |
57 | 44 | } |
58 | | - } |
| 45 | + } |
59 | 46 | } |
0 commit comments