From 7d8de9cefe6ae4346a329ea2dffba55836bf8b7a Mon Sep 17 00:00:00 2001 From: Amir Abbas Mousavian Date: Sat, 3 Dec 2016 20:26:27 +0330 Subject: [PATCH] FileObject init accessors fixed, refactoring --- Sources/DropboxHelper.swift | 2 +- Sources/FileProvider.swift | 321 ++++++++++++++----------------- Sources/WebDAVFileProvider.swift | 2 +- 3 files changed, 143 insertions(+), 182 deletions(-) diff --git a/Sources/DropboxHelper.swift b/Sources/DropboxHelper.swift index 67c9c09..5b97413 100644 --- a/Sources/DropboxHelper.swift +++ b/Sources/DropboxHelper.swift @@ -19,7 +19,7 @@ public struct FileProviderDropboxError: Error, CustomStringConvertible { } public final class DropboxFileObject: FileObject { - public init(name: String, path: String) { + internal init(name: String, path: String) { super.init(absoluteURL: URL(string: path), name: name, path: path) } diff --git a/Sources/FileProvider.swift b/Sources/FileProvider.swift index cff1824..358af2d 100644 --- a/Sources/FileProvider.swift +++ b/Sources/FileProvider.swift @@ -15,178 +15,6 @@ import Cocoa public typealias ImageClass = NSImage #endif -public enum FileType: String { - case directory - case regular - case symbolicLink - case socket - case characterSpecial - case blockSpecial - case namedPipe - case unknown - - public init(urlResourceTypeValue: URLFileResourceType) { - switch urlResourceTypeValue { - case URLFileResourceType.namedPipe: self = .namedPipe - case URLFileResourceType.characterSpecial: self = .characterSpecial - case URLFileResourceType.directory: self = .directory - case URLFileResourceType.blockSpecial: self = .blockSpecial - case URLFileResourceType.regular: self = .regular - case URLFileResourceType.symbolicLink: self = .symbolicLink - case URLFileResourceType.socket: self = .socket - case URLFileResourceType.unknown: self = .unknown - default: self = .unknown - } - } - - public init(fileTypeValue: FileAttributeType) { - switch fileTypeValue { - case FileAttributeType.typeCharacterSpecial: self = .characterSpecial - case FileAttributeType.typeDirectory: self = .directory - case FileAttributeType.typeBlockSpecial: self = .blockSpecial - case FileAttributeType.typeRegular: self = .regular - case FileAttributeType.typeSymbolicLink: self = .symbolicLink - case FileAttributeType.typeSocket: self = .socket - case FileAttributeType.typeUnknown: self = .unknown - default: self = .unknown - } - } - - var resourceType: URLFileResourceType { - switch self { - case .namedPipe: return .namedPipe - case .characterSpecial: return .characterSpecial - case .directory: return .directory - case .blockSpecial: return .blockSpecial - case .regular: return .regular - case .symbolicLink: return .symbolicLink - case .socket: return .socket - case .unknown: return .unknown - } - } -} - -public protocol FoundationErrorEnum { - init? (rawValue: Int) - var rawValue: Int { get } -} - -extension URLError.Code: FoundationErrorEnum {} -extension CocoaError.Code: FoundationErrorEnum {} - -open class FileObject { - open internal(set) var allValues: [String: Any] - - internal init(allValues: [String: Any]) { - self.allValues = allValues - } - - internal init(absoluteURL: URL? = nil, name: String, path: String) { - self.allValues = [String: Any]() - self.absoluteURL = absoluteURL - self.name = name - self.path = path - } - - open internal(set) var absoluteURL: URL? { - get { - return allValues["NSURLAbsoluteURLKey"] as? URL - } - set { - allValues["NSURLAbsoluteURLKey"] = newValue - } - } - - open internal(set) var name: String { - get { - return allValues[URLResourceKey.nameKey.rawValue] as! String - } - set { - allValues[URLResourceKey.nameKey.rawValue] = newValue - } - } - - open internal(set) var path: String { - get { - return allValues[URLResourceKey.pathKey.rawValue] as! String - } - set { - allValues[URLResourceKey.pathKey.rawValue] = newValue - } - } - - open internal(set) var size: Int64 { - get { - return allValues[URLResourceKey.fileSizeKey.rawValue] as? Int64 ?? -1 - } - set { - allValues[URLResourceKey.fileSizeKey.rawValue] = Int(exactly: newValue) ?? Int.max - } - } - - open internal(set) var creationDate: Date? { - get { - return allValues[URLResourceKey.creationDateKey.rawValue] as? Date - } - set { - allValues[URLResourceKey.creationDateKey.rawValue] = newValue - } - } - - open internal(set) var modifiedDate: Date? { - get { - return allValues[URLResourceKey.contentModificationDateKey.rawValue] as? Date - } - set { - allValues[URLResourceKey.contentModificationDateKey.rawValue] = newValue - } - } - - open internal(set) var fileType: FileType? { - get { - guard let typeString = allValues[URLResourceKey.fileResourceTypeKey.rawValue] as? String else { - return nil - } - let type = URLFileResourceType(rawValue: typeString) - return FileType(urlResourceTypeValue: type) - } - set { - allValues[URLResourceKey.fileResourceTypeKey.rawValue] = newValue?.resourceType.rawValue ?? FileType.unknown.resourceType.rawValue - } - } - - open internal(set) var isHidden: Bool { - get { - return allValues[URLResourceKey.isHiddenKey.rawValue] as? Bool ?? false - } - set { - allValues[URLResourceKey.isHiddenKey.rawValue] = newValue - } - } - - open internal(set) var isReadOnly: Bool { - get { - return !(allValues[URLResourceKey.isWritableKey.rawValue] as? Bool ?? true) - } - set { - allValues[URLResourceKey.isWritableKey.rawValue] = !newValue - } - } - - open var isDirectory: Bool { - return self.fileType == .directory - } - - open var isRegularFile: Bool { - return self.fileType == .regular - } - - open var isSymLink: Bool { - return self.fileType == .symbolicLink - } -} - - public typealias SimpleCompletionHandler = ((_ error: Error?) -> Void)? public protocol FileProviderBasic: class { @@ -472,6 +300,117 @@ public enum FileOperationType: CustomStringConvertible { } } +open class FileObject { + open internal(set) var allValues: [String: Any] + + internal init(allValues: [String: Any]) { + self.allValues = allValues + } + + internal init(absoluteURL: URL? = nil, name: String, path: String) { + self.allValues = [String: Any]() + self.absoluteURL = absoluteURL + self.name = name + self.path = path + } + + open internal(set) var absoluteURL: URL? { + get { + return allValues["NSURLAbsoluteURLKey"] as? URL + } + set { + allValues["NSURLAbsoluteURLKey"] = newValue + } + } + + open internal(set) var name: String { + get { + return allValues[URLResourceKey.nameKey.rawValue] as! String + } + set { + allValues[URLResourceKey.nameKey.rawValue] = newValue + } + } + + open internal(set) var path: String { + get { + return allValues[URLResourceKey.pathKey.rawValue] as! String + } + set { + allValues[URLResourceKey.pathKey.rawValue] = newValue + } + } + + open internal(set) var size: Int64 { + get { + return allValues[URLResourceKey.fileSizeKey.rawValue] as? Int64 ?? -1 + } + set { + allValues[URLResourceKey.fileSizeKey.rawValue] = Int(exactly: newValue) ?? Int.max + } + } + + open internal(set) var creationDate: Date? { + get { + return allValues[URLResourceKey.creationDateKey.rawValue] as? Date + } + set { + allValues[URLResourceKey.creationDateKey.rawValue] = newValue + } + } + + open internal(set) var modifiedDate: Date? { + get { + return allValues[URLResourceKey.contentModificationDateKey.rawValue] as? Date + } + set { + allValues[URLResourceKey.contentModificationDateKey.rawValue] = newValue + } + } + + open internal(set) var fileType: URLFileResourceType? { + get { + guard let typeString = allValues[URLResourceKey.fileResourceTypeKey.rawValue] as? String else { + return nil + } + return URLFileResourceType(rawValue: typeString) + } + set { + allValues[URLResourceKey.fileResourceTypeKey.rawValue] = newValue + } + } + + open internal(set) var isHidden: Bool { + get { + return allValues[URLResourceKey.isHiddenKey.rawValue] as? Bool ?? false + } + set { + allValues[URLResourceKey.isHiddenKey.rawValue] = newValue + } + } + + open internal(set) var isReadOnly: Bool { + get { + return !(allValues[URLResourceKey.isWritableKey.rawValue] as? Bool ?? true) + } + set { + allValues[URLResourceKey.isWritableKey.rawValue] = !newValue + } + } + + open var isDirectory: Bool { + return self.fileType == .directory + } + + open var isRegularFile: Bool { + return self.fileType == .regular + } + + open var isSymLink: Bool { + return self.fileType == .symbolicLink + } +} + public protocol OperationHandle { var operationType: FileOperationType { get } var bytesSoFar: Int64 { get } @@ -489,13 +428,6 @@ public extension OperationHandle { } } -internal class Weak { - weak var value : T? - init (_ value: T) { - self.value = value - } -} - public protocol FileProviderDelegate: class { func fileproviderSucceed(_ fileProvider: FileProviderOperations, operation: FileOperationType) func fileproviderFailed(_ fileProvider: FileProviderOperations, operation: FileOperationType) @@ -512,13 +444,42 @@ public protocol FileOperationDelegate: class { } // THESE ARE METHODS TO PROVIDE COMPATIBILITY WITH SWIFT 2.3 SIMOULTANIOUSLY! - internal extension URL { var uw_scheme: String { return self.scheme ?? "" } } +internal class Weak { + weak var value : T? + init (_ value: T) { + self.value = value + } +} + +extension URLFileResourceType { + public init(fileTypeValue: FileAttributeType) { + switch fileTypeValue { + case FileAttributeType.typeCharacterSpecial: self = .characterSpecial + case FileAttributeType.typeDirectory: self = .directory + case FileAttributeType.typeBlockSpecial: self = .blockSpecial + case FileAttributeType.typeRegular: self = .regular + case FileAttributeType.typeSymbolicLink: self = .symbolicLink + case FileAttributeType.typeSocket: self = .socket + case FileAttributeType.typeUnknown: self = .unknown + default: self = .unknown + } + } +} + +public protocol FoundationErrorEnum { + init? (rawValue: Int) + var rawValue: Int { get } +} + +extension URLError.Code: FoundationErrorEnum {} +extension CocoaError.Code: FoundationErrorEnum {} + internal func jsonToDictionary(_ jsonString: String) -> [String: AnyObject]? { guard let data = jsonString.data(using: .utf8) else { return nil diff --git a/Sources/WebDAVFileProvider.swift b/Sources/WebDAVFileProvider.swift index 7981798..2f1e2eb 100644 --- a/Sources/WebDAVFileProvider.swift +++ b/Sources/WebDAVFileProvider.swift @@ -9,7 +9,7 @@ import Foundation public final class WebDavFileObject: FileObject { - public init(absoluteURL: URL, name: String, path: String) { + internal init(absoluteURL: URL, name: String, path: String) { super.init(absoluteURL: absoluteURL, name: name, path: path) }