-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2、增加上传到FTP。 3、修改例子调用格式。
- Loading branch information
Showing
7 changed files
with
321 additions
and
26 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
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,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Backup2Cloud.Conf | ||
{ | ||
/// <summary> | ||
/// FTP基本配置。 | ||
/// </summary> | ||
public abstract class BaseFtpConf | ||
{ | ||
/// <summary> | ||
/// 服务器主机名。 | ||
/// 默认为本机。 | ||
/// </summary> | ||
public string host { get; set; } = "localhost"; | ||
|
||
/// <summary> | ||
/// 服务器的FTP端口。 | ||
/// 默认为21。 | ||
/// </summary> | ||
public int port { get; set; } = 21; | ||
|
||
/// <summary> | ||
/// 用户名。 | ||
/// 默认为anonymous。 | ||
/// </summary> | ||
public string user { get; set; } = "anonymous"; | ||
|
||
/// <summary> | ||
/// 密码。 | ||
/// </summary> | ||
public string password { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// 是否使用匿名登陆。 | ||
/// 默认为false。 | ||
/// </summary> | ||
public bool anonymous { get; set; } = false; | ||
|
||
/// <summary> | ||
/// 在ftp上的路径。 | ||
/// </summary> | ||
public string path { get; set; } = "/"; | ||
} | ||
} |
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
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,117 @@ | ||
using Backup2Cloud.Conf; | ||
using Backup2Cloud.Logging; | ||
using FluentFTP; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net; | ||
using System.Text; | ||
|
||
namespace Backup2Cloud.DataSource | ||
{ | ||
/// <summary> | ||
/// FTP数据源。从远端FTP获取数据。 | ||
/// </summary> | ||
[Name("ftp")] | ||
public class FtpDataSource : BaseFtpConf, IDataSource | ||
{ | ||
public string Tips | ||
{ | ||
get | ||
{ | ||
return string.Empty; | ||
} | ||
} | ||
|
||
public object GetExample() | ||
{ | ||
return new FtpDataSource(); | ||
} | ||
|
||
public void SaveData(string des) | ||
{ | ||
using (FtpClient client = new FtpClient(host)) | ||
{ | ||
client.Port = port; | ||
client.Encoding = Encoding.UTF8; | ||
if (anonymous == false) | ||
{ | ||
client.Credentials = new NetworkCredential(user, password); | ||
} | ||
|
||
client.Connect(); | ||
if (client.DirectoryExists(path)) | ||
{ | ||
int result = DownloadDirectory(path, "/", des, client); | ||
Log.Print($"从 ftp://{ host }:{ port }{ path } 成功下载 { result } 个文件到 { des } 。"); | ||
} | ||
else | ||
{ | ||
if (client.DownloadFile(des, path) == false) | ||
{ | ||
throw new IOException($"下载 ftp://{ host }:{ port }{ path } 失败。"); | ||
} | ||
|
||
Log.Print($"成功下载 ftp://{ host }:{ port }{ path } 到 { des } 。"); | ||
} | ||
|
||
client.Disconnect(); | ||
} | ||
} | ||
|
||
private int DownloadDirectory(string baseSrc, string dir, string des, FtpClient client) | ||
{ | ||
string target = FormatDirectoryName(des + dir); | ||
if (Directory.Exists(target)) | ||
{ | ||
Directory.Delete(target, true); | ||
} | ||
|
||
Directory.CreateDirectory(target); | ||
var list = client.GetListing(FormatDirectoryName(baseSrc + dir)); | ||
List<string> dirs = new List<string>(); | ||
int result = 0; | ||
foreach (var i in list) | ||
{ | ||
switch (i.Type) | ||
{ | ||
case FtpFileSystemObjectType.File: | ||
if (client.DownloadFile(target + i.Name, i.FullName)) | ||
{ | ||
++result; | ||
} | ||
|
||
break; | ||
|
||
case FtpFileSystemObjectType.Directory: | ||
dirs.Add(i.Name); | ||
break; | ||
|
||
case FtpFileSystemObjectType.Link: | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
|
||
foreach (var i in dirs) | ||
{ | ||
result += DownloadDirectory(baseSrc, dir + i + "/", des + i + "/", client); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private string FormatDirectoryName(string name) | ||
{ | ||
name = name.Replace("\\", "/"); | ||
name = name.Replace("//", "/"); | ||
if (name.EndsWith("/") == false) | ||
{ | ||
name += "/"; | ||
} | ||
|
||
return name; | ||
} | ||
} | ||
} |
Oops, something went wrong.