Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
fix(macos): convert the file a Finder alias points to
mendax-iitm4a3c9cd7251f
diff --git a/src/Filee.Core/Platform/IPlatformServices.cs b/src/Filee.Core/Platform/IPlatformServices.csindex 049f6fb2..d3690414 100644--- a/src/Filee.Core/Platform/IPlatformServices.cs+++ b/src/Filee.Core/Platform/IPlatformServices.cs@@ -53,9 +53,9 @@ public interface IPlatformServices}/// <summary>- /// The existing file or folder the shortcut at <paramref name="path"/> points to (a .lnk on Windows); null for- /// anything else, a broken shortcut, or a platform whose shortcuts Filee doesn't follow (macOS aliases and Linux- /// .desktop links aren't followed yet).+ /// The existing file or folder the shortcut at <paramref name="path"/> points to (a .lnk on Windows, a Finder+ /// alias on macOS); null for anything else, a broken shortcut, or a platform whose shortcuts Filee doesn't follow+ /// (Linux .desktop links aren't followed yet)./// </summary>string? ShortcutTarget(string path) => null;diff --git a/src/Filee.Platform.MacOS/MacOSPlatformServices.cs b/src/Filee.Platform.MacOS/MacOSPlatformServices.csindex 8fe002a5..a7a53a6b 100644--- a/src/Filee.Platform.MacOS/MacOSPlatformServices.cs+++ b/src/Filee.Platform.MacOS/MacOSPlatformServices.cs@@ -34,6 +34,8 @@ public sealed class MacOSPlatformServices : IPlatformServicespublic TrashRefusal? MoveToTrash(string path) => NativeMethods.MoveToTrash(path);+ public string? ShortcutTarget(string path) => NativeMethods.AliasTarget(path);+public bool IsFileManagerAt(int x, int y) =>string.Equals(ProcessNameAt(x, y), "Finder", StringComparison.OrdinalIgnoreCase);diff --git a/src/Filee.Platform.MacOS/NativeMethods.cs b/src/Filee.Platform.MacOS/NativeMethods.csindex 2fe25218..e2c00ff8 100644--- a/src/Filee.Platform.MacOS/NativeMethods.cs+++ b/src/Filee.Platform.MacOS/NativeMethods.cs@@ -209,6 +209,104 @@ internal static class NativeMethods[return: MarshalAs(UnmanagedType.I1)]private static extern bool SendTrash(nint receiver, nint selector, nint url, nint resultingUrl, out nint error);+ /// <summary>+ /// The existing file or folder the Finder alias at <paramref name="path"/> points to; null for anything that+ /// isn't an alias (CFURLCreateBookmarkDataFromFile reads no bookmark from it), a broken alias, or one whose+ /// target is on a volume that isn't mounted: resolving never shows UI or mounts a server, since a drag waits on it.+ /// </summary>+ internal static string? AliasTarget(string path)+ {+ const nuint withoutUi = 1 << 8, withoutMounting = 1 << 9;+ var url = FileUrl(path);+ if (url == 0)+ return null;+ nint data = 0, target = 0;+ try+ {+ data = CFURLCreateBookmarkDataFromFile(0, url, out var error);+ if (data == 0)+ {+ Release(error);+ return null;+ }+ target = CFURLCreateByResolvingBookmarkData(0, data, withoutUi | withoutMounting, 0, 0, out _, out error);+ Release(error);+ if (target == 0)+ return null;+ var buffer = new byte[4096];+ if (!CFURLGetFileSystemRepresentation(target, true, buffer, buffer.Length))+ return null;+ // The file system representation is decomposed (NFD); dropped paths and the names Filee writes are+ // composed, and APFS and HFS+ find the file either way, so a "보고서" alias and its target match.+ var resolved = System.Text.Encoding.UTF8.GetString(buffer, 0, Array.IndexOf(buffer, (byte)0))+ .Normalize(System.Text.NormalizationForm.FormC);+ return File.Exists(resolved) || Directory.Exists(resolved) ? resolved : null;+ }+ finally+ {+ Release(target);+ Release(data);+ CFRelease(url);+ }+ }++ /// <summary>Writes a Finder alias at <paramref name="path"/> to <paramref name="target"/> (for the tests).</summary>+ internal static void CreateAlias(string path, string target)+ {+ const nuint suitableForBookmarkFile = 1 << 10;+ var targetUrl = FileUrl(target);+ var aliasUrl = FileUrl(path);+ nint data = 0;+ try+ {+ data = CFURLCreateBookmarkData(0, targetUrl, suitableForBookmarkFile, 0, 0, out var error);+ Release(error);+ if (data == 0 || !CFURLWriteBookmarkDataToFile(data, aliasUrl, 0, out error))+ {+ Release(error);+ throw new IOException($"Could not write an alias at {path}.");+ }+ }+ finally+ {+ Release(data);+ Release(aliasUrl);+ Release(targetUrl);+ }+ }++ private static nint FileUrl(string path)+ {+ var bytes = System.Text.Encoding.UTF8.GetBytes(Path.GetFullPath(path));+ return CFURLCreateFromFileSystemRepresentation(0, bytes, bytes.Length, Directory.Exists(path));+ }++ private static void Release(nint value)+ {+ if (value != 0)+ CFRelease(value);+ }++ [DllImport(CoreFoundation)]+ private static extern nint CFURLCreateFromFileSystemRepresentation(nint allocator, byte[] buffer, nint length, [MarshalAs(UnmanagedType.I1)] bool isDirectory);++ [DllImport(CoreFoundation)]+ [return: MarshalAs(UnmanagedType.I1)]+ private static extern bool CFURLGetFileSystemRepresentation(nint url, [MarshalAs(UnmanagedType.I1)] bool resolveAgainstBase, byte[] buffer, nint maxLength);++ [DllImport(CoreFoundation)]+ private static extern nint CFURLCreateBookmarkDataFromFile(nint allocator, nint fileUrl, out nint error);++ [DllImport(CoreFoundation)]+ private static extern nint CFURLCreateByResolvingBookmarkData(nint allocator, nint bookmark, nuint options, nint relativeToUrl, nint resourcePropertiesToInclude, [MarshalAs(UnmanagedType.I1)] out bool isStale, out nint error);++ [DllImport(CoreFoundation)]+ private static extern nint CFURLCreateBookmarkData(nint allocator, nint url, nuint options, nint resourcePropertiesToInclude, nint relativeToUrl, out nint error);++ [DllImport(CoreFoundation)]+ [return: MarshalAs(UnmanagedType.I1)]+ private static extern bool CFURLWriteBookmarkDataToFile(nint bookmark, nint fileUrl, nuint options, out nint error);+private readonly struct AutoreleasePool : IDisposable{private readonly nint _pool = Send(Send(objc_getClass("NSAutoreleasePool"), sel_registerName("alloc")), sel_registerName("init"));diff --git a/tests/Filee.Platform.Tests/MacOSAliasTests.cs b/tests/Filee.Platform.Tests/MacOSAliasTests.csnew file mode 100644index 00000000..b31cac94--- /dev/null+++ b/tests/Filee.Platform.Tests/MacOSAliasTests.cs@@ -0,0 +1,80 @@+// Finder aliases dropped on Filee convert the file they point to, like Windows shortcuts (#159).++using Filee.Platform.MacOS;++// Each test checks for macOS before it touches the native methods.+#pragma warning disable CA1416++namespace Filee.Platform.Tests;++public sealed class MacOSAliasTests : IDisposable+{+ // The temporary folder is under /var, a symbolic link to /private/var, and a resolved alias gives the real path.+ private readonly string _folder = RealPath(Directory.CreateTempSubdirectory("filee-alias-").FullName);++ private static string RealPath(string path) =>+ path.StartsWith("/var/", StringComparison.Ordinal) ? "/private" + path : path;++ public void Dispose()+ {+ try { Directory.Delete(_folder, true); } catch (IOException) { }+ }++ private string Write(string name)+ {+ var path = Path.Combine(_folder, name);+ File.WriteAllText(path, "x");+ return path;+ }++ private string Alias(string name, string target)+ {+ var path = Path.Combine(_folder, name);+ NativeMethods.CreateAlias(path, target);+ return path;+ }++ [Fact]+ public void An_alias_resolves_to_its_file_or_folder_and_a_broken_one_to_nothing()+ {+ if (!OperatingSystem.IsMacOS())+ {+ Assert.Skip("Finder aliases need macOS.");+ return;+ }+ var report = Write("보고서 report.docx");+ var folder = Directory.CreateDirectory(Path.Combine(_folder, "photos")).FullName;+ var gone = Write("gone.pdf");+ var toReport = Alias("report.docx alias", report);+ var toFolder = Alias("photos alias", folder);+ var broken = Alias("gone alias", gone);+ File.Delete(gone);+ var platform = new MacOSPlatformServices();++ Assert.Equal(report, platform.ShortcutTarget(toReport));+ Assert.Equal(folder, platform.ShortcutTarget(toFolder));+ Assert.Null(platform.ShortcutTarget(broken));+ Assert.Null(platform.ShortcutTarget(report));+ Assert.Null(platform.ShortcutTarget(folder));+ Assert.Null(platform.ShortcutTarget(Path.Combine(_folder, "missing alias")));+ }++ [Fact]+ public void An_alias_dropped_with_its_target_gives_the_file_once_and_a_broken_one_stays()+ {+ if (!OperatingSystem.IsMacOS())+ {+ Assert.Skip("Finder aliases need macOS.");+ return;+ }+ var report = Write("report.docx");+ var photo = Write("photo.jpg");+ var toReport = Alias("report alias", report);+ var broken = Write("broken alias");+ Core.Platform.IPlatformServices platform = new MacOSPlatformServices();++ var files = platform.ResolveShortcuts([toReport, photo, report, broken]);++ Assert.Equal([report, photo, broken], files);+ }+}