diff --git a/src/SyncClipboard.Core/Clipboard/Profile/GroupProfile.cs b/src/SyncClipboard.Core/Clipboard/Profile/GroupProfile.cs index be749810..4919104a 100644 --- a/src/SyncClipboard.Core/Clipboard/Profile/GroupProfile.cs +++ b/src/SyncClipboard.Core/Clipboard/Profile/GroupProfile.cs @@ -19,10 +19,10 @@ public class GroupProfile : FileProfile protected override IClipboardSetter ClipboardSetter => ServiceProvider.GetRequiredService>(); - private GroupProfile(string[] files, string hash) + private GroupProfile(IEnumerable files, string hash) : base(Path.Combine(LocalTemplateFolder, $"{Path.GetRandomFileName()}.zip"), hash) { - _files = files; + _files = files.ToArray(); } public GroupProfile(ClipboardProfileDTO profileDTO) : base(profileDTO) @@ -35,24 +35,42 @@ public static async Task Create(string[] files, CancellationToken return new GroupProfile(files, hash); } + private static int FileCompare(FileInfo file1, FileInfo file2) + { + if (file1.Length == file2.Length) + { + return Comparer.Default.Compare(file1.Name.ListHashCode(), file2.Name.ListHashCode()); + } + return Comparer.Default.Compare(file1.Length, file2.Length); + } + + private static int FileNameCompare(string file1, string file2) + { + return Comparer.Default.Compare( + Path.GetFileName(file1).ListHashCode(), + Path.GetFileName(file2).ListHashCode() + ); + } + private static string CaclHash(string[] files) { var maxSize = Config.GetConfig().MaxFileByte; - Array.Sort(files); + Array.Sort(files, FileNameCompare); long sumSize = 0; int hash = 0; - string? hashString = null; foreach (var file in files) { if (Directory.Exists(file)) { var directoryInfo = new DirectoryInfo(file); hash = (hash * -1521134295) + directoryInfo.Name.ListHashCode(); - foreach (var subFile in directoryInfo.GetFiles("*", SearchOption.AllDirectories)) + var subFiles = directoryInfo.GetFiles("*", SearchOption.AllDirectories); + Array.Sort(subFiles, FileCompare); + foreach (var subFile in subFiles) { sumSize += subFile.Length; if (sumSize > maxSize) - break; + return MD5_FOR_OVERSIZED_FILE; hash = (hash * -1521134295) + (subFile.Name + subFile.Length.ToString()).ListHashCode(); } } @@ -65,12 +83,11 @@ private static string CaclHash(string[] files) if (sumSize > maxSize) { - hashString = MD5_FOR_OVERSIZED_FILE; - break; + return MD5_FOR_OVERSIZED_FILE; } } - return hashString ?? hash.ToString(); + return hash.ToString(); } public override async Task UploadProfile(IWebDav webdav, CancellationToken token) diff --git a/src/SyncClipboard.Core/Models/ClipboardMetaInfomation.cs b/src/SyncClipboard.Core/Models/ClipboardMetaInfomation.cs index be6361a7..fffe82ca 100644 --- a/src/SyncClipboard.Core/Models/ClipboardMetaInfomation.cs +++ b/src/SyncClipboard.Core/Models/ClipboardMetaInfomation.cs @@ -13,7 +13,7 @@ public record class ClipboardMetaInfomation public string[]? _files; public string[]? Files { - get => _files; + get => (string[]?)_files?.Clone(); set { _files = (string[]?)value?.Clone(); diff --git a/src/SyncClipboard.Desktop/ClipboardAva/ClipboardFactory.Macos.cs b/src/SyncClipboard.Desktop/ClipboardAva/ClipboardFactory.Macos.cs index 6e89fa6b..4c5c8c4b 100644 --- a/src/SyncClipboard.Desktop/ClipboardAva/ClipboardFactory.Macos.cs +++ b/src/SyncClipboard.Desktop/ClipboardAva/ClipboardFactory.Macos.cs @@ -35,7 +35,7 @@ private async Task HandleMacosClipboard(CancellationTok { var clipboard = App.Current.MainWindow.Clipboard!; var formats = await clipboard.GetFormatsAsync().WaitAsync(token); - + ClipboardMetaInfomation meta = new(); foreach (var handlerMapping in MacFormatHandlerlist) { @@ -52,7 +52,7 @@ private async Task HandleMacosClipboard(CancellationTok private async Task HandleMacosFile(ClipboardMetaInfomation meta, CancellationToken token) { var items = await Clipboard.GetDataAsync(Format.FileList).WaitAsync(token) as IEnumerable; - meta.Files = items?.Select(item=> item.Path.LocalPath).ToArray(); + meta.Files = items?.Select(item => item.Path.LocalPath).ToArray(); } [SupportedOSPlatform("macos")] diff --git a/src/SyncClipboard.Server.Core/Controller/SyncClipboardPassiveController.cs b/src/SyncClipboard.Server.Core/Controller/SyncClipboardPassiveController.cs index 6fab30e7..803438ef 100644 --- a/src/SyncClipboard.Server.Core/Controller/SyncClipboardPassiveController.cs +++ b/src/SyncClipboard.Server.Core/Controller/SyncClipboardPassiveController.cs @@ -20,6 +20,7 @@ protected override async Task GetSyncProfile(string rootPat protected override async Task PutSyncProfile(ClipboardProfileDTO profileDTO, string rootPath, string path) { + _profileDtoCache = null; await _profileDtoHelper.SetLocalClipboardWithDto(profileDTO, Path.Combine(rootPath, "file")); return Results.Ok(); }