Browse Source

home: fix permission issue when importing shortcut

This fixes both drag & drop linking as well as .utm file opening.

Fixes #5310
Fixes #5311
osy 2 years ago
parent
commit
291be25111
3 changed files with 11 additions and 8 deletions
  1. 4 2
      Managers/UTMVirtualMachine.m
  2. 3 6
      Platform/Shared/ContentView.swift
  3. 4 0
      Platform/UTMData.swift

+ 4 - 2
Managers/UTMVirtualMachine.m

@@ -139,9 +139,11 @@ const dispatch_time_t kScreenshotPeriodSeconds = 60 * NSEC_PER_SEC;
 }
 
 + (nullable UTMVirtualMachine *)virtualMachineWithURL:(NSURL *)url {
-    [url startAccessingSecurityScopedResource];
+    BOOL isScopedAccess = [url startAccessingSecurityScopedResource];
     UTMConfigurationWrapper *config = [[UTMConfigurationWrapper alloc] initFrom:url];
-    [url stopAccessingSecurityScopedResource];
+    if (isScopedAccess) {
+        [url stopAccessingSecurityScopedResource];
+    }
     if (config) {
         UTMVirtualMachine *vm = [UTMVirtualMachine virtualMachineWithConfigurationWrapper:config packageURL:url];
         dispatch_async(dispatch_get_main_queue(), ^{

+ 3 - 6
Platform/Shared/ContentView.swift

@@ -217,12 +217,9 @@ extension ContentView: DropDelegate {
 
         providers.forEach { provider in
             group.enter()
-            _ = provider.loadDataRepresentation(forTypeIdentifier: UTType.fileURL.identifier) { (data, error) in
-                if let data = data {
-                    let url = URL(dataRepresentation: data, relativeTo: nil)
-                    if url?.pathExtension == "utm" {
-                        validURLs.append(url!)
-                    }
+            _ = provider.loadObject(ofClass: URL.self) { url, _ in
+                if url?.pathExtension == "utm" {
+                    validURLs.append(url!)
                 }
                 group.leave()
             }

+ 4 - 0
Platform/UTMData.swift

@@ -589,6 +589,10 @@ class UTMData: ObservableObject {
         defer { url.stopAccessingSecurityScopedResource() }
         
         logger.info("importing: \(url)")
+        // attempt to turn temp URL to presistent bookmark early otherwise,
+        // when stopAccessingSecurityScopedResource() is called, we lose access
+        let bookmark = try url.persistentBookmarkData()
+        let url = try URL(resolvingPersistentBookmarkData: bookmark)
         let fileBasePath = url.deletingLastPathComponent()
         let fileName = url.lastPathComponent
         let dest = documentsURL.appendingPathComponent(fileName, isDirectory: true)