This commit is contained in:
辉鸭蛋
2025-01-12 17:06:06 +08:00
parent 39c043bf3f
commit a532f8ab29
5 changed files with 123 additions and 27 deletions

View File

@@ -94,6 +94,15 @@ public class TosClientHelper
{
objectKey ??= Path.GetFileName(localFileName);
// 检查是否已经上传成功
// var collection = _dbService.UserDb.GetCollection<FileUploadItem>("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<FileUploadItem>("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
/// <param name="partSize">分片大小</param>
public void ResumableUpload(string objectKey, string uploadID, string localFileName, long partSize = 20 * 1024 * 1024, UploadProgressCallback? progressCallback = null)
{
// 检查是否已经上传成功
// var collection = _dbService.UserDb.GetCollection<FileUploadItem>("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,

View File

@@ -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<FileUploadItem>("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} 上传完成");
}

View File

@@ -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; }

View File

@@ -110,9 +110,20 @@
<StackPanel Orientation="Horizontal">
<ui:Button x:Name="UploadButton"
Command="{Binding UploadCommand}"
Content="上传"
Icon="{ui:SymbolIcon CloudArrowUp24}"
Visibility="{Binding IsUploading, Converter={StaticResource BooleanToVisibilityRevertConverter}}" />
Visibility="{Binding IsUploading, Converter={StaticResource BooleanToVisibilityRevertConverter}}">
<ui:Button.Style>
<Style TargetType="ui:Button" BasedOn="{StaticResource {x:Type ui:Button}}">
<Setter Property="Content" Value="上传" />
<Setter Property="Icon" Value="{ui:SymbolIcon CloudArrowUp24}" />
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsUploadSuccess}" Value="True">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</ui:Button.Style>
</ui:Button>
<ui:Button Command="{Binding StopUploadCommand}"
Content="停止"
@@ -143,13 +154,6 @@
</Setter.Value>
</Setter>
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsUploadSuccess}" Value="False" />
<Condition Binding="{Binding UploadProgress}" Value="0" />
</MultiDataTrigger.Conditions>
<Setter Property="Text" Value="" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>

View File

@@ -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);
}
}