Skip to content

Commit

Permalink
Added isReachable method to test connectivity
Browse files Browse the repository at this point in the history
- Fixed Cloud provider bug when moving/deleting evicted file
  • Loading branch information
amosavian committed Feb 14, 2017
1 parent 489a9a1 commit e899804
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion FileProvider.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "FileProvider"
s.version = "0.12.7"
s.version = "0.12.8"
s.summary = "FileManager replacement for Local and Remote (WebDAV/Dropbox/OneDrive/SMB2) files on iOS and macOS."

# This description is used to generate tags and improve search results.
Expand Down
4 changes: 2 additions & 2 deletions FileProvider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@
799396601D48B7BF00086753 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_VERSION_STRING = 0.12.7;
BUNDLE_VERSION_STRING = 0.12.8;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
Expand Down Expand Up @@ -627,7 +627,7 @@
799396611D48B7BF00086753 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_VERSION_STRING = 0.12.7;
BUNDLE_VERSION_STRING = 0.12.8;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
Expand Down
8 changes: 7 additions & 1 deletion Sources/CloudFileProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ open class CloudFileProvider: LocalFileProvider {
}
}

open override func isReachable(completionHandler: @escaping (Bool) -> Void) {
dispatch_queue.async {
completionHandler(self.fileManager.ubiquityIdentityToken != nil)
}
}

/**
Creates a new directory at the specified path asynchronously.
This will create any necessary intermediate directories.
Expand Down Expand Up @@ -609,7 +615,7 @@ open class CloudFileProvider: LocalFileProvider {
}

/// Returns a pulic url with expiration date, can be shared with other people.
open func temporaryLink(to path: String, completionHandler: @escaping ((_ link: URL?, _ attribute: FileObject?, _ expiration: Date?, _ error: Error?) -> Void)) {
open func publicLink(to path: String, completionHandler: @escaping ((_ link: URL?, _ attribute: FileObject?, _ expiration: Date?, _ error: Error?) -> Void)) {
operation_queue.addOperation {
do {
var expiration: NSDate?
Expand Down
6 changes: 6 additions & 0 deletions Sources/DropboxFileProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ open class DropboxFileProvider: FileProviderBasicRemote {
task.resume()
}

open func isReachable(completionHandler: @escaping (Bool) -> Void) {
self.storageProperties { total, _ in
completionHandler(total > 0)
}
}

open weak var fileOperationDelegate: FileOperationDelegate?
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/FileProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public protocol FileProviderBasic: class {
- Returns: An url, can be used to access to file directly.
*/
func url(of path: String?) -> URL

/// Checks the connection to server or permission on local
func isReachable(completionHandler: @escaping(_ success: Bool) -> Void)
}

extension FileProviderBasic {
Expand Down
17 changes: 13 additions & 4 deletions Sources/LocalFileProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
}
}

open func isReachable(completionHandler: @escaping (Bool) -> Void) {
dispatch_queue.async {
completionHandler(self.fileManager.isReadableFile(atPath: self.baseURL!.path))
}
}

open weak var fileOperationDelegate : FileOperationDelegate?

@discardableResult
Expand Down Expand Up @@ -247,8 +253,9 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
undoManager.endUndoGrouping()
}

var successfulSecurityScopedResourceAccess = false

let operationHandler: (URL, URL?) -> Void = { source, dest in
let successfulSecurityScopedResourceAccess = source.startAccessingSecurityScopedResource()
do {
switch opType {
case .create:
Expand Down Expand Up @@ -295,18 +302,20 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo

if isCoorinating {
var intents = [NSFileAccessIntent]()

successfulSecurityScopedResourceAccess = source.startAccessingSecurityScopedResource()
switch opType {
case .create, .remove, .modify:
case .create, .modify:
intents.append(NSFileAccessIntent.writingIntent(with: source, options: .forReplacing))
case .copy:
guard let dest = dest else { return nil }
intents.append(NSFileAccessIntent.readingIntent(with: source, options: forUploading ? .forUploading : .withoutChanges))
intents.append(NSFileAccessIntent.writingIntent(with: dest, options: .forReplacing))
case .move:
guard let dest = dest else { return nil }
intents.append(NSFileAccessIntent.writingIntent(with: source, options: .forDeleting))
intents.append(NSFileAccessIntent.writingIntent(with: source, options: .forMoving))
intents.append(NSFileAccessIntent.writingIntent(with: dest, options: .forReplacing))
case .remove:
intents.append(NSFileAccessIntent.writingIntent(with: source, options: .forDeleting))
default:
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions Sources/OneDriveFileProvide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ open class OneDriveFileProvider: FileProviderBasicRemote {
task.resume()
}

open func isReachable(completionHandler: @escaping (Bool) -> Void) {
let url = URL(string: "/drive/root", relativeTo: baseURL)!
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
let status = (response as? HTTPURLResponse)?.statusCode ?? 400
completionHandler(status == 200)
})
task.resume()
}

open weak var fileOperationDelegate: FileOperationDelegate?
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/SMBFileProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class SMBFileProvider: FileProvider, FileProviderMonitor {
NotImplemented()
}

func isReachable(completionHandler: @escaping (Bool) -> Void) {
NotImplemented()
}

open weak var fileOperationDelegate: FileOperationDelegate?

open func create(folder folderName: String, at atPath: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
Expand Down
13 changes: 13 additions & 0 deletions Sources/WebDAVFileProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ open class WebDAVFileProvider: FileProviderBasicRemote {
})
}

open func isReachable(completionHandler: @escaping (Bool) -> Void) {
var request = URLRequest(url: baseURL!)
request.httpMethod = "PROPFIND"
request.setValue("0", forHTTPHeaderField: "Depth")
request.setValue("text/xml; charset=\"utf-8\"", forHTTPHeaderField: "Content-Type")
request.httpBody = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop><D:quota-available-bytes/><D:quota-used-bytes/></D:prop>\n</D:propfind>".data(using: .utf8)
request.setValue(String(request.httpBody!.count), forHTTPHeaderField: "Content-Length")
runDataTask(with: request, completionHandler: { (data, response, error) in
let status = (response as? HTTPURLResponse)?.statusCode ?? 400
completionHandler(status < 300)
})
}

open weak var fileOperationDelegate: FileOperationDelegate?
}

Expand Down
Binary file modified fileprovider.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e899804

Please sign in to comment.