Skip to content

Commit

Permalink
feat(ios): download icloud files when re-indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
leizhe authored and andelf committed Jan 6, 2022
1 parent e5640bc commit c7b3bc5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 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 changes: 55 additions & 0 deletions 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 change: 1 addition & 0 deletions src/main/frontend/fs/capacitor_fs.cljs
Expand Up @@ -196,6 +196,7 @@
(.pickFolder util/folder-picker)
#(js->clj % :keywordize-keys true)
:path)
_ (when (util/native-ios?) (.downloadFilesFromiCloud util/download-icloud-files))
files (readdir path)
files (js->clj files :keywordize-keys true)]
(into [] (concat [{:path path}] files))))
Expand Down
2 changes: 2 additions & 0 deletions src/main/frontend/mobile/util.cljs
Expand Up @@ -24,6 +24,8 @@
(.isPluginAvailable Capacitor name))

(defonce folder-picker (registerPlugin "FolderPicker"))
(when (native-ios?)
(defonce download-icloud-files (registerPlugin "DownloadiCloudFiles")))
(when (native-ios?)
(defonce ios-file-container (registerPlugin "FileContainer")))

Expand Down

0 comments on commit c7b3bc5

Please sign in to comment.