Skip to content

Commit

Permalink
对MagickImage图片转换进行缓存,防止多次转换造成文件hash不一致
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed May 30, 2024
1 parent b208f94 commit 03cc931
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ private void ReleaseRef()
try
{
ClipboardChangedImpl?.Invoke();
meta ??= await ClipboardFactory.GetMetaInfomation(new CancellationTokenSource(1000).Token);
var profile = await ClipboardFactory.CreateProfileFromMeta(meta, new CancellationTokenSource(1000).Token);
var token = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token;
meta ??= await ClipboardFactory.GetMetaInfomation(token);
var profile = await ClipboardFactory.CreateProfileFromMeta(meta, token);
ChangedImpl?.Invoke(meta, profile);
}
catch (Exception ex)
Expand Down
4 changes: 0 additions & 4 deletions src/SyncClipboard.Core/Clipboard/Profile/ImageProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ private static string SaveImageToFile(IClipboardImage image)

private static string GetImageExtention()
{
if (OperatingSystem.IsWindows())
{
return "bmp";
}
return "png";
}

Expand Down
36 changes: 35 additions & 1 deletion src/SyncClipboard.Core/Utilities/ClipboardImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace SyncClipboard.Core.Utilities;

public class ClipboardImage : IClipboardImage
{
private static int CacheHash = 0;
private static readonly MemoryStream Cache = new MemoryStream();

private readonly byte[] _imageBytes;

public ClipboardImage(byte[]? imageBytes)
Expand All @@ -15,7 +18,38 @@ public ClipboardImage(byte[]? imageBytes)

public void Save(string path)
{
var hash = _imageBytes.ListHashCode();
if (UseCache(hash, path))
return;

using MagickImage magickImage = new(_imageBytes);
magickImage.Write(path);
lock (Cache)
{
CacheHash = hash;
Cache.SetLength(0);
Cache.Seek(0, SeekOrigin.Begin);
magickImage.Write(Cache, MagickFormat.Png);
WriteToFile(path);
}
}

private static bool UseCache(int hash, string path)
{
lock (Cache)
{
if (hash == CacheHash)
{
WriteToFile(path);
return true;
}
}
return false;
}

private static void WriteToFile(string file)
{
using var fileStream = File.Create(file);
Cache.Seek(0, SeekOrigin.Begin);
Cache.CopyTo(fileStream);
}
}
8 changes: 8 additions & 0 deletions src/SyncClipboard.Desktop/ClipboardAva/ClipboardListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ protected override void UnRegistSystemEvent(MetaChanged action)
_cts = null;
}

internal void TriggerClipboardChangedEvent()
{
_cts?.Cancel();
_cts?.Dispose();
_cts = null;
InvokeTick(null);
}

private async void InvokeTick(object? _)
{
if (_tickSemaphore.Wait(0) is false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Input;
using Microsoft.Extensions.DependencyInjection;
using SyncClipboard.Core.Clipboard;
using SyncClipboard.Core.Models;
using System;
Expand All @@ -23,13 +24,14 @@ private static async Task SetPackageToClipboard(DataObject obj, CancellationToke
try
{
await App.Current.Clipboard.SetDataObjectAsync(obj).WaitAsync(ctk);
await Task.Delay(200, ctk);
}
catch { }
finally
{
ClipboardFactory._semaphoreSlim.Release();
}

App.Current.Services.GetRequiredService<ClipboardListener>().TriggerClipboardChangedEvent();
}

[SupportedOSPlatform("linux")]
Expand Down

0 comments on commit 03cc931

Please sign in to comment.