Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): download icloud files when re-indexing
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters