diff --git a/BetterGenshinImpact/Helpers/Upload/TosClientHelper.cs b/BetterGenshinImpact/Helpers/Upload/TosClientHelper.cs index 2655063e..06873f42 100644 --- a/BetterGenshinImpact/Helpers/Upload/TosClientHelper.cs +++ b/BetterGenshinImpact/Helpers/Upload/TosClientHelper.cs @@ -94,6 +94,15 @@ public class TosClientHelper { objectKey ??= Path.GetFileName(localFileName); + // 检查是否已经上传成功 + // var collection = _dbService.UserDb.GetCollection("FileUploads"); + // var existingItem = collection.FindById(objectKey); + // if (existingItem?.Status == UploadStatus.UploadSuccess.ToString()) + // { + // Debug.WriteLine($"File {objectKey} already uploaded successfully"); + // return; + // } + var fileUploadItem = new FileUploadItem { Id = objectKey, @@ -101,7 +110,7 @@ public class TosClientHelper ObjectKey = objectKey, Status = UploadStatus.Uploading.ToString() }; - + _dbService.Upsert("FileUploads", fileUploadItem); @@ -152,6 +161,17 @@ public class TosClientHelper } objectKey ??= Path.GetFileName(localFileName); + + // 检查是否已经上传成功 + // var collection = _dbService.UserDb.GetCollection("FileUploads"); + // var existingItem = collection.FindById(objectKey); + // if (existingItem?.Status == UploadStatus.UploadSuccess.ToString()) + // { + // Debug.WriteLine($"File {objectKey} already uploaded successfully"); + // progressCallback?.Invoke(100, 100, 100); + // return; + // } + string uploadID = null; var fileUploadItem = new FileUploadItem @@ -326,6 +346,16 @@ public class TosClientHelper /// 分片大小 public void ResumableUpload(string objectKey, string uploadID, string localFileName, long partSize = 20 * 1024 * 1024, UploadProgressCallback? progressCallback = null) { + // 检查是否已经上传成功 + // var collection = _dbService.UserDb.GetCollection("FileUploads"); + // var existingItem = collection.FindById(objectKey); + // if (existingItem?.Status == UploadStatus.UploadSuccess.ToString()) + // { + // Debug.WriteLine($"File {objectKey} already uploaded successfully"); + // progressCallback?.Invoke(100, 100, 100); + // return; + // } + var fileUploadItem = new FileUploadItem { Id = objectKey, diff --git a/BetterGenshinImpact/Model/KeyMouseScriptItem.cs b/BetterGenshinImpact/Model/KeyMouseScriptItem.cs index 61290426..4955913d 100644 --- a/BetterGenshinImpact/Model/KeyMouseScriptItem.cs +++ b/BetterGenshinImpact/Model/KeyMouseScriptItem.cs @@ -14,6 +14,9 @@ using BetterGenshinImpact.GameTask.Common; using Microsoft.Extensions.Logging; using System.Windows; using Newtonsoft.Json; +using LiteDB; +using BetterGenshinImpact.Model.TosUpload; +using BetterGenshinImpact.Service; namespace BetterGenshinImpact.Model; @@ -73,19 +76,75 @@ public partial class KeyMouseScriptItem : ObservableObject return $"{bytesPerSecond:F0} B/s"; } + private string GetRemotePath(string localFilePath) + { + var dirName = new DirectoryInfo(Path).Name; + var userName = TaskContext.Instance().Config.CommonConfig.UserName; + var uid = TaskContext.Instance().Config.CommonConfig.Uid; + + if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(uid)) + { + throw new InvalidOperationException("用户名或UID未设置"); + } + + var relativePath = localFilePath.Replace(_scriptPath, "").TrimStart('\\'); + var remotePath = $"{dirName[..10]}_{userName}_{uid}/{relativePath}"; + return remotePath.Replace(@"\", "/"); + } + + public void InitializeUploadStatus() + { + try + { + var collection = DbLiteService.Instance.UserDb.GetCollection("FileUploads"); + var files = Directory.GetFiles(Path, "*.*", SearchOption.AllDirectories); + + // 检查是否所有文件都已上传成功 + var allFilesUploaded = true; + foreach (var file in files) + { + try + { + var remotePath = GetRemotePath(file); + var fileUploadItem = collection.FindById(remotePath); + + if (fileUploadItem == null || fileUploadItem.Status != UploadStatus.UploadSuccess.ToString()) + { + allFilesUploaded = false; + break; + } + } + catch (InvalidOperationException) + { + IsUploadSuccess = false; + return; + } + } + + IsUploadSuccess = allFilesUploaded; + } + catch (Exception ex) + { + _logger.LogError(ex, "检查上传状态出错"); + IsUploadSuccess = false; + } + } + [RelayCommand] private async Task Upload() { - if (string.IsNullOrEmpty(Path) || !Directory.Exists(Path)) { await MessageBox.ErrorAsync($"文件夹不存在:{Path}"); return; } - var userName = TaskContext.Instance().Config.CommonConfig.UserName; - var uid = TaskContext.Instance().Config.CommonConfig.Uid; - if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(uid)) + try + { + // 提前验证用户信息,避免开始上传后才发现问题 + _ = GetRemotePath(Path); + } + catch (InvalidOperationException) { await MessageBox.ErrorAsync("请先设置用户名和UID"); return; @@ -98,7 +157,7 @@ public partial class KeyMouseScriptItem : ObservableObject await MessageBox.ErrorAsync("上传前文件校验失败,联系管理员"); return; } - + try { _uploadCts = new CancellationTokenSource(); @@ -109,9 +168,6 @@ public partial class KeyMouseScriptItem : ObservableObject var dirName = new DirectoryInfo(Path).Name; _logger.LogDebug($"{dirName} 开始上传..."); - // var userName = TaskContext.Instance().Config.CommonConfig.UserName; - // var uid = TaskContext.Instance().Config.CommonConfig.Uid; - await Task.Run(() => { try @@ -134,11 +190,9 @@ public partial class KeyMouseScriptItem : ObservableObject foreach (var file in files) { _uploadCts.Token.ThrowIfCancellationRequested(); - - var relativePath = file.Replace(_scriptPath, "").TrimStart('\\'); + var remotePath = GetRemotePath(file); + 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") @@ -178,6 +232,9 @@ public partial class KeyMouseScriptItem : ObservableObject UploadProgress = 100; UploadSpeed = string.Empty; // 清空速度显示 + + // 上传完成后,不需要额外的文件夹状态更新 + // FileUploadItem 的状态已经在 TosClientHelper 中更新 IsUploadSuccess = true; _logger.LogDebug($"{dirName} 上传完成"); } diff --git a/BetterGenshinImpact/Model/TosUpload/FolderUploadItem.cs b/BetterGenshinImpact/Model/TosUpload/FolderUploadItem.cs index 44c01b95..790d9481 100644 --- a/BetterGenshinImpact/Model/TosUpload/FolderUploadItem.cs +++ b/BetterGenshinImpact/Model/TosUpload/FolderUploadItem.cs @@ -2,8 +2,11 @@ public class FolderUploadItem { - // 文件路径 - public string? FilePath { get; set; } + // 文件夹路径作为Id + public string? Id { get; set; } + + // 文件夹名称 + public string? FolderName { get; set; } // 上传状态 public string? Status { get; set; } diff --git a/BetterGenshinImpact/View/Pages/KeyMouseRecordPage.xaml b/BetterGenshinImpact/View/Pages/KeyMouseRecordPage.xaml index 2f7b8dc1..ca97fce2 100644 --- a/BetterGenshinImpact/View/Pages/KeyMouseRecordPage.xaml +++ b/BetterGenshinImpact/View/Pages/KeyMouseRecordPage.xaml @@ -110,9 +110,20 @@ + Visibility="{Binding IsUploading, Converter={StaticResource BooleanToVisibilityRevertConverter}}"> + + + + - - - - - - - diff --git a/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs index 983d4769..f3d092e3 100644 --- a/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs @@ -80,13 +80,15 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation else { // 如果是新项目,创建新的实例 - updatedItems.Add(new KeyMouseScriptItem + var km = new KeyMouseScriptItem { Name = d.Name, Path = d.FullName, CreateTime = d.CreationTime, CreateTimeStr = d.CreationTime.ToString("yyyy-MM-dd HH:mm:ss") - }); + }; + km.InitializeUploadStatus(); + updatedItems.Add(km); } }