Compare commits

..

2 Commits

Author SHA1 Message Date
陈伟 068494a6f9 添加默认下载目录 2025-04-02 11:26:08 +08:00
洁 任 dd93c62d1d sqllite接入,sqlsugar应用 2025-04-02 09:30:13 +08:00
6 changed files with 252 additions and 73 deletions

View File

@ -11,6 +11,7 @@ using HeBianGu.Service.Mvp;
using HeBianGu.Systems.Identity;
using HeBianGu.Systems.Setting;
using Hopetry.Services;
using SqlSugar;
using SystemSetting = FileUploader.Models.SystemSetting;
namespace HeBianGu.App.Disk
@ -89,6 +90,7 @@ namespace HeBianGu.App.Disk
#endregion
#region - WindowCaption -
//以下各种功能按钮
@ -118,100 +120,147 @@ namespace HeBianGu.App.Disk
//services.AddSingleton<IExplorerService, WindowExplorerServiceDemo>();
var minioService = services.GetService<MinioService>();
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType != DriveType.Fixed) continue; // 只获取固定硬盘
Console.WriteLine(
// D:\ (Fixed) 软件 NTFS 329113399296
$"{drive.Name} ({drive.DriveType}) {drive.VolumeLabel} {drive.DriveFormat} {drive.TotalSize} {drive.AvailableFreeSpace}");
}
var minioService = services.GetService<MinioService>();
var bucketName = minioService._bucketName;
var xmlSerializerService = services.GetService<ISerializerService>();
if (File.Exists("./settings.xml"))
if (!File.Exists("./settings.xml"))
{
var setting = xmlSerializerService.Load<SystemSetting>("./settings.xml");
if (!string.IsNullOrEmpty(setting.SyncDir))
var tempSetting = new SystemSetting();
tempSetting.TaskCount = 3;
tempSetting.SyncDir = Path.Combine(SelectDrive(),"HopetryBoxData");
if (!Directory.Exists(tempSetting.SyncDir))
{
if (setting.TaskCount == 0)
{
setting.TaskCount = 3;
}
Directory.CreateDirectory(tempSetting.SyncDir);
}
xmlSerializerService.Save("./settings.xml", tempSetting);
}
// 创建统一的取消令牌源
var cancelToken = new CancellationTokenSource();
var temp = SyncSetting.Instance;
var notRun = true;
// 注册设置变更监听
temp.PropertyChanged += (s, e) =>
var setting = xmlSerializerService.Load<SystemSetting>("./settings.xml");
if (string.IsNullOrEmpty(setting.SyncDir))
{
var selectDrive = SelectDrive();
setting.SyncDir = Path.Combine(selectDrive, "HopetryBoxData");
if (!Directory.Exists(setting.SyncDir))
{
Directory.CreateDirectory(setting.SyncDir);
}
// 保存配置
xmlSerializerService.Save("./settings.xml", setting);
}
if (setting.TaskCount == 0)
{
setting.TaskCount = 3;
}
// 创建统一的取消令牌源
var cancelToken = new CancellationTokenSource();
var temp = SyncSetting.Instance;
var notRun = true;
// 注册设置变更监听
temp.PropertyChanged += (s, e) =>
{
if (temp.IsOn && notRun)
{
// 异步启动服务
Console.WriteLine("启动同步:");
try
{
if (temp.IsOn && notRun)
minioService.MirrorAsync(bucketName, setting.SyncDir, setting.TaskCount);
minioService.RealTimeListen(bucketName, setting.SyncDir,
cancellationToken: cancelToken.Token);
notRun = false;
}
catch (OperationCanceledException)
{
Console.WriteLine("同步监听已取消");
}
catch (Exception ex)
{
Console.WriteLine($"同步失败: {ex.Message}");
}
}
// 运行中 且 temp.IsOn 变成 false
else if (!notRun && !temp.IsOn)
{
notRun = true;
// 当设置关闭时取消所有任务
cancelToken.Cancel(); // 取消任务
cancelToken.Dispose();
cancelToken = new CancellationTokenSource();
}
};
/*var delayTimer = new DispatcherTimer();
delayTimer.Interval = TimeSpan.FromSeconds(5);
delayTimer.Tick += async (s, e) =>
{
delayTimer.Stop(); // 确保只执行一次
// 异步操作不会阻塞UI
await Current.Dispatcher.InvokeAsync(() =>
{
// 注册设置变更监听
SyncSetting.Instance.PropertyChanged += (s1, e1) =>
{
if (SyncSetting.Instance.IsOn)
{
// 异步启动服务
Console.WriteLine("启动同步:");
try
{
minioService.MirrorAsync(bucketName, setting.SyncDir, setting.TaskCount);
minioService.RealTimeListen(bucketName, setting.SyncDir,
cancellationToken: cancelToken.Token);
notRun = false;
}
catch (OperationCanceledException)
{
Console.WriteLine("同步监听已取消");
}
catch (Exception ex)
{
Console.WriteLine($"同步失败: {ex.Message}");
}
_ = StartSyncServicesAsync();
}
// 运行中 且 temp.IsOn 变成 false
else if (!notRun && !temp.IsOn)
else
{
notRun = true;
// 当设置关闭时取消所有任务
cancelToken.Cancel(); // 取消任务
cancelToken.Cancel();
cancelToken.Dispose();
cancelToken = new CancellationTokenSource();
}
};
/*var delayTimer = new DispatcherTimer();
delayTimer.Interval = TimeSpan.FromSeconds(5);
delayTimer.Tick += async (s, e) =>
{
delayTimer.Stop(); // 确保只执行一次
// 异步操作不会阻塞UI
await Current.Dispatcher.InvokeAsync(() =>
{
// 注册设置变更监听
SyncSetting.Instance.PropertyChanged += (s1, e1) =>
{
if (SyncSetting.Instance.IsOn)
{
// 异步启动服务
_ = StartSyncServicesAsync();
}
else
{
// 当设置关闭时取消所有任务
cancelToken.Cancel();
cancelToken.Dispose();
cancelToken = new CancellationTokenSource();
}
};
});
};
});
};
delayTimer.Start();*/
delayTimer.Start();*/
/*var syncSettingPath = SyncSetting.Instance.GetPath();
var syncSetting = xmlSerializerService.Load<SyncSetting>(syncSettingPath);
SyncSetting.Instance.IsOn = syncSetting.IsOn;
if (syncSetting.IsOn)
{
// 是否启监听
minioService.MirrorAsync1(bucketName, setting.SyncDir, setting.TaskCount);
}*/
/*var syncSettingPath = SyncSetting.Instance.GetPath();
var syncSetting = xmlSerializerService.Load<SyncSetting>(syncSettingPath);
SyncSetting.Instance.IsOn = syncSetting.IsOn;
if (syncSetting.IsOn)
{
// 是否启监听
minioService.MirrorAsync1(bucketName, setting.SyncDir, setting.TaskCount);
}*/
/*minioService.RealTimeListen(bucketName, setting.SyncDir,
cancellationToken: cancelToken.Token);*/
/*minioService.RealTimeListen(bucketName, setting.SyncDir,
cancellationToken: cancelToken.Token);*/
}
private static string SelectDrive()
{
var maxFreeSpace = 0L;
var selectDrive = "";
// 如果同步目录为空,则选择一个本地空闲空间比较大的分区
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType != DriveType.Fixed) continue; // 只获取固定硬盘
if (drive.AvailableFreeSpace > maxFreeSpace)
{
maxFreeSpace = drive.AvailableFreeSpace;
selectDrive = drive.Name;
}
}
return selectDrive;
}
protected override void Configure(IApplicationBuilder app)

View File

@ -57,6 +57,9 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.4.0" />
<PackageReference Include="Polly" Version="8.5.2" />
<PackageReference Include="SqlSugar" Version="5.1.4.187" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.187" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
<PackageReference Include="WindowsAPICodePack" Version="8.0.6" />
<PackageReference Include="WPF-UI" Version="4.0.2" />
</ItemGroup>

28
Models/FUpload.cs Normal file
View File

@ -0,0 +1,28 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hopetry.Models
{
[SugarTable("f_upload")]
public class FUpload
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public string FilePath { get; set; }
public string FileName { get; set; }
public long FileSize { get; set; }
public string FileType { get; set; }
public DateTime CreateTime { get; set; }
public DateTime CompleteTime { get; set; }
public bool IsComplete { get; set; }
}
}

View File

@ -0,0 +1,40 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hopetry.Provider
{
public class SqlSugarConfig
{
public static SqlSugarScope GetSqlSugarScope()
{
return new SqlSugarScope(new ConnectionConfig()
{
ConnectionString = $"Data Source={GetDbPath()};Version=3;", // 数据库路径
DbType = DbType.Sqlite, // 数据库类型
IsAutoCloseConnection = true, // 自动释放
InitKeyType = InitKeyType.Attribute // 从实体特性中读取主键信息
},
db =>
{
// 配置AOP
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql); // 输出SQL
};
});
}
private static string GetDbPath()
{
// 这里假设数据库放在应用程序根目录下
// 对于WPF项目可以使用AppDomain.CurrentDomain.BaseDirectory获取基目录
var basePath = AppDomain.CurrentDomain.BaseDirectory;
return Path.Combine(basePath, "minio.db");
}
}
}

View File

@ -0,0 +1,58 @@
using Hopetry.Models;
using Hopetry.Provider;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hopetry.Services
{
public class FileUploadService
{
private readonly SqlSugarScope _db;
public FileUploadService()
{
_db = SqlSugarConfig.GetSqlSugarScope();
}
// 示例:查询所有文件信息
public List<FUpload> GetAllFiles()
{
return _db.Queryable<FUpload>().ToList();
}
// 示例:添加文件信息
public bool AddFile(FUpload file)
{
return _db.Insertable(file).ExecuteCommand() > 0;
}
// 示例:更新文件信息
public bool UpdateFile(FUpload file)
{
return _db.Updateable(file).ExecuteCommand() > 0;
}
// 示例:删除文件信息
public bool DeleteFile(int id)
{
return _db.Deleteable<FUpload>().Where(f => f.Id == id).ExecuteCommand() > 0;
}
// 检查数据库是否存在,不存在则创建
public void InitDatabase()
{
_db.DbMaintenance.CreateDatabase(); // SQLite不需要创建数据库但可以用于检查
// 如果表不存在则创建
if (!_db.DbMaintenance.IsAnyTable("f_upload", false))
{
_db.CodeFirst.InitTables(typeof(FUpload));
}
}
}
}

View File

@ -23,6 +23,7 @@ namespace HeBianGu.App.Disk
internal class LoyoutViewModel : MvcViewModelBase
{
private string _path;
private readonly FileUploadService _uploadService;
/// <summary> 说明 </summary>
public string Path
{
@ -368,9 +369,9 @@ namespace HeBianGu.App.Disk
public LoyoutViewModel(SendViewModel sendViewModel)
{
_sendViewModel = sendViewModel;
_uploadService = new FileUploadService();
UploadCommand = new AsyncRelayCommand(async () => await UploadFile());
UploadCommand1 = new AsyncRelayCommand(async () => await UploadFile1());
// 初始化Timer
_progressTimer = new System.Timers.Timer(1000);
_progressTimer.Elapsed += UpdateProgress;