瀏覽代碼

feat(ios): download icloud files when re-indexing

leizhe 3 年之前
父節點
當前提交
c7b3bc5fa1

+ 13 - 0
ios/App/App/DownloadiCloudFiles.m

@@ -0,0 +1,13 @@
+//
+//  DowloadiCloudFiles.m
+//  Logseq
+//
+//  Created by leizhe on 2021/12/29.
+//
+
+#import <Foundation/Foundation.h>
+#import <Capacitor/Capacitor.h>
+
+CAP_PLUGIN(DownloadiCloudFiles, "DownloadiCloudFiles",
+           CAP_PLUGIN_METHOD(downloadFilesFromiCloud, CAPPluginReturnPromise);
+           )

+ 55 - 0
ios/App/App/DownloadiCloudFiles.swift

@@ -0,0 +1,55 @@
+//
+//  DownloadiCloudFiles.swift
+//  Logseq
+//
+//  Created by leizhe on 2021/12/29.
+//
+
+import Foundation
+import Capacitor
+
+@objc(DownloadiCloudFiles)
+public class DownloadiCloudFiles: CAPPlugin,  UIDocumentPickerDelegate  {
+    
+    public var _call: CAPPluginCall? = nil
+
+    let fileManager = FileManager.default
+    
+    var containerUrl: URL? {
+        return fileManager.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
+    }
+    
+    @objc func downloadFilesFromiCloud(_ call: CAPPluginCall) {
+        
+        if let url = self.containerUrl, fileManager.fileExists(atPath: url.path) {
+            do {
+                let downloaded = try self.downloadAllFilesFromCloud(at: url)
+                print("All files has been downloaded!")
+                self._call?.resolve(["success": downloaded])
+            } catch {
+                print("Can't download logseq's files from iCloud to local device.")
+                print(error.localizedDescription)
+            }
+        }
+    }
+    
+    func downloadAllFilesFromCloud(at url: URL) throws -> Bool {
+            
+        guard url.hasDirectoryPath else { return false }
+        let files = try fileManager.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [])
+        
+        var completed = false
+        
+        for file in files {
+            if file.pathExtension.lowercased().contains("icloud") {
+                try fileManager.startDownloadingUbiquitousItem(at: url)
+                completed = true
+            } else {
+                if try downloadAllFilesFromCloud(at: file) {
+                    completed = true
+                }
+            }
+        }
+        return completed
+    }
+}

+ 1 - 0
src/main/frontend/fs/capacitor_fs.cljs

@@ -196,6 +196,7 @@
                   (.pickFolder util/folder-picker)
                   (.pickFolder util/folder-picker)
                   #(js->clj % :keywordize-keys true)
                   #(js->clj % :keywordize-keys true)
                   :path)
                   :path)
+            _ (when (util/native-ios?) (.downloadFilesFromiCloud util/download-icloud-files))
             files (readdir path)
             files (readdir path)
             files (js->clj files :keywordize-keys true)]
             files (js->clj files :keywordize-keys true)]
       (into [] (concat [{:path path}] files))))
       (into [] (concat [{:path path}] files))))

+ 2 - 0
src/main/frontend/mobile/util.cljs

@@ -24,6 +24,8 @@
   (.isPluginAvailable Capacitor name))
   (.isPluginAvailable Capacitor name))
 
 
 (defonce folder-picker (registerPlugin "FolderPicker"))
 (defonce folder-picker (registerPlugin "FolderPicker"))
+(when (native-ios?)
+ (defonce download-icloud-files (registerPlugin "DownloadiCloudFiles")))
 (when (native-ios?)
 (when (native-ios?)
   (defonce ios-file-container (registerPlugin "FileContainer")))
   (defonce ios-file-container (registerPlugin "FileContainer")))