Skip to content

Commit

Permalink
修复windows获取文件类型剪贴板失败
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed May 9, 2024
1 parent 723105d commit c8fe471
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/SyncClipboard.Core/Commons/Env.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class Env
public static readonly string Directory = AppDomain.CurrentDomain.BaseDirectory;
public static readonly string AppDataDirectory = GetAppDataDirectory();
public static readonly string ProgramPath = Environment.ProcessPath ?? "";
public static readonly string ProgramFolder = Path.GetDirectoryName(ProgramPath) ?? "";
public static readonly string UserConfigFile = FullPath("SyncClipboard.json");
public static readonly string TemplateFileFolder = FullPath("file");
public static readonly string RemoteFileFolder = "file";
Expand Down
23 changes: 22 additions & 1 deletion src/SyncClipboard.WinUI3/ClipboardWinUI/ClipboardFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ internal partial class ClipboardFactory : ClipboardFactoryBase
[StandardDataFormats.Bitmap] = HanleBitmap,
[StandardDataFormats.Html] = HanleHtml,
[StandardDataFormats.StorageItems] = HanleFiles,
//["FileDrop"] = HanleFiles2,
["Preferred DropEffect"] = HanleDropEffect,
["Object Descriptor"] = HanleObjectDescriptor,
}.ToList();
Expand Down Expand Up @@ -91,6 +92,26 @@ private static async Task HanleFiles(DataPackageView ClipboardData, ClipboardMet
meta.Files = list.Select(storageItem => storageItem.Path).ToArray();
}

// https://learn.microsoft.com/en-us/windows/win32/shell/clipboard#cf_hdrop
#pragma warning disable CC0068 // Unused Method
#pragma warning disable IDE0060 // 删除未使用的参数
private static async Task HanleFiles2(DataPackageView ClipboardData, ClipboardMetaInfomation meta, CancellationToken ctk)
#pragma warning restore IDE0060 // 删除未使用的参数
{
if (meta.Files is not null && meta.Files.Any())
return;
var res = await ClipboardData.GetDataAsync("FileDrop");
using IRandomAccessStream randomAccessStream = res.As<IRandomAccessStream>();
using var stream = randomAccessStream.AsStreamForRead();
using MemoryStream ms = new();
stream.CopyTo(ms);
var bytes = ms.ToArray();
var str = Encoding.Unicode.GetString(bytes);
var files = str.Split('\0').Where(file => Directory.Exists(file) || File.Exists(file));
meta.Files = files.ToArray();
}
#pragma warning restore CC0068 // Unused Method

private static async Task HanleHtml(DataPackageView ClipboardData, ClipboardMetaInfomation meta, CancellationToken ctk)
=> meta.Html = await ClipboardData.GetHtmlFormatAsync();

Expand Down Expand Up @@ -145,7 +166,7 @@ public override async Task<ClipboardMetaInfomation> GetMetaInfomation(Cancellati
{
errortimes += 1;
Logger.Write(LOG_TAG, ex.ToString());
Thread.Sleep(200);
await Task.Delay(100, ctk);
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion src/SyncClipboard.WinUI3/ClipboardWinUI/FileClipboardSetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
using System.IO;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.Storage.Streams;

namespace SyncClipboard.WinUI3.ClipboardWinUI;

internal class FileClipboardSetter : ClipboardSetterBase<FileProfile>, IClipboardSetter<GroupProfile>
{
public static string[] UnusualType = { ".lnk", ".url", ".wsh" };
private static readonly string Thumbnail = Path.Combine($"{Core.Commons.Env.ProgramFolder}", "Assets", "icon.ico");

protected override DataPackage CreatePackage(ClipboardMetaInfomation metaInfomation)
{
Expand All @@ -28,7 +30,8 @@ protected override DataPackage CreatePackage(ClipboardMetaInfomation metaInfomat
}
else if (IsUnusualType(file))
{
list.Add(new UnusualStorageItem(file));
var image = RandomAccessStreamReference.CreateFromFile(StorageFile.GetFileFromPathAsync(Thumbnail).AsTask().Result);
list.Add(StorageFile.CreateStreamedFileAsync(Path.GetFileName(file), StreamHandler(file), image).AsTask().Result);
}
else
{
Expand All @@ -51,4 +54,22 @@ public static bool IsUnusualType(string file)
}
return false;
}

private static StreamedFileDataRequestedHandler StreamHandler(string file)
{
return (StreamedFileDataRequest request) =>
{
try
{
using FileStream fileStream = new(file, FileMode.Open, FileAccess.Read);
using var stream = request.AsStreamForWrite();
fileStream.CopyTo(stream);
request.Dispose();
}
catch (Exception)
{
request.FailAndClose(StreamedFileFailureMode.Incomplete);
}
};
}
}

0 comments on commit c8fe471

Please sign in to comment.