Skip to content

Commit

Permalink
GroupProfile hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed May 9, 2024
1 parent 6bb4a78 commit 723105d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
35 changes: 26 additions & 9 deletions src/SyncClipboard.Core/Clipboard/Profile/GroupProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class GroupProfile : FileProfile
protected override IClipboardSetter<Profile> ClipboardSetter
=> ServiceProvider.GetRequiredService<IClipboardSetter<GroupProfile>>();

private GroupProfile(string[] files, string hash)
private GroupProfile(IEnumerable<string> files, string hash)
: base(Path.Combine(LocalTemplateFolder, $"{Path.GetRandomFileName()}.zip"), hash)
{
_files = files;
_files = files.ToArray();
}

public GroupProfile(ClipboardProfileDTO profileDTO) : base(profileDTO)
Expand All @@ -35,24 +35,42 @@ public static async Task<GroupProfile> Create(string[] files, CancellationToken
return new GroupProfile(files, hash);
}

private static int FileCompare(FileInfo file1, FileInfo file2)
{
if (file1.Length == file2.Length)
{
return Comparer<int>.Default.Compare(file1.Name.ListHashCode(), file2.Name.ListHashCode());
}
return Comparer<long>.Default.Compare(file1.Length, file2.Length);
}

private static int FileNameCompare(string file1, string file2)
{
return Comparer<int>.Default.Compare(
Path.GetFileName(file1).ListHashCode(),
Path.GetFileName(file2).ListHashCode()
);
}

private static string CaclHash(string[] files)
{
var maxSize = Config.GetConfig<SyncConfig>().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();
}
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/SyncClipboard.Core/Models/ClipboardMetaInfomation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private async Task<ClipboardMetaInfomation> HandleMacosClipboard(CancellationTok
{
var clipboard = App.Current.MainWindow.Clipboard!;
var formats = await clipboard.GetFormatsAsync().WaitAsync(token);

ClipboardMetaInfomation meta = new();
foreach (var handlerMapping in MacFormatHandlerlist)
{
Expand All @@ -52,7 +52,7 @@ private async Task<ClipboardMetaInfomation> HandleMacosClipboard(CancellationTok
private async Task HandleMacosFile(ClipboardMetaInfomation meta, CancellationToken token)
{
var items = await Clipboard.GetDataAsync(Format.FileList).WaitAsync(token) as IEnumerable<IStorageItem>;
meta.Files = items?.Select(item=> item.Path.LocalPath).ToArray();
meta.Files = items?.Select(item => item.Path.LocalPath).ToArray();
}

[SupportedOSPlatform("macos")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected override async Task<ClipboardProfileDTO> GetSyncProfile(string rootPat

protected override async Task<IResult> PutSyncProfile(ClipboardProfileDTO profileDTO, string rootPath, string path)
{
_profileDtoCache = null;
await _profileDtoHelper.SetLocalClipboardWithDto(profileDTO, Path.Combine(rootPath, "file"));
return Results.Ok();
}
Expand Down

0 comments on commit 723105d

Please sign in to comment.