Compare commits

...

8 Commits

Author SHA1 Message Date
Amir Abbas Mousavian eccbeb7174 Fixed WebDAV connection error for Apache servers 2016-08-02 14:59:32 +04:30
Amir Abbas Mousavian a077d000bc Updated Readme 2016-07-31 01:27:13 +04:30
Amir Abbas Mousavian 6a3ea633bf WebDAV file protocol conformance to FileProvider 2016-07-29 10:57:18 +04:30
Amir Abbas Mousavian 3ca26ad3df pod 0.3.2 2016-07-28 15:10:17 +04:30
Amir Abbas Mousavian 364b93c6fd Modifier correction 2016-07-28 14:31:48 +04:30
Amir Abbas Mousavian 506952be13 Corrected modifiers and updated pod to 0.3.1 2016-07-28 14:26:37 +04:30
Amir Abbas Mousavian 69ca35f0ae Source compatibility with Swift 2.3 2016-07-27 16:12:11 +04:30
Amir Abbas Mousavian dcbe3c1c3e Updated sourcefiles tag 2016-07-27 15:19:41 +04:30
12 changed files with 201 additions and 139 deletions
+2 -2
View File
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#
s.name = "FileProvider"
s.version = "0.3.0"
s.version = "0.3.3"
s.summary = "NSFileManager replacement for Local and Remote (WebDAV/Dropbox/SMB2) files on iOS and MacOS."
# This description is used to generate tags and improve search results.
@@ -93,7 +93,7 @@ Pod::Spec.new do |s|
# Not including the public_header_files will make all headers public.
#
s.source_files = "Sources/*.swift"
s.source_files = "Sources/**/*.swift"
s.exclude_files = "Sources/Exclude"
# s.public_header_files = "Classes/**/*.h"
+8 -5
View File
@@ -170,16 +170,16 @@
isa = PBXGroup;
children = (
7993969A1D48C02300086753 /* CIFSTypes.swift */,
799396A41D48C02300086753 /* SMBErrorType.swift */,
7993969B1D48C02300086753 /* SMB2DataTypes.swift */,
7993969C1D48C02300086753 /* SMB2FileHandle.swift */,
799396A31D48C02300086753 /* SMB2Types.swift */,
799396A21D48C02300086753 /* SMB2Tree.swift */,
799396A01D48C02300086753 /* SMB2Session.swift */,
7993969D1D48C02300086753 /* SMB2FileOperation.swift */,
7993969C1D48C02300086753 /* SMB2FileHandle.swift */,
7993969E1D48C02300086753 /* SMB2IOCtl.swift */,
7993969F1D48C02300086753 /* SMB2Query.swift */,
799396A01D48C02300086753 /* SMB2Session.swift */,
799396A11D48C02300086753 /* SMB2SetInfo.swift */,
799396A21D48C02300086753 /* SMB2Tree.swift */,
799396A31D48C02300086753 /* SMB2Types.swift */,
799396A41D48C02300086753 /* SMBErrorType.swift */,
);
path = SMBTypes;
sourceTree = "<group>";
@@ -778,6 +778,7 @@
7993966E1D48B7F600086753 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
7993967A1D48B80D00086753 /* Build configuration list for PBXNativeTarget "FileProvider OSX" */ = {
isa = XCConfigurationList;
@@ -786,6 +787,7 @@
7993967C1D48B80D00086753 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
799396871D48B82700086753 /* Build configuration list for PBXNativeTarget "FileProvider tvOS" */ = {
isa = XCConfigurationList;
@@ -794,6 +796,7 @@
799396891D48B82700086753 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
+16 -7
View File
@@ -30,7 +30,7 @@ Local and WebDAV providers are fully tested and can be used in production enviro
## Requirements
- **Swift 2.2**
- **Swift 2.2 or 2.3**
- iOS 8.0 , OSX 10.10
- XCode 7.3
@@ -81,7 +81,9 @@ You can't change the base url later. and all paths are related to this base url
For remote file providers authentication may be necessary:
let credential = NSURLCredential(user: "user", password: "pass", persistence: NSURLCredentialPersistence.Permanent)
let webdavProvider = WebDAVFileProvider(baseURL: "http://www.example.com/dav", credential: credential)
let webdavProvider = WebDAVFileProvider(baseURL: NSURL(string: "http://www.example.com/dav")!, credential: credential)
* In case you want to connect non-secure servers for WebDAV (http) in iOS 9+ / macOS 10.11+ you should disable App Transport Security (ATS) according to [this guide.](https://gist.github.com/mlynch/284699d676fe9ed0abfa)
* For Dropbox, user is clientID and password is Token which both must be retrieved via [OAuth2 API of Dropbox](https://www.dropbox.com/developers/reference/oauth-guide). There are libraries like [p2/OAuth2](https://github.com/p2/OAuth2) or [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift) which can facilate the procedure to retrieve token.
@@ -101,7 +103,7 @@ Your class should conforms `FileProviderDelegate` class:
documentsProvider.delegate = self
}
func fileproviderSucceed(fileProvider: FileProvider, operation: FileOperation) {
func fileproviderSucceed(fileProvider: FileProviderOperations, operation: FileOperation) {
switch operation {
case .Copy(source: let source, destination: let dest):
NSLog("\(source) copied to \(dest).")
@@ -112,7 +114,7 @@ Your class should conforms `FileProviderDelegate` class:
}
}
func fileproviderFailed(fileProvider: FileProvider, operation: FileOperation) {
func fileproviderFailed(fileProvider: FileProviderOperations, operation: FileOperation) {
switch operation {
case .Copy(source: let source, destination: let dest):
NSLog("copy of \(source) failed.")
@@ -123,7 +125,7 @@ Your class should conforms `FileProviderDelegate` class:
}
}
func fileproviderProgress(fileProvider: FileProvider, operation: FileOperation, progress: Float) {
func fileproviderProgress(fileProvider: FileProviderOperations, operation: FileOperation, progress: Float) {
switch operation {
case .Copy(source: let source, destination: let dest):
NSLog("Copy\(source) to \(dest): \(progress * 100) completed.")
@@ -211,7 +213,7 @@ Move file old.txt to new.txt in current path:
### Retrieve Content of File
THere is two method for this purpose, one of them loads entire file into NSData and another can load a portion of file.
There is two method for this purpose, one of them loads entire file into NSData and another can load a portion of file.
documentsProvider.contentsAtPath(path: "old.txt", completionHandler: {
(contents: NSData?, error: ErrorType?) -> Void
@@ -252,6 +254,13 @@ You can monitor updates in some file system (Local and SMB2), there is three met
We would love for you to contribute to **FileProvider**, check the `LICENSE` file for more info.
## Projects in use
* [EDM - Browse and Receive Files](https://itunes.apple.com/us/app/edm-browse-and-receive-files/id948397575?ls=1&mt=8)
* [File Manager - PDF Reader & Music Player](https://itunes.apple.com/us/app/file-manager-pdf-reader-music/id1017809685?ls=1&mt=8)
If you used this library in your project, you can open an issue to inform us.
## Meta
Amir-Abbas Mousavian [@amosavian](https://twitter.com/amosavian)
@@ -260,7 +269,7 @@ Distributed under the MIT license. See `LICENSE` for more information.
[https://github.com/yourname/github-link](https://github.com/dbader/)
[swift-image]:https://img.shields.io/badge/swift-2.2-green.svg
[swift-image]:https://img.shields.io/badge/swift-2.2%2C%202.3-green.svg
[swift-url]: https://swift.org/
[license-image]: https://img.shields.io/badge/License-MIT-blue.svg
[license-url]: LICENSE
+37 -29
View File
@@ -19,8 +19,8 @@ public enum FileProviderDropboxErrorCode: Int {
}
public struct FileProviderDropboxError: ErrorType, CustomStringConvertible {
let code: FileProviderDropboxErrorCode
let path: String
public let code: FileProviderDropboxErrorCode
public let path: String
public var description: String {
switch code {
@@ -36,11 +36,11 @@ public struct FileProviderDropboxError: ErrorType, CustomStringConvertible {
}
public final class DropboxFileObject: FileObject {
let serverTime: NSDate?
let id: String?
let rev: String?
public let serverTime: NSDate?
public let id: String?
public let rev: String?
init(absoluteURL: NSURL, name: String, path: String, size: Int64, serverTime: NSDate?, createdDate: NSDate?, modifiedDate: NSDate?, fileType: FileType, isHidden: Bool, isReadOnly: Bool, id: String?, rev: String?) {
public init(absoluteURL: NSURL, name: String, path: String, size: Int64, serverTime: NSDate?, createdDate: NSDate?, modifiedDate: NSDate?, fileType: FileType, isHidden: Bool, isReadOnly: Bool, id: String?, rev: String?) {
self.serverTime = serverTime
self.id = id
self.rev = rev
@@ -74,13 +74,13 @@ public class DropboxFileProvider: NSObject, FileProviderBasic {
return _session!
}
init? (baseURL: NSURL, credential: NSURLCredential?) {
if !["http", "https"].contains(baseURL.scheme.lowercaseString) {
public init? (baseURL: NSURL, credential: NSURLCredential?) {
if !["http", "https"].contains(baseURL.uw_scheme.lowercaseString) {
return nil
}
self.baseURL = baseURL
dispatch_queue = dispatch_queue_create("FileProvider.\(type)", DISPATCH_QUEUE_CONCURRENT)
//let url = baseURL.absoluteString
//let url = baseURL.uw_absoluteString
self.credential = credential
}
@@ -132,7 +132,7 @@ extension DropboxFileProvider: FileProviderOperations {
}
public func createFile(fileAttribs: FileObject, atPath path: String, contents data: NSData?, completionHandler: SimpleCompletionHandler) {
NotImplemented()
self.writeContentsAtPath(path, contents: data ?? NSData(), completionHandler: completionHandler)
}
public func moveItemAtPath(path: String, toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) {
@@ -203,27 +203,34 @@ extension DropboxFileProvider: FileProviderOperations {
request.HTTPMethod = "PUT"
let task = session.uploadTaskWithRequest(request, fromFile: localFile) { (data, response, error) in
completionHandler?(error: error)
self.delegateNotify(.Move(source: localFile.absoluteString, destination: toPath), error: error)
self.delegateNotify(.Move(source: localFile.uw_absoluteString, destination: toPath), error: error)
}
task.taskDescription = self.dictionaryToJSON(["type": "Copy", "source": localFile.absoluteString, "dest": toPath])
task.taskDescription = self.dictionaryToJSON(["type": "Copy", "source": localFile.uw_absoluteString, "dest": toPath])
task.resume()
}
public func copyPathToLocalFile(path: String, toLocalURL: NSURL, completionHandler: SimpleCompletionHandler) {
NotImplemented()
let request = NSMutableURLRequest(URL: absoluteURL(path))
let task = session.downloadTaskWithRequest(request) { (sourceFileURL, response, error) in
if let sourceFileURL = sourceFileURL {
do {
try NSFileManager.defaultManager().copyItemAtURL(sourceFileURL, toURL: toLocalURL)
} catch let e {
completionHandler?(error: e)
return
}
public func copyPathToLocalFile(path: String, toLocalURL destURL: NSURL, completionHandler: SimpleCompletionHandler) {
let url = NSURL(string: "https://api.dropboxapi.com/2/files/download")!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let requestDictionary = ["path": path]
request.setValue(dictionaryToJSON(requestDictionary), forHTTPHeaderField: "Dropbox-API-Arg")
let task = session.downloadTaskWithRequest(request, completionHandler: { (cacheURL, response, error) in
guard let cacheURL = cacheURL, let httpResponse = response as? NSHTTPURLResponse where httpResponse.statusCode < 300 else {
let code = FileProviderDropboxErrorCode(rawValue: (response as? NSHTTPURLResponse)?.statusCode ?? -1)
let dbError: FileProviderDropboxError? = code != nil ? FileProviderDropboxError(code: code!, path: path) : nil
completionHandler?(error: dbError ?? error)
return
}
completionHandler?(error: error)
}
task.taskDescription = self.dictionaryToJSON(["type": "Copy", "source": path, "dest": toLocalURL.absoluteString])
do {
try NSFileManager.defaultManager().moveItemAtURL(cacheURL, toURL: destURL)
completionHandler?(error: nil)
} catch let e {
completionHandler?(error: e)
}
})
task.resume()
}
}
@@ -234,7 +241,6 @@ extension DropboxFileProvider: FileProviderReadWrite {
}
public func contentsAtPath(path: String, offset: Int64, length: Int, completionHandler: ((contents: NSData?, error: ErrorType?) -> Void)) {
let url = NSURL(string: "https://api.dropboxapi.com/2/files/download")!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
@@ -254,7 +260,7 @@ extension DropboxFileProvider: FileProviderReadWrite {
completionHandler(contents: nil, error: dbError ?? error)
return
}
let destURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(cacheURL.lastPathComponent ?? "tmpfile")
let destURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).uw_URLByAppendingPathComponent(cacheURL.lastPathComponent ?? "tmpfile")
do {
try NSFileManager.defaultManager().moveItemAtURL(cacheURL, toURL: destURL)
completionHandler(contents: NSData(contentsOfURL: destURL), error: error)
@@ -267,7 +273,7 @@ extension DropboxFileProvider: FileProviderReadWrite {
public func writeContentsAtPath(path: String, contents data: NSData, atomically: Bool = false, completionHandler: SimpleCompletionHandler) {
NotImplemented()
let url = atomically ? absoluteURL(path).URLByAppendingPathExtension("tmp") : absoluteURL(path)
let url = atomically ? absoluteURL(path).uw_URLByAppendingPathExtension("tmp") : absoluteURL(path)
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "PUT"
let task = session.uploadTaskWithRequest(request, fromData: data) { (data, response, error) in
@@ -335,6 +341,8 @@ internal extension DropboxFileProvider {
}
}
extension DropboxFileProvider: FileProvider {}
// MARK: URLSession delegate
extension DropboxFileProvider: NSURLSessionDataDelegate, NSURLSessionDownloadDelegate {
public func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
+64 -28
View File
@@ -25,7 +25,7 @@ public enum FileType: String {
case NamedPipe
case Unknown
init(urlResourceTypeValue: String) {
public init(urlResourceTypeValue: String) {
switch urlResourceTypeValue {
case NSURLFileResourceTypeNamedPipe: self = .NamedPipe
case NSURLFileResourceTypeCharacterSpecial: self = .CharacterSpecial
@@ -39,7 +39,7 @@ public enum FileType: String {
}
}
init(fileTypeValue: String) {
public init(fileTypeValue: String) {
switch fileTypeValue {
case NSFileTypeCharacterSpecial: self = .CharacterSpecial
case NSFileTypeDirectory: self = .Directory
@@ -53,7 +53,7 @@ public enum FileType: String {
}
}
protocol FoundationErrorEnum {
public protocol FoundationErrorEnum {
init? (rawValue: Int)
var rawValue: Int { get }
}
@@ -62,17 +62,17 @@ extension NSURLError: FoundationErrorEnum {}
extension NSCocoaError: FoundationErrorEnum {}
public class FileObject {
let absoluteURL: NSURL?
let name: String
let path: String
let size: Int64
let createdDate: NSDate?
let modifiedDate: NSDate?
let fileType: FileType
let isHidden: Bool
let isReadOnly: Bool
public let absoluteURL: NSURL?
public let name: String
public let path: String
public let size: Int64
public let createdDate: NSDate?
public let modifiedDate: NSDate?
public let fileType: FileType
public let isHidden: Bool
public let isReadOnly: Bool
init(absoluteURL: NSURL?, name: String, path: String, size: Int64, createdDate: NSDate?, modifiedDate: NSDate?, fileType: FileType, isHidden: Bool, isReadOnly: Bool) {
public init(absoluteURL: NSURL?, name: String, path: String, size: Int64, createdDate: NSDate?, modifiedDate: NSDate?, fileType: FileType, isHidden: Bool, isReadOnly: Bool) {
self.absoluteURL = absoluteURL
self.name = name
self.path = path
@@ -84,7 +84,7 @@ public class FileObject {
self.isReadOnly = isReadOnly
}
init(name: String, path: String, createdDate: NSDate?, modifiedDate: NSDate?, isHidden: Bool, isReadOnly: Bool) {
public init(name: String, path: String, createdDate: NSDate?, modifiedDate: NSDate?, isHidden: Bool, isReadOnly: Bool) {
self.absoluteURL = nil
self.name = name
self.path = path
@@ -96,11 +96,11 @@ public class FileObject {
self.isReadOnly = isReadOnly
}
var isDirectory: Bool {
public var isDirectory: Bool {
return self.fileType == .Directory
}
var isSymLink: Bool {
public var isSymLink: Bool {
return self.fileType == .SymbolicLink
}
}
@@ -156,7 +156,7 @@ public protocol FileProvider: FileProviderBasic, FileProviderOperations, FilePro
}
extension FileProviderBasic {
var bareCurrentPath: String {
public var bareCurrentPath: String {
return currentPath.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: ". /"))
}
@@ -168,12 +168,12 @@ extension FileProviderBasic {
rpath = self.currentPath
}
if isPathRelative, let baseURL = baseURL {
if rpath.hasPrefix("/") && baseURL.absoluteString.hasSuffix("/") {
if rpath.hasPrefix("/") && baseURL.uw_absoluteString.hasSuffix("/") {
var npath = rpath
npath.removeAtIndex(npath.startIndex)
return baseURL.URLByAppendingPathComponent(npath)
return baseURL.uw_URLByAppendingPathComponent(npath)
} else {
return baseURL.URLByAppendingPathComponent(rpath)
return baseURL.uw_URLByAppendingPathComponent(rpath)
}
} else {
return NSURL(fileURLWithPath: rpath).URLByStandardizingPath!
@@ -181,8 +181,8 @@ extension FileProviderBasic {
}
public func relativePathOf(url url: NSURL) -> String {
guard let baseURL = self.baseURL else { return url.absoluteString }
return url.URLByStandardizingPath!.absoluteString.stringByReplacingOccurrencesOfString(baseURL.absoluteString, withString: "/").stringByRemovingPercentEncoding!
guard let baseURL = self.baseURL else { return url.uw_absoluteString }
return url.URLByStandardizingPath!.uw_absoluteString.stringByReplacingOccurrencesOfString(baseURL.uw_absoluteString, withString: "/").stringByRemovingPercentEncoding!
}
internal func correctPath(path: String?) -> String? {
@@ -237,7 +237,7 @@ extension FileProviderBasic {
default:
domain = NSCocoaErrorDomain
}
return NSError(domain: domain, code: code.rawValue, userInfo: [NSURLErrorFailingURLErrorKey: fileURL, NSURLErrorFailingURLStringErrorKey: fileURL.absoluteString])
return NSError(domain: domain, code: code.rawValue, userInfo: [NSURLErrorFailingURLErrorKey: fileURL, NSURLErrorFailingURLStringErrorKey: fileURL.uw_absoluteString])
}
internal func NotImplemented() {
@@ -293,7 +293,7 @@ public protocol ExtendedFileProvider: FileProvider {
func propertiesOfFileAtPath(path: String, completionHandler: ((propertiesDictionary: [String: AnyObject], keys: [String], error: ErrorType?) -> Void))
}
public enum FileOperation {
public enum FileOperation: CustomStringConvertible {
case Create (path: String)
case Copy (source: String, destination: String)
case Move (source: String, destination: String)
@@ -301,7 +301,7 @@ public enum FileOperation {
case Remove (path: String)
case Link (link: String, target: String)
var description: String {
public var description: String {
switch self {
case .Create(path: _): return "Create"
case .Copy(source: _, destination: _): return "Copy"
@@ -312,7 +312,7 @@ public enum FileOperation {
}
}
var actionDescription: String {
internal var actionDescription: String {
switch self {
case .Create(path: _): return "Creating"
case .Copy(source: _, destination: _): return "Copying"
@@ -336,6 +336,42 @@ public protocol FileOperationDelegate: class {
/// fileProvider(_:shouldOperate:) gives the delegate an opportunity to filter the file operation. Returning true from this method will allow the copy to happen. Returning false from this method causes the item in question to be skipped. If the item skipped was a directory, no children of that directory will be subject of the operation, nor will the delegate be notified of those children.
func fileProvider(fileProvider: FileProviderOperations, shouldDoOperation operation: FileOperation) -> Bool
/// fileProvider:shouldProceedAfterError:copyingItemAtPath:toPath: gives the delegate an opportunity to recover from or continue copying after an error. If an error occurs, the error object will contain an ErrorType indicating the problem. The source path and destination paths are also provided. If this method returns true, the FileProvider instance will continue as if the error had not occurred. If this method returns false, the NSFileManager instance will stop copying, return false from copyItemAtPath:toPath:error: and the error will be provied there.
/// fileProvider(_:shouldProceedAfterError:copyingItemAtPath:toPath:) gives the delegate an opportunity to recover from or continue copying after an error. If an error occurs, the error object will contain an ErrorType indicating the problem. The source path and destination paths are also provided. If this method returns true, the FileProvider instance will continue as if the error had not occurred. If this method returns false, the NSFileManager instance will stop copying, return false from copyItemAtPath:toPath:error: and the error will be provied there.
func fileProvider(fileProvider: FileProviderOperations, shouldProceedAfterError error: ErrorType, operation: FileOperation) -> Bool
}
}
// THESE ARE METHODS TO PROVIDE COMPATIBILITY WITH SWIFT 2.3 SIMOULTANIOUSLY!
internal extension NSURL {
var uw_scheme: String {
#if swift(>=2.3)
return self.scheme ?? ""
#else
return self.scheme
#endif
}
var uw_absoluteString: String {
#if swift(>=2.3)
return self.absoluteString ?? ""
#else
return self.absoluteString
#endif
}
func uw_URLByAppendingPathComponent(pathComponent: String) -> NSURL {
#if swift(>=2.3)
return self.URLByAppendingPathComponent(pathComponent)!
#else
return self.URLByAppendingPathComponent(pathComponent)
#endif
}
func uw_URLByAppendingPathExtension(pathExtension: String) -> NSURL {
#if swift(>=2.3)
return self.URLByAppendingPathExtension(pathExtension)!
#else
return self.URLByAppendingPathExtension(pathExtension)
#endif
}
}
+13 -12
View File
@@ -9,9 +9,9 @@
import Foundation
public final class LocalFileObject: FileObject {
let allocatedSize: Int64
public let allocatedSize: Int64
init(absoluteURL: NSURL, name: String, path: String, size: Int64, allocatedSize: Int64, createdDate: NSDate?, modifiedDate: NSDate?, fileType: FileType, isHidden: Bool, isReadOnly: Bool) {
public init(absoluteURL: NSURL, name: String, path: String, size: Int64, allocatedSize: Int64, createdDate: NSDate?, modifiedDate: NSDate?, fileType: FileType, isHidden: Bool, isReadOnly: Bool) {
self.allocatedSize = allocatedSize
super.init(absoluteURL: absoluteURL, name: name, path: path, size: size, createdDate: createdDate, modifiedDate: modifiedDate, fileType: fileType, isHidden: isHidden, isReadOnly: isReadOnly)
}
@@ -31,14 +31,14 @@ public class LocalFileProvider: FileProvider, FileProviderMonitor {
public let opFileManager = NSFileManager()
private var fileProviderManagerDelegate: LocalFileProviderManagerDelegate? = nil
init () {
public init () {
dispatch_queue = dispatch_queue_create("FileProvider.\(type)", DISPATCH_QUEUE_CONCURRENT)
operation_queue = dispatch_queue_create("FileProvider.\(type).Operation", DISPATCH_QUEUE_SERIAL)
fileProviderManagerDelegate = LocalFileProviderManagerDelegate(provider: self)
opFileManager.delegate = fileProviderManagerDelegate
}
init (baseURL: NSURL) {
public init (baseURL: NSURL) {
self.baseURL = baseURL
dispatch_queue = dispatch_queue_create("FileProvider.\(type)", DISPATCH_QUEUE_CONCURRENT)
operation_queue = dispatch_queue_create("FileProvider.\(type).Operation", DISPATCH_QUEUE_SERIAL)
@@ -96,7 +96,7 @@ public class LocalFileProvider: FileProvider, FileProviderMonitor {
public func createFolder(folderName: String, atPath: String, completionHandler: SimpleCompletionHandler) {
dispatch_async(operation_queue) {
do {
try self.opFileManager.createDirectoryAtURL(self.absoluteURL(atPath).URLByAppendingPathComponent(folderName), withIntermediateDirectories: true, attributes: [:])
try self.opFileManager.createDirectoryAtURL(self.absoluteURL(atPath).uw_URLByAppendingPathComponent(folderName), withIntermediateDirectories: true, attributes: [:])
completionHandler?(error: nil)
dispatch_async(dispatch_get_main_queue(), {
self.delegate?.fileproviderSucceed(self, operation: .Create(path: (atPath as NSString).stringByAppendingPathComponent(folderName) + "/"))
@@ -112,7 +112,7 @@ public class LocalFileProvider: FileProvider, FileProviderMonitor {
public func createFile(fileAttribs: FileObject, atPath: String, contents data: NSData?, completionHandler: SimpleCompletionHandler) {
dispatch_async(operation_queue) {
let fileURL = self.absoluteURL(atPath).URLByAppendingPathComponent(fileAttribs.name)
let fileURL = self.absoluteURL(atPath).uw_URLByAppendingPathComponent(fileAttribs.name)
var attributes = [String : AnyObject]()
if let createdDate = fileAttribs.createdDate {
attributes[NSFileCreationDate] = createdDate
@@ -208,12 +208,12 @@ public class LocalFileProvider: FileProvider, FileProviderMonitor {
try self.opFileManager.copyItemAtURL(localFile, toURL: self.absoluteURL(toPath))
completionHandler?(error: nil)
dispatch_async(dispatch_get_main_queue(), {
self.delegate?.fileproviderSucceed(self, operation: .Copy(source: localFile.absoluteString, destination: toPath))
self.delegate?.fileproviderSucceed(self, operation: .Copy(source: localFile.uw_absoluteString, destination: toPath))
})
} catch let e as NSError {
completionHandler?(error: e)
dispatch_async(dispatch_get_main_queue(), {
self.delegate?.fileproviderFailed(self, operation: .Copy(source: localFile.absoluteString, destination: toPath))
self.delegate?.fileproviderFailed(self, operation: .Copy(source: localFile.uw_absoluteString, destination: toPath))
})
}
}
@@ -225,12 +225,12 @@ public class LocalFileProvider: FileProvider, FileProviderMonitor {
try self.opFileManager.copyItemAtURL(self.absoluteURL(path), toURL: toLocalURL)
completionHandler?(error: nil)
dispatch_async(dispatch_get_main_queue(), {
self.delegate?.fileproviderSucceed(self, operation: .Copy(source: path, destination: toLocalURL.absoluteString))
self.delegate?.fileproviderSucceed(self, operation: .Copy(source: path, destination: toLocalURL.uw_absoluteString))
})
} catch let e as NSError {
completionHandler?(error: e)
dispatch_async(dispatch_get_main_queue(), {
self.delegate?.fileproviderFailed(self, operation: .Copy(source: path, destination: toLocalURL.absoluteString))
self.delegate?.fileproviderFailed(self, operation: .Copy(source: path, destination: toLocalURL.uw_absoluteString))
})
}
}
@@ -324,6 +324,7 @@ public class LocalFileProvider: FileProvider, FileProviderMonitor {
for (i, monitor) in monitors.enumerate() {
if self.relativePathOf(url: monitor.url) == path {
removedMonitor = monitors.removeAtIndex(i)
break
}
}
removedMonitor?.stop()
@@ -334,7 +335,7 @@ public class LocalFileProvider: FileProvider, FileProviderMonitor {
}
}
extension LocalFileProvider {
public extension LocalFileProvider {
public func createSymbolicLinkAtPath(path: String, withDestinationPath destPath: String, completionHandler: SimpleCompletionHandler) {
dispatch_async(operation_queue) {
do {
@@ -353,7 +354,7 @@ extension LocalFileProvider {
}
}
class LocalFileProviderManagerDelegate: NSObject, NSFileManagerDelegate {
internal class LocalFileProviderManagerDelegate: NSObject, NSFileManagerDelegate {
weak var provider: LocalFileProvider?
init(provider: LocalFileProvider) {
+32 -32
View File
@@ -8,111 +8,111 @@
import Foundation
class SMBFileProvider: FileProvider, FileProviderMonitor {
var type: String = "Samba"
var isPathRelative: Bool = true
var baseURL: NSURL?
var currentPath: String = ""
var dispatch_queue: dispatch_queue_t
weak var delegate: FileProviderDelegate?
let credential: NSURLCredential?
public class SMBFileProvider: FileProvider, FileProviderMonitor {
public var type: String = "Samba"
public var isPathRelative: Bool = true
public var baseURL: NSURL?
public var currentPath: String = ""
public var dispatch_queue: dispatch_queue_t
public weak var delegate: FileProviderDelegate?
public let credential: NSURLCredential?
typealias FileObjectClass = FileObject
public typealias FileObjectClass = FileObject
init? (baseURL: NSURL, credential: NSURLCredential, afterInitialized: SimpleCompletionHandler) {
guard baseURL.scheme.lowercaseString == "smb" else {
public init? (baseURL: NSURL, credential: NSURLCredential, afterInitialized: SimpleCompletionHandler) {
guard baseURL.uw_scheme.lowercaseString == "smb" else {
return nil
}
self.baseURL = baseURL
dispatch_queue = dispatch_queue_create("FileProvider.\(type)", DISPATCH_QUEUE_CONCURRENT)
//let url = baseURL.absoluteString
//let url = baseURL.uw_absoluteString
self.credential = credential
}
func contentsOfDirectoryAtPath(path: String, completionHandler: ((contents: [FileObjectClass], error: ErrorType?) -> Void)) {
public func contentsOfDirectoryAtPath(path: String, completionHandler: ((contents: [FileObjectClass], error: ErrorType?) -> Void)) {
NotImplemented()
dispatch_async(dispatch_queue) {
}
}
func attributesOfItemAtPath(path: String, completionHandler: ((attributes: FileObjectClass?, error: ErrorType?) -> Void)) {
public func attributesOfItemAtPath(path: String, completionHandler: ((attributes: FileObjectClass?, error: ErrorType?) -> Void)) {
NotImplemented()
}
weak var fileOperationDelegate: FileOperationDelegate?
public weak var fileOperationDelegate: FileOperationDelegate?
func createFolder(folderName: String, atPath: String, completionHandler: SimpleCompletionHandler) {
public func createFolder(folderName: String, atPath: String, completionHandler: SimpleCompletionHandler) {
NotImplemented()
}
func createFile(fileAttribs: FileObject, atPath: String, contents data: NSData?, completionHandler: SimpleCompletionHandler) {
public func createFile(fileAttribs: FileObject, atPath: String, contents data: NSData?, completionHandler: SimpleCompletionHandler) {
NotImplemented()
}
func moveItemAtPath(path: String, toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) {
public func moveItemAtPath(path: String, toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) {
NotImplemented()
}
func copyItemAtPath(path: String, toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) {
public func copyItemAtPath(path: String, toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) {
NotImplemented()
}
func removeItemAtPath(path: String, completionHandler: SimpleCompletionHandler) {
public func removeItemAtPath(path: String, completionHandler: SimpleCompletionHandler) {
NotImplemented()
}
func copyLocalFileToPath(localFile: NSURL, toPath: String, completionHandler: SimpleCompletionHandler) {
public func copyLocalFileToPath(localFile: NSURL, toPath: String, completionHandler: SimpleCompletionHandler) {
NotImplemented()
}
func copyPathToLocalFile(path: String, toLocalURL: NSURL, completionHandler: SimpleCompletionHandler) {
public func copyPathToLocalFile(path: String, toLocalURL: NSURL, completionHandler: SimpleCompletionHandler) {
NotImplemented()
}
func contentsAtPath(path: String, completionHandler: ((contents: NSData?, error: ErrorType?) -> Void)) {
public func contentsAtPath(path: String, completionHandler: ((contents: NSData?, error: ErrorType?) -> Void)) {
NotImplemented()
}
func contentsAtPath(path: String, offset: Int64, length: Int, completionHandler: ((contents: NSData?, error: ErrorType?) -> Void)) {
public func contentsAtPath(path: String, offset: Int64, length: Int, completionHandler: ((contents: NSData?, error: ErrorType?) -> Void)) {
NotImplemented()
}
func writeContentsAtPath(path: String, contents data: NSData, atomically: Bool, completionHandler: SimpleCompletionHandler) {
public func writeContentsAtPath(path: String, contents data: NSData, atomically: Bool, completionHandler: SimpleCompletionHandler) {
NotImplemented()
}
func searchFilesAtPath(path: String, recursive: Bool, query: String, foundItemHandler: ((FileObjectClass) -> Void)?, completionHandler: ((files: [FileObjectClass], error: ErrorType?) -> Void)) {
public func searchFilesAtPath(path: String, recursive: Bool, query: String, foundItemHandler: ((FileObjectClass) -> Void)?, completionHandler: ((files: [FileObjectClass], error: ErrorType?) -> Void)) {
NotImplemented()
}
func registerNotifcation(path: String, eventHandler: (() -> Void)) {
public func registerNotifcation(path: String, eventHandler: (() -> Void)) {
NotImplemented()
}
func unregisterNotifcation(path: String) {
public func unregisterNotifcation(path: String) {
NotImplemented()
}
func isRegisteredForNotification(path: String) -> Bool {
public func isRegisteredForNotification(path: String) -> Bool {
return false
}
}
// MARK: basic CIFS interactivity
enum SMBFileProviderError: Int, ErrorType, CustomStringConvertible {
public enum SMBFileProviderError: Int, ErrorType, CustomStringConvertible {
case BadHeader
case IncompatibleHeader
case IncorrectParamsLength
case IncorrectMessageLength
case InvalidCommand
var description: String {
public var description: String {
return "SMB message structure is invalid"
}
}
extension SMBFileProvider {
private extension SMBFileProvider {
private func getPID() -> UInt32 {
return UInt32(NSProcessInfo.processInfo().processIdentifier)
}
+2 -1
View File
@@ -243,11 +243,12 @@ extension SMB2 {
// SRV_ENUMERATE_SNAPSHOTS
struct SrvSnapshots: IOCtlResponseProtocol {
let count: UInt32
let returnedCount: UInt32
let snapshots: [SMBTime]
init?(data: NSData) {
self.count = decode(data)
let returnedCount: UInt32 = decode(data.subdataWithRange(NSRange(location: 4, length: 4)))
self.returnedCount = decode(data.subdataWithRange(NSRange(location: 4, length: 4)))
//let size: UInt32 = decode(data.subdataWithRange(NSRange(location: 8, length: 4)))
var snapshots = [SMBTime]()
let dateFormatter = NSDateFormatter()
+2
View File
@@ -11,6 +11,8 @@ import Foundation
extension SMB2 {
// MARK: SMB2 Query Directory
// MARK: SMB2 Change Notify
// MARK: SMB2 Query Info
+2 -2
View File
@@ -10,7 +10,7 @@ import Foundation
/// Error Types and Description
enum NTStatus: UInt32, ErrorType, CustomStringConvertible {
public enum NTStatus: UInt32, ErrorType, CustomStringConvertible {
case SUCCESS = 0x00000000
case NOT_IMPLEMENTED = 0xC0000002
case INVALID_DEVICE_REQUEST = 0xC0000010
@@ -131,7 +131,7 @@ enum NTStatus: UInt32, ErrorType, CustomStringConvertible {
case NETWORK_SESSION_EXPIRED = 0xC000035C
case SMB_TOO_MANY_UIDS = 0xC000205A
var description: String {
public var description: String {
switch self {
case NOT_IMPLEMENTED, INVALID_DEVICE_REQUEST, ILLEGAL_FUNCTION:
return "Invalid Function."
+2 -2
View File
@@ -49,10 +49,10 @@ public class TCPSocketClient: NSObject, NSStreamDelegate {
* - parameter secure: establishing connection using an SSL/TLS connection
*/
init?(baseURL: NSURL, secure: Bool = false) {
public init?(baseURL: NSURL, secure: Bool = false) {
self.baseURL = baseURL
self.secureConnection = secure
let scheme = baseURL.scheme.lowercaseString
let scheme = baseURL.uw_scheme.lowercaseString
let defaultPort = secure ? UInt32(TCPSocketClient.securePorts[scheme] ?? 0) : UInt32(TCPSocketClient.ports[scheme] ?? 0)
self.port = baseURL.port?.unsignedIntValue ?? defaultPort
if self.port == 0 {
+21 -19
View File
@@ -25,8 +25,8 @@ public enum FileProviderWebDavErrorCode: Int {
}
public struct FileProviderWebDavError: ErrorType, CustomStringConvertible {
let code: FileProviderWebDavErrorCode
let url: NSURL
public let code: FileProviderWebDavErrorCode
public let url: NSURL
public var description: String {
switch code {
@@ -48,10 +48,10 @@ public struct FileProviderWebDavError: ErrorType, CustomStringConvertible {
}
public final class WebDavFileObject: FileObject {
let contentType: String
let entryTag: String?
public let contentType: String
public let entryTag: String?
init(absoluteURL: NSURL, name: String, path: String, size: Int64, contentType: String, createdDate: NSDate?, modifiedDate: NSDate?, fileType: FileType, isHidden: Bool, isReadOnly: Bool, entryTag: String?) {
public init(absoluteURL: NSURL, name: String, path: String, size: Int64, contentType: String, createdDate: NSDate?, modifiedDate: NSDate?, fileType: FileType, isHidden: Bool, isReadOnly: Bool, entryTag: String?) {
self.contentType = contentType
self.entryTag = entryTag
super.init(absoluteURL: absoluteURL, name: name, path: path, size: size, createdDate: createdDate, modifiedDate: modifiedDate, fileType: fileType, isHidden: isHidden, isReadOnly: isReadOnly)
@@ -84,13 +84,13 @@ public class WebDAVFileProvider: NSObject, FileProviderBasic {
return _session!
}
init? (baseURL: NSURL, credential: NSURLCredential?) {
if !["http", "https"].contains(baseURL.scheme.lowercaseString) {
public init? (baseURL: NSURL, credential: NSURLCredential?) {
if !["http", "https"].contains(baseURL.uw_scheme.lowercaseString) {
return nil
}
self.baseURL = baseURL
dispatch_queue = dispatch_queue_create("FileProvider.\(type)", DISPATCH_QUEUE_CONCURRENT)
//let url = baseURL.absoluteString
//let url = baseURL.uw_absoluteString
self.credential = credential
}
@@ -102,7 +102,7 @@ public class WebDAVFileProvider: NSObject, FileProviderBasic {
let url = absoluteURL(path)
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "PROPFIND"
request.setValue(baseURL?.absoluteString, forHTTPHeaderField: "Host")
//request.setValue(baseURL?.uw_absoluteString, forHTTPHeaderField: "Host")
request.setValue("1", 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:allprop/></D:propfind>".dataUsingEncoding(NSUTF8StringEncoding)
@@ -128,7 +128,7 @@ public class WebDAVFileProvider: NSObject, FileProviderBasic {
public func attributesOfItemAtPath(path: String, completionHandler: ((attributes: FileObject?, error: ErrorType?) -> Void)) {
let request = NSMutableURLRequest(URL: absoluteURL(path))
request.HTTPMethod = "PROPFIND"
request.setValue(baseURL?.absoluteString, forHTTPHeaderField: "Host")
//request.setValue(baseURL?.uw_absoluteString, forHTTPHeaderField: "Host")
request.setValue("1", 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:allprop/></D:propfind>".dataUsingEncoding(NSUTF8StringEncoding)
@@ -154,7 +154,7 @@ extension WebDAVFileProvider: FileProviderOperations {
let url = absoluteURL((atPath as NSString).stringByAppendingPathComponent(folderName) + "/")
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "MKCOL"
request.setValue(baseURL?.absoluteString, forHTTPHeaderField: "Host")
//request.setValue(baseURL?.uw_absoluteString, forHTTPHeaderField: "Host")
let task = session.dataTaskWithRequest(request) { (data, response, error) in
if let response = response as? NSHTTPURLResponse, let code = FileProviderWebDavErrorCode(rawValue: response.statusCode) where code != .OK {
completionHandler?(error: FileProviderWebDavError(code: code, url: url))
@@ -193,8 +193,8 @@ extension WebDAVFileProvider: FileProviderOperations {
} else {
request.HTTPMethod = "COPY"
}
request.setValue(baseURL?.absoluteString, forHTTPHeaderField: "Host")
request.setValue(absoluteURL(path).absoluteString, forHTTPHeaderField: "Destination")
//request.setValue(baseURL?.uw_absoluteString, forHTTPHeaderField: "Host")
request.setValue(absoluteURL(path).uw_absoluteString, forHTTPHeaderField: "Destination")
if !overwrite {
request.setValue("F", forHTTPHeaderField: "Overwrite")
}
@@ -223,7 +223,7 @@ extension WebDAVFileProvider: FileProviderOperations {
let url = absoluteURL(path)
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "DELETE"
request.setValue(baseURL?.absoluteString, forHTTPHeaderField: "Host")
//request.setValue(baseURL?.uw_absoluteString, forHTTPHeaderField: "Host")
let task = session.dataTaskWithRequest(request) { (data, response, error) in
if let response = response as? NSHTTPURLResponse, let code = FileProviderWebDavErrorCode(rawValue: response.statusCode) {
defer {
@@ -249,9 +249,9 @@ extension WebDAVFileProvider: FileProviderOperations {
request.HTTPMethod = "PUT"
let task = session.uploadTaskWithRequest(request, fromFile: localFile) { (data, response, error) in
completionHandler?(error: error)
self.delegateNotify(.Move(source: localFile.absoluteString, destination: toPath), error: error)
self.delegateNotify(.Move(source: localFile.uw_absoluteString, destination: toPath), error: error)
}
task.taskDescription = self.dictionaryToJSON(["type": "Copy", "source": localFile.absoluteString, "dest": toPath])
task.taskDescription = self.dictionaryToJSON(["type": "Copy", "source": localFile.uw_absoluteString, "dest": toPath])
task.resume()
}
@@ -268,7 +268,7 @@ extension WebDAVFileProvider: FileProviderOperations {
}
completionHandler?(error: error)
}
task.taskDescription = self.dictionaryToJSON(["type": "Copy", "source": path, "dest": toLocalURL.absoluteString])
task.taskDescription = self.dictionaryToJSON(["type": "Copy", "source": path, "dest": toLocalURL.uw_absoluteString])
task.resume()
}
}
@@ -294,7 +294,7 @@ extension WebDAVFileProvider: FileProviderReadWrite {
public func writeContentsAtPath(path: String, contents data: NSData, atomically: Bool = false, completionHandler: SimpleCompletionHandler) {
// FIXME: lock destination before writing process
let url = atomically ? absoluteURL(path).URLByAppendingPathExtension("tmp") : absoluteURL(path)
let url = atomically ? absoluteURL(path).uw_URLByAppendingPathExtension("tmp") : absoluteURL(path)
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "PUT"
let task = session.uploadTaskWithRequest(request, fromData: data) { (data, response, error) in
@@ -317,7 +317,7 @@ extension WebDAVFileProvider: FileProviderReadWrite {
let url = absoluteURL(path)
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "PROPFIND"
request.setValue(baseURL?.absoluteString, forHTTPHeaderField: "Host")
//request.setValue(baseURL?.uw_absoluteString, forHTTPHeaderField: "Host")
//request.setValue("1", 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:allprop/></D:propfind>".dataUsingEncoding(NSUTF8StringEncoding)
@@ -357,6 +357,8 @@ extension WebDAVFileProvider: FileProviderReadWrite {
// TODO: implements methods for lock mechanism
}
extension WebDAVFileProvider: FileProvider {}
// MARK: WEBDAV XML response implementation
internal extension WebDAVFileProvider {