This commit is contained in:
辉鸭蛋
2025-01-09 22:22:22 +08:00
parent c25c5bef8a
commit 2ea810f0a1
3 changed files with 88 additions and 31 deletions

View File

@@ -35,6 +35,9 @@ public partial class KeyMouseScriptItem : ObservableObject
private readonly string _scriptPath = Global.Absolute(@"User\KeyMouseScript");
private readonly ILogger _logger = TaskControl.Logger;
[ObservableProperty]
private bool _isUploadSuccess;
[RelayCommand]
private async Task Upload()
{
@@ -42,10 +45,11 @@ public partial class KeyMouseScriptItem : ObservableObject
{
_uploadCts = new CancellationTokenSource();
IsUploading = true;
IsUploadSuccess = false;
UploadProgress = 0;
var dirName = new DirectoryInfo(Path).Name;
_logger.LogInformation($"{dirName} 开始上传...");
_logger.LogDebug($"{dirName} 开始上传...");
var userName = TaskContext.Instance().Config.CommonConfig.UserName;
var uid = TaskContext.Instance().Config.CommonConfig.Uid;
@@ -57,6 +61,14 @@ public partial class KeyMouseScriptItem : ObservableObject
var tosClient = new TosClientHelper();
var files = Directory.GetFiles(Path, "*.*", SearchOption.AllDirectories);
// 计算所有文件的总大小
long totalSize = 0;
long uploadedSize = 0;
foreach (var file in files)
{
totalSize += new FileInfo(file).Length;
}
foreach (var file in files)
{
if (_uploadCts.Token.IsCancellationRequested)
@@ -66,43 +78,57 @@ public partial class KeyMouseScriptItem : ObservableObject
var needUploadFileName = System.IO.Path.GetFileName(file);
var remotePath = $"{dirName[..10]}_{userName}_{uid}/{relativePath}";
remotePath = remotePath.Replace(@"\", "/");
var fileSize = new FileInfo(file).Length;
if (needUploadFileName == "video.mkv" || needUploadFileName == "video.mp4")
{
tosClient.UploadLargeFile(file, remotePath, 20 * 1024 * 1024, (bytes, totalBytes, percentage) =>
{
UploadProgress = percentage;
_logger.LogInformation($"上传进度: {percentage:F}%");
var currentFileProgress = bytes;
var overallProgress = ((double)(uploadedSize + currentFileProgress) / totalSize) * 100;
UploadProgress = Math.Min(overallProgress, 99.9); // 保留最后0.1%给上传完成时
_logger.LogDebug($"上传进度: {overallProgress:F}%");
});
}
else
{
tosClient.UploadFile(file, remotePath);
}
uploadedSize += fileSize;
var progress = ((double)uploadedSize / totalSize) * 100;
UploadProgress = Math.Min(progress, 99.9);
}
UploadProgress = 100;
_logger.LogInformation($"{dirName} 上传完成");
IsUploadSuccess = true;
_logger.LogDebug($"{dirName} 上传完成");
}
catch (Exception ex)
{
_logger.LogError($"上传过程中发生错误:{ex.Message}");
IsUploadSuccess = false;
throw;
}
}, _uploadCts.Token);
}
catch (OperationCanceledException)
{
_logger.LogInformation("上传已取消");
_logger.LogDebug("上传已取消");
IsUploadSuccess = false;
}
catch (Exception ex)
{
_logger.LogError(ex, "上传出错");
IsUploadSuccess = false;
}
finally
{
IsUploading = false;
UploadProgress = 0;
if (!IsUploadSuccess)
{
UploadProgress = 0;
}
_uploadCts?.Dispose();
_uploadCts = null;
}