diff --git a/Sources/FileProvider.swift b/Sources/FileProvider.swift index 372d7c1..cff1824 100644 --- a/Sources/FileProvider.swift +++ b/Sources/FileProvider.swift @@ -304,7 +304,7 @@ public protocol FileProvider: FileProviderBasic, FileProviderOperations, FilePro extension FileProviderBasic { public var type: String { - return type(of: self).type + return Self.type } public var bareCurrentPath: String { @@ -416,12 +416,10 @@ extension FileProviderBasic { if let isotime = dateFor.date(from: dateString) { return isotime } - //self.init() return nil } } - public protocol ExtendedFileProvider: FileProvider { func thumbnailOfFileSupported(path: String) -> Bool func propertiesOfFileSupported(path: String) -> Bool @@ -479,6 +477,7 @@ public protocol OperationHandle { var bytesSoFar: Int64 { get } var totalBytes: Int64 { get } var inProgress: Bool { get } + var progress: Float { get } func cancel() -> Bool } diff --git a/Sources/WebDAVFileProvider.swift b/Sources/WebDAVFileProvider.swift index 634e7d8..7981798 100644 --- a/Sources/WebDAVFileProvider.swift +++ b/Sources/WebDAVFileProvider.swift @@ -214,7 +214,7 @@ extension WebDAVFileProvider: FileProviderOperations { guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else { return nil } - return self.copyMoveItem(operation: opType, overwrite: overwrite, completionHandler: completionHandler) + return self.doOperation(operation: opType, overwrite: overwrite, completionHandler: completionHandler) } @discardableResult @@ -223,17 +223,36 @@ extension WebDAVFileProvider: FileProviderOperations { guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else { return nil } - return self.copyMoveItem(operation: opType, overwrite: overwrite, completionHandler: completionHandler) + return self.doOperation(operation: opType, overwrite: overwrite, completionHandler: completionHandler) } - fileprivate func copyMoveItem(operation opType: FileOperationType, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? { - guard let source = opType.source, let dest = opType.destination else { return nil } - // Using switch is more readable than using reflect. Maybe some - let sourceURL = absoluteURL(source) + @discardableResult + public func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? { + let opType = FileOperationType.remove(path: path) + guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else { + return nil + } + return self.doOperation(operation: opType, completionHandler: completionHandler) + } + + func doOperation(operation opType: FileOperationType, overwrite: Bool? = nil, completionHandler: SimpleCompletionHandler) -> OperationHandle? { + let sourceURL = absoluteURL(opType.source!) var request = URLRequest(url: sourceURL) - request.httpMethod = opType.description.uppercased() // "COPY" or "MOVE" - request.setValue(absoluteURL(dest).absoluteString, forHTTPHeaderField: "Destination") - if !overwrite { + if let dest = opType.destination { + request.setValue(absoluteURL(dest).absoluteString, forHTTPHeaderField: "Destination") + } + switch opType { + case .copy: + request.httpMethod = "COPY" + case .move: + request.httpMethod = "MOVE" + case .remove: + request.httpMethod = "DELETE" + default: + return nil + } + + if let overwrite = overwrite, !overwrite { request.setValue("F", forHTTPHeaderField: "Overwrite") } let task = session.dataTask(with: request, completionHandler: { (data, response, error) in @@ -260,38 +279,6 @@ extension WebDAVFileProvider: FileProviderOperations { return RemoteOperationHandle(operationType: opType, tasks: [task]) } - @discardableResult - public func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? { - let opType = FileOperationType.remove(path: path) - guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else { - return nil - } - let url = absoluteURL(path) - var request = URLRequest(url: url) - request.httpMethod = "DELETE" - let task = session.dataTask(with: request, completionHandler: { (data, response, error) in - var responseError: FileProviderWebDavError? - if let response = response as? HTTPURLResponse, let code = FileProviderHTTPErrorCode(rawValue: response.statusCode) { - if response.statusCode >= 300 { - responseError = FileProviderWebDavError(code: code, url: url) - } - if code == .multiStatus, let data = data { - let xresponses = self.parseXMLResponse(data) - for xresponse in xresponses where (xresponse.status ?? 0) >= 300 { - completionHandler?(FileProviderWebDavError(code: code, url: url)) - } - } - } - if (response as? HTTPURLResponse)?.statusCode ?? 0 != FileProviderHTTPErrorCode.multiStatus.rawValue { - completionHandler?(responseError ?? error) - } - self.delegateNotify(opType, error: responseError ?? error) - }) - task.taskDescription = opType.json - task.resume() - return RemoteOperationHandle(operationType: opType, tasks: [task]) - } - @discardableResult public func copyItem(localFile: URL, to toPath: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? { let opType = FileOperationType.copy(source: localFile.absoluteString, destination: toPath)