Skip to content

Commit 0466f71

Browse files
committed
Fixed: OneDrive operations may fail, Made linux-ready changes.
1 parent 760229c commit 0466f71

6 files changed

+62
-5
lines changed

Docs/OAuth.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ if let refreshToken = {SAVED_REFRESH_TOKEN} {
7070
} else {
7171
_ = oauth.authorize(
7272
withCallbackURL: URL(string: "\(appScheme)://oauth-callback/onedrive")!,
73-
scope: "offline_access User.Read Files.ReadWrite Files.ReadWrite.All Files.ReadWrite.AppFolder", state: "ONEDRIVE",
73+
scope: "offline_access User.Read Files.ReadWrite.All", state: "ONEDRIVE",
7474
success: { credential, response, parameters in
7575
let credential = URLCredential(user: user ?? "anonymous", password: credential.oauthToken, persistence: .permanent)
7676
// TODO: Save refreshToken in keychain

Sources/Extensions/FoundationExtensions.swift

+7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ internal extension URL {
8282
var fileExists: Bool {
8383
return (try? self.checkResourceIsReachable()) ?? false
8484
}
85+
86+
#if os(macOS) || os(iOS) || os(tvOS)
87+
#else
88+
func checkPromisedItemIsReachable() throws -> Bool {
89+
return false
90+
}
91+
#endif
8592
}
8693

8794
public extension URLRequest {

Sources/FileProvider.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ public protocol FileProviderMonitor: FileProviderBasic {
608608
func isRegisteredForNotification(path: String) -> Bool
609609
}
610610

611+
#if os(macOS) || os(iOS) || os(tvOS)
611612
/// Allows undo file operations done by provider
612613
public protocol FileProvideUndoable: FileProviderOperations {
613614
/// To initialize undo manager either call `setupUndoManager()` or set it manually.
@@ -663,6 +664,7 @@ public extension FileProvideUndoable {
663664
self.undoManager?.levelsOfUndo = 10
664665
}
665666
}
667+
#endif
666668

667669
/// This protocol defines method to share a public link with other users
668670
public protocol FileProviderSharing {
@@ -793,7 +795,6 @@ public protocol ExtendedFileProvider: FileProviderBasic {
793795
///
794796
/// - Parameter path: path of file.
795797
/// - Returns: A `Bool` idicates path can have properties.
796-
797798
func propertiesOfFileSupported(path: String) -> Bool
798799

799800
/**

Sources/LocalFileProvider.swift

+40-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Foundation
1414

1515
it uses `FileManager` foundation class with some additions like searching and reading a portion of file.
1616
*/
17-
open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndoable {
17+
open class LocalFileProvider: FileProvider, FileProviderMonitor {
1818
open class var type: String { return "Local" }
1919
open fileprivate(set) var baseURL: URL?
2020
open var dispatch_queue: DispatchQueue
@@ -28,8 +28,9 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
2828
open private(set) var opFileManager = FileManager()
2929
fileprivate var fileProviderManagerDelegate: LocalFileProviderManagerDelegate? = nil
3030

31+
#if os(macOS) || os(iOS) || os(tvOS)
3132
open var undoManager: UndoManager? = nil
32-
33+
3334
/**
3435
Forces file operations to use `NSFileCoordinating`, should be set `true` if:
3536
- Files are on ubiquity (iCloud) container.
@@ -40,6 +41,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
4041
otherwise it's `false` to accelerate operations.
4142
*/
4243
open var isCoorinating: Bool
44+
#endif
4345

4446
/**
4547
Initializes provider for the specified common directory in the requested domains.
@@ -53,6 +55,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
5355
self.init(baseURL: FileManager.default.urls(for: directory, in: domainMask).first!)
5456
}
5557

58+
#if os(macOS) || os(iOS) || os(tvOS)
5659
/**
5760
Failable initializer for the specified shared container directory, allows data and files to be shared among app
5861
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
8992

9093
try? fileManager.createDirectory(at: finalBaseURL, withIntermediateDirectories: true)
9194
}
95+
#endif
9296

9397
/// Initializes provider for the specified local URL.
9498
///
@@ -142,7 +146,9 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
142146
public func copy(with zone: NSZone? = nil) -> Any {
143147
let copy = LocalFileProvider(baseURL: self.baseURL!)
144148
copy.undoManager = self.undoManager
149+
#if os(macOS) || os(iOS) || os(tvOS)
145150
copy.isCoorinating = self.isCoorinating
151+
#endif
146152
copy.delegate = self.delegate
147153
copy.fileOperationDelegate = self.fileOperationDelegate
148154
return copy
@@ -267,12 +273,14 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
267273
return self.doOperation(operation, completionHandler: completionHandler)
268274
}
269275

276+
#if os(macOS) || os(iOS) || os(tvOS)
270277
@objc dynamic func doSimpleOperation(_ box: UndoBox) {
271278
guard let _ = self.undoManager else { return }
272279
_ = self.doOperation(box.undoOperation) { (_) in
273280
return
274281
}
275282
}
283+
#endif
276284

277285
@discardableResult
278286
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
309317
return nil
310318
}
311319

320+
#if os(macOS) || os(iOS) || os(tvOS)
312321
if let undoManager = self.undoManager, let undoOp = self.undoOperation(for: operation) {
313322
let undoBox = UndoBox(provider: self, operation: operation, undoOperation: undoOp)
314323
undoManager.beginUndoGrouping()
@@ -318,6 +327,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
318327
}
319328

320329
var successfulSecurityScopedResourceAccess = false
330+
#endif
321331

322332
let operationHandler: (URL, URL?) -> Void = { source, dest in
323333
do {
@@ -350,19 +360,23 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
350360
default:
351361
return
352362
}
363+
#if os(macOS) || os(iOS) || os(tvOS)
353364
if successfulSecurityScopedResourceAccess {
354365
source.stopAccessingSecurityScopedResource()
355366
}
367+
#endif
356368

357369
progress.completedUnitCount = progress.totalUnitCount
358370
self.dispatch_queue.async {
359371
completionHandler?(nil)
360372
}
361373
self.delegateNotify(operation)
362374
} catch {
375+
#if os(macOS) || os(iOS) || os(tvOS)
363376
if successfulSecurityScopedResourceAccess {
364377
source.stopAccessingSecurityScopedResource()
365378
}
379+
#endif
366380
progress.cancel()
367381
self.dispatch_queue.async {
368382
completionHandler?(error)
@@ -371,6 +385,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
371385
}
372386
}
373387

388+
#if os(macOS) || os(iOS) || os(tvOS)
374389
if isCoorinating {
375390
successfulSecurityScopedResourceAccess = source.startAccessingSecurityScopedResource()
376391
var intents = [NSFileAccessIntent]()
@@ -401,6 +416,11 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
401416
operationHandler(source, dest)
402417
}
403418
}
419+
#else
420+
operation_queue.addOperation {
421+
operationHandler(source, dest)
422+
}
423+
#endif
404424
return progress
405425
}
406426

@@ -434,6 +454,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
434454
}
435455
}
436456

457+
#if os(macOS) || os(iOS) || os(tvOS)
437458
if isCoorinating {
438459
let intent = NSFileAccessIntent.readingIntent(with: url, options: .withoutChanges)
439460
coordinated(intents: [intent], operationHandler: operationHandler, errorHandler: { error in
@@ -447,6 +468,11 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
447468
operationHandler(url)
448469
}
449470
}
471+
#else
472+
dispatch_queue.async {
473+
operationHandler(url)
474+
}
475+
#endif
450476

451477
return progress
452478
}
@@ -512,6 +538,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
512538
}
513539
}
514540

541+
#if os(macOS) || os(iOS) || os(tvOS)
515542
if isCoorinating {
516543
let intent = NSFileAccessIntent.readingIntent(with: url, options: .withoutChanges)
517544
coordinated(intents: [intent], operationHandler: operationHandler, errorHandler: { error in
@@ -523,7 +550,11 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
523550
operationHandler(url)
524551
}
525552
}
526-
553+
#else
554+
dispatch_queue.async {
555+
operationHandler(url)
556+
}
557+
#endif
527558
return progress
528559
}
529560

@@ -618,6 +649,10 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
618649
}
619650
}
620651

652+
#if os(macOS) || os(iOS) || os(tvOS)
653+
654+
extension LocalFileProvider: FileProvideUndoable { }
655+
621656
internal extension LocalFileProvider {
622657
func coordinated(intents: [NSFileAccessIntent], operationHandler: @escaping (_ url: URL) -> Void, errorHandler: ((_ error: Error) -> Void)? = nil) {
623658
let coordinator = NSFileCoordinator(filePresenter: nil)
@@ -649,3 +684,5 @@ internal extension LocalFileProvider {
649684
}
650685
}
651686
}
687+
#endif
688+

Sources/LocalHelper.swift

+2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ internal class LocalFileProviderManagerDelegate: NSObject, FileManagerDelegate {
224224
}
225225
}
226226

227+
#if os(macOS) || os(iOS) || os(tvOS)
227228
class UndoBox: NSObject {
228229
weak var provider: FileProvideUndoable?
229230
let operation: FileOperationType
@@ -235,3 +236,4 @@ class UndoBox: NSObject {
235236
self.undoOperation = undoOperation
236237
}
237238
}
239+
#endif

Sources/OneDriveFileProvider.swift

+10
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,16 @@ open class OneDriveFileProvider: HTTPFileProvider, FileProviderSharing {
331331
}, completionHandler: completionHandler)
332332
}
333333

334+
/**
335+
Returns an independent url to access the file. Some providers like `Dropbox` due to their nature.
336+
don't return an absolute url to be used to access file directly.
337+
- Parameter path: Relative path of file or directory.
338+
- Returns: An url, can be used to access to file directly.
339+
*/
340+
open override func url(of path: String) -> URL {
341+
return OneDriveFileObject.url(of: path, modifier: nil, baseURL: baseURL!, route: route)
342+
}
343+
334344
/**
335345
Returns an independent url to access the file. Some providers like `Dropbox` due to their nature.
336346
don't return an absolute url to be used to access file directly.

0 commit comments

Comments
 (0)