Files
FileProcessClient/utils/FileUploadUtils.cs
T
2026-09-27 18:52:16 +08:00

164 lines
6.1 KiB
C#

using edu.huvtc.utils;
using FileProcessClient.domain;
using FluentFTP;
using System.Diagnostics;
namespace FileProcessClient.utils
{
internal class FileUploadUtils
{
public static async Task UploadFileAsync(string localPath, string remoteFilePath, Action<int> callback)
{
var token = new CancellationToken();
using (var conn = new AsyncFtpClient(CommonUtils.getFtpServer(), CommonUtils.getFtpUsr(), CommonUtils.getFtpPassword(), CommonUtils.getFtpServerPort(), config: new FtpConfig
{
DataConnectionType = FtpDataConnectionType.PORT
}))
{
try
{
await conn.Connect(token);
Progress<FtpProgress> progress = new Progress<FtpProgress>(p =>
{
callback((int)(p.Progress));
//Trace.WriteLine(p.Progress);
});
await conn.UploadFile(localPath, remoteFilePath, FtpRemoteExists.Overwrite, false, FtpVerify.None, progress, token);
}
catch (Exception ex)
{
callback(-1);
//Trace.WriteLine(ex.ToString());
}
finally
{
await conn.Disconnect();
}
}
}
public static async Task<bool> UploadFilesAsync(List<MediaFileItem> list, string remoteRootPath, Action<int, int> callback)
{
var token = new CancellationToken();
using (var conn = new AsyncFtpClient(CommonUtils.getFtpServer(), CommonUtils.getFtpUsr(), CommonUtils.getFtpPassword(), CommonUtils.getFtpServerPort(), config: new FtpConfig
{
DataConnectionType = FtpDataConnectionType.PASV,
ConnectTimeout = 60000,
ReadTimeout = 60000
}))
{
int index = 0;
try
{
await conn.Connect(token);
//conn.AutoDetect(FtpDataConnectionType.PORT);
Progress<FtpProgress> progress = new Progress<FtpProgress>(p =>
{
//Trace.WriteLine(p.Progress);
callback(index, (int)(p.Progress));
});
bool pathExits = await conn.DirectoryExists(remoteRootPath + "/json", token);
if (!pathExits)
{
await conn.CreateDirectory(remoteRootPath + "/json", true, token);
}
pathExits = await conn.DirectoryExists(remoteRootPath + "/media", token);
if (!pathExits)
{
await conn.CreateDirectory(remoteRootPath + "/media", true, token);
}
foreach (MediaFileItem file in list)
{
//Trace.WriteLine(file.fileName);
//Trace.WriteLine(file.urlPath);
await conn.UploadFile(file.fileName, file.urlPath, FtpRemoteExists.Overwrite, false, FtpVerify.None, progress, token);
index++;
}
return true;
}
catch (Exception ex)
{
callback(index, -1);
Trace.WriteLine(ex.ToString());
//return false;
}
finally
{
await conn.Disconnect();
}
return false;
}
}
public static async Task<bool> UploadFilesAsync(List<string> localFiles, string remoteRootPath, Action<int, int> callback)
{
var token = new CancellationToken();
using (var conn = new AsyncFtpClient(CommonUtils.getFtpServer(), CommonUtils.getFtpUsr(), CommonUtils.getFtpPassword(), CommonUtils.getFtpServerPort(), config: new FtpConfig
{
DataConnectionType = FtpDataConnectionType.PORT
}))
{
int index = 0;
try
{
await conn.Connect(token);
Progress<FtpProgress> progress = new Progress<FtpProgress>(p =>
{
callback(index, (int)(p.Progress));
});
bool pathExits = await conn.DirectoryExists(remoteRootPath, token);
if (!pathExits)
{
await conn.CreateDirectory(remoteRootPath, true, token);
}
foreach (var file in localFiles)
{
//Trace.WriteLine(file);
//Trace.WriteLine(remoteRootPath + "/" + getFileName(file));
await conn.UploadFile(file, remoteRootPath + "/" + getFileName(file), FtpRemoteExists.Overwrite, false, FtpVerify.None, progress, token);
index++;
}
return true;
}
catch (Exception ex)
{
callback(index, -1);
return false;
//Trace.WriteLine(ex.ToString());
}
finally
{
await conn.Disconnect();
}
}
}
public static string getFileName(string fullPath)
{
string[] arr = fullPath.Split(Path.DirectorySeparatorChar);
return arr[arr.Length - 1];
}
public static string getFileExt(string fullPath)
{
string[] arr = fullPath.Split("\\.");
return arr[arr.Length - 1];
}
public static string getFileCreateDataTime(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.CreationTime.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}