|
9 | 9 | import Foundation |
10 | 10 |
|
11 | 11 | open class LocalFileProvider: FileProvider, FileProviderMonitor { |
12 | | - open static let type = "Local" |
13 | | - open var isPathRelative: Bool = true |
14 | | - open private(set) var baseURL: URL? = LocalFileProvider.defaultBaseURL() |
15 | | - open var currentPath: String = "" |
| 12 | + open static let type: String = "Local" |
| 13 | + open var isPathRelative: Bool |
| 14 | + open fileprivate(set) var baseURL: URL? |
| 15 | + open var currentPath: String |
16 | 16 | open var dispatch_queue: DispatchQueue |
17 | 17 | open var operation_queue: DispatchQueue |
18 | 18 | open weak var delegate: FileProviderDelegate? |
19 | | - open let credential: URLCredential? = nil |
| 19 | + open fileprivate(set) var credential: URLCredential? |
20 | 20 |
|
21 | 21 | open private(set) var fileManager = FileManager() |
22 | 22 | open private(set) var opFileManager = FileManager() |
23 | 23 | fileprivate var fileProviderManagerDelegate: LocalFileProviderManagerDelegate? = nil |
24 | 24 |
|
25 | | - public init () { |
26 | | - dispatch_queue = DispatchQueue(label: "FileProvider.\(LocalFileProvider.type)", attributes: DispatchQueue.Attributes.concurrent) |
27 | | - operation_queue = DispatchQueue(label: "FileProvider.\(LocalFileProvider.type).Operation", attributes: []) |
28 | | - fileProviderManagerDelegate = LocalFileProviderManagerDelegate(provider: self) |
29 | | - opFileManager.delegate = fileProviderManagerDelegate |
| 25 | + /// default values are `directory: .documentDirectory, domainMask: .userDomainMask` |
| 26 | + public convenience init (directory: FileManager.SearchPathDirectory = .documentDirectory, domainMask: FileManager.SearchPathDomainMask = .userDomainMask) { |
| 27 | + self.init(baseURL: FileManager.default.urls(for: directory, in: domainMask).first!) |
30 | 28 | } |
31 | 29 |
|
32 | 30 | public init (baseURL: URL) { |
33 | 31 | self.baseURL = baseURL |
| 32 | + self.isPathRelative = true |
| 33 | + self.currentPath = "" |
| 34 | + self.credential = nil |
| 35 | + |
34 | 36 | dispatch_queue = DispatchQueue(label: "FileProvider.\(LocalFileProvider.type)", attributes: DispatchQueue.Attributes.concurrent) |
35 | 37 | operation_queue = DispatchQueue(label: "FileProvider.\(LocalFileProvider.type).Operation", attributes: []) |
36 | 38 | fileProviderManagerDelegate = LocalFileProviderManagerDelegate(provider: self) |
37 | 39 | opFileManager.delegate = fileProviderManagerDelegate |
38 | 40 | } |
39 | 41 |
|
40 | | - open static func defaultBaseURL() -> URL { |
41 | | - let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true); |
42 | | - return URL(fileURLWithPath: paths[0]) |
| 42 | + open class func defaultBaseURL() -> URL { |
| 43 | + return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! |
43 | 44 | } |
44 | 45 |
|
45 | 46 | open func contentsOfDirectory(path: String, completionHandler: @escaping ((_ contents: [FileObject], _ error: Error?) -> Void)) { |
@@ -356,3 +357,33 @@ public extension LocalFileProvider { |
356 | 357 | } |
357 | 358 | } |
358 | 359 | } |
| 360 | + |
| 361 | +class CloudFileProvider: LocalFileProvider { |
| 362 | + // FIXME: convert static var type to class var in next Swift version |
| 363 | + |
| 364 | + open var type: String { return "iCloudDrive" } |
| 365 | + |
| 366 | + public init? (containerId: String?) { |
| 367 | + assert(!Thread.isMainThread, "LocalFileProvider.init(containerId:) is not recommended to be executed on Main Thread.") |
| 368 | + guard FileManager.default.ubiquityIdentityToken == nil else { |
| 369 | + return nil |
| 370 | + } |
| 371 | + guard let ubiquityURL = FileManager.default.url(forUbiquityContainerIdentifier: containerId) else { |
| 372 | + return nil |
| 373 | + } |
| 374 | + super.init(baseURL: ubiquityURL.appendingPathComponent("Documents")) |
| 375 | + |
| 376 | + dispatch_queue = DispatchQueue(label: "FileProvider.\(self.type)", attributes: DispatchQueue.Attributes.concurrent) |
| 377 | + operation_queue = DispatchQueue(label: "FileProvider.\(self.type).Operation", attributes: []) |
| 378 | + |
| 379 | + fileManager.url(forUbiquityContainerIdentifier: containerId) |
| 380 | + opFileManager.url(forUbiquityContainerIdentifier: containerId) |
| 381 | + fileProviderManagerDelegate = LocalFileProviderManagerDelegate(provider: self) |
| 382 | + opFileManager.delegate = fileProviderManagerDelegate |
| 383 | + } |
| 384 | + |
| 385 | + open override static func defaultBaseURL() -> URL { |
| 386 | + return FileManager.default.url(forUbiquityContainerIdentifier: nil) ?? super.defaultBaseURL() |
| 387 | + } |
| 388 | + |
| 389 | +} |
0 commit comments