@@ -14,7 +14,7 @@ import Foundation
14
14
15
15
it uses `FileManager` foundation class with some additions like searching and reading a portion of file.
16
16
*/
17
- open class LocalFileProvider : FileProvider , FileProviderMonitor , FileProvideUndoable {
17
+ open class LocalFileProvider : FileProvider , FileProviderMonitor {
18
18
open class var type : String { return " Local " }
19
19
open fileprivate( set) var baseURL : URL ?
20
20
open var dispatch_queue : DispatchQueue
@@ -28,8 +28,9 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
28
28
open private( set) var opFileManager = FileManager ( )
29
29
fileprivate var fileProviderManagerDelegate : LocalFileProviderManagerDelegate ? = nil
30
30
31
+ #if os(macOS) || os(iOS) || os(tvOS)
31
32
open var undoManager : UndoManager ? = nil
32
-
33
+
33
34
/**
34
35
Forces file operations to use `NSFileCoordinating`, should be set `true` if:
35
36
- Files are on ubiquity (iCloud) container.
@@ -40,6 +41,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
40
41
otherwise it's `false` to accelerate operations.
41
42
*/
42
43
open var isCoorinating : Bool
44
+ #endif
43
45
44
46
/**
45
47
Initializes provider for the specified common directory in the requested domains.
@@ -53,6 +55,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
53
55
self . init ( baseURL: FileManager . default. urls ( for: directory, in: domainMask) . first!)
54
56
}
55
57
58
+ #if os(macOS) || os(iOS) || os(tvOS)
56
59
/**
57
60
Failable initializer for the specified shared container directory, allows data and files to be shared among app
58
61
and extensions regarding sandbox requirements. Container ID is same with app group specified in project `Capabilities`
@@ -89,6 +92,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
89
92
90
93
try ? fileManager. createDirectory ( at: finalBaseURL, withIntermediateDirectories: true )
91
94
}
95
+ #endif
92
96
93
97
/// Initializes provider for the specified local URL.
94
98
///
@@ -142,7 +146,9 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
142
146
public func copy( with zone: NSZone ? = nil ) -> Any {
143
147
let copy = LocalFileProvider ( baseURL: self . baseURL!)
144
148
copy. undoManager = self . undoManager
149
+ #if os(macOS) || os(iOS) || os(tvOS)
145
150
copy. isCoorinating = self . isCoorinating
151
+ #endif
146
152
copy. delegate = self . delegate
147
153
copy. fileOperationDelegate = self . fileOperationDelegate
148
154
return copy
@@ -267,12 +273,14 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
267
273
return self . doOperation ( operation, completionHandler: completionHandler)
268
274
}
269
275
276
+ #if os(macOS) || os(iOS) || os(tvOS)
270
277
@objc dynamic func doSimpleOperation( _ box: UndoBox ) {
271
278
guard let _ = self . undoManager else { return }
272
279
_ = self . doOperation ( box. undoOperation) { ( _) in
273
280
return
274
281
}
275
282
}
283
+ #endif
276
284
277
285
@discardableResult
278
286
fileprivate func doOperation( _ operation: FileOperationType , data: Data ? = nil , overwrite: Bool = true , atomically: Bool = false , forUploading: Bool = false , completionHandler: SimpleCompletionHandler ) -> Progress ? {
@@ -309,6 +317,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
309
317
return nil
310
318
}
311
319
320
+ #if os(macOS) || os(iOS) || os(tvOS)
312
321
if let undoManager = self . undoManager, let undoOp = self . undoOperation ( for: operation) {
313
322
let undoBox = UndoBox ( provider: self , operation: operation, undoOperation: undoOp)
314
323
undoManager. beginUndoGrouping ( )
@@ -318,6 +327,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
318
327
}
319
328
320
329
var successfulSecurityScopedResourceAccess = false
330
+ #endif
321
331
322
332
let operationHandler : ( URL , URL ? ) -> Void = { source, dest in
323
333
do {
@@ -350,19 +360,23 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
350
360
default :
351
361
return
352
362
}
363
+ #if os(macOS) || os(iOS) || os(tvOS)
353
364
if successfulSecurityScopedResourceAccess {
354
365
source. stopAccessingSecurityScopedResource ( )
355
366
}
367
+ #endif
356
368
357
369
progress. completedUnitCount = progress. totalUnitCount
358
370
self . dispatch_queue. async {
359
371
completionHandler ? ( nil )
360
372
}
361
373
self . delegateNotify ( operation)
362
374
} catch {
375
+ #if os(macOS) || os(iOS) || os(tvOS)
363
376
if successfulSecurityScopedResourceAccess {
364
377
source. stopAccessingSecurityScopedResource ( )
365
378
}
379
+ #endif
366
380
progress. cancel ( )
367
381
self . dispatch_queue. async {
368
382
completionHandler ? ( error)
@@ -371,6 +385,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
371
385
}
372
386
}
373
387
388
+ #if os(macOS) || os(iOS) || os(tvOS)
374
389
if isCoorinating {
375
390
successfulSecurityScopedResourceAccess = source. startAccessingSecurityScopedResource ( )
376
391
var intents = [ NSFileAccessIntent] ( )
@@ -401,6 +416,11 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
401
416
operationHandler ( source, dest)
402
417
}
403
418
}
419
+ #else
420
+ operation_queue. addOperation {
421
+ operationHandler ( source, dest)
422
+ }
423
+ #endif
404
424
return progress
405
425
}
406
426
@@ -434,6 +454,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
434
454
}
435
455
}
436
456
457
+ #if os(macOS) || os(iOS) || os(tvOS)
437
458
if isCoorinating {
438
459
let intent = NSFileAccessIntent . readingIntent ( with: url, options: . withoutChanges)
439
460
coordinated ( intents: [ intent] , operationHandler: operationHandler, errorHandler: { error in
@@ -447,6 +468,11 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
447
468
operationHandler ( url)
448
469
}
449
470
}
471
+ #else
472
+ dispatch_queue. async {
473
+ operationHandler ( url)
474
+ }
475
+ #endif
450
476
451
477
return progress
452
478
}
@@ -512,6 +538,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
512
538
}
513
539
}
514
540
541
+ #if os(macOS) || os(iOS) || os(tvOS)
515
542
if isCoorinating {
516
543
let intent = NSFileAccessIntent . readingIntent ( with: url, options: . withoutChanges)
517
544
coordinated ( intents: [ intent] , operationHandler: operationHandler, errorHandler: { error in
@@ -523,7 +550,11 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
523
550
operationHandler ( url)
524
551
}
525
552
}
526
-
553
+ #else
554
+ dispatch_queue. async {
555
+ operationHandler ( url)
556
+ }
557
+ #endif
527
558
return progress
528
559
}
529
560
@@ -618,6 +649,10 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
618
649
}
619
650
}
620
651
652
+ #if os(macOS) || os(iOS) || os(tvOS)
653
+
654
+ extension LocalFileProvider : FileProvideUndoable { }
655
+
621
656
internal extension LocalFileProvider {
622
657
func coordinated( intents: [ NSFileAccessIntent ] , operationHandler: @escaping ( _ url: URL ) -> Void , errorHandler: ( ( _ error: Error ) -> Void ) ? = nil ) {
623
658
let coordinator = NSFileCoordinator ( filePresenter: nil )
@@ -649,3 +684,5 @@ internal extension LocalFileProvider {
649
684
}
650
685
}
651
686
}
687
+ #endif
688
+
0 commit comments