Simplify temp local file creation while fetching contents, use File Provider volume
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Wed, 15 Mar 2023 11:59:02 +0000 (12:59 +0100)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Fri, 12 May 2023 05:28:46 +0000 (13:28 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension.swift
shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/LocalFilesUtils.swift

index 892d1619d8d06fa1a7474fce8deaffe4d11255c3..d6b699c3c13824ae0d8683e33a0858b3cb7f41fb 100644 (file)
@@ -145,7 +145,7 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension, NKComm
 
         // TODO: Handle folders nicely
         do {
-            let fileNameLocalPath = try localPathForNCFile(ocId: metadata.ocId, fileNameView: metadata.fileNameView)
+            let fileNameLocalPath = try localPathForNCFile(ocId: metadata.ocId, fileNameView: metadata.fileNameView, domain: self.domain)
 
             dbManager.setStatusForItemMetadata(metadata, status: NextcloudItemMetadataTable.Status.downloading) { updatedMetadata in
 
index 0a9d1eaa497fc2847039ac7e4a5e39e47617a08d..bb3a2bdbc4dc615c553c8f4b3047af9418d7649a 100644 (file)
@@ -30,29 +30,21 @@ func pathForFileProviderExtData() -> URL? {
     return containerUrl?.appendingPathComponent("FileProviderExt/")
 }
 
-func pathForFileProviderExtFiles() -> URL? {
-    let fileProviderDataUrl = pathForFileProviderExtData()
-    return fileProviderDataUrl?.appendingPathComponent("Files/")
-}
-
-@discardableResult func localPathForNCDirectory(ocId: String) throws -> URL {
-    guard let fileProviderFilesPathUrl = pathForFileProviderExtFiles() else {
-        throw URLError(.badURL)
+func pathForFileProviderTempFilesForDomain(_ domain: NSFileProviderDomain) throws -> URL? {
+    guard let fpManager = NSFileProviderManager(for: domain) else {
+        throw NSFileProviderError(.providerNotFound)
     }
 
-    let folderPathUrl = fileProviderFilesPathUrl.appendingPathComponent(ocId)
-    let folderPath = folderPathUrl.path
+    let fileProviderDataUrl = try fpManager.temporaryDirectoryURL()
+    return fileProviderDataUrl.appendingPathComponent("TemporaryNextcloudFiles/")
+}
 
-    if !FileManager.default.fileExists(atPath: folderPath) {
-        try FileManager.default.createDirectory(at: folderPathUrl, withIntermediateDirectories: true)
+func localPathForNCFile(ocId: String, fileNameView: String, domain: NSFileProviderDomain) throws -> URL {
+    guard let fileProviderFilesPathUrl = try pathForFileProviderTempFilesForDomain(domain) else {
+        throw URLError(.badURL)
     }
 
-    return folderPathUrl
-}
-
-@discardableResult func localPathForNCFile(ocId: String, fileNameView: String) throws -> URL {
-    let fileFolderPathUrl = try localPathForNCDirectory(ocId: ocId)
-    let filePathUrl = fileFolderPathUrl.appendingPathComponent(fileNameView)
+    let filePathUrl = fileProviderFilesPathUrl.appendingPathComponent(fileNameView)
     let filePath = filePathUrl.path
 
     if !FileManager.default.fileExists(atPath: filePath) {