diff --git a/Sources/FTPFileProvider.swift b/Sources/FTPFileProvider.swift index bc8b493..8ed68cb 100644 --- a/Sources/FTPFileProvider.swift +++ b/Sources/FTPFileProvider.swift @@ -281,22 +281,39 @@ open class FTPFileProvider: FileProviderBasicRemote { open func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping ((_ files: [FileObject], _ error: Error?) -> Void)) -> Progress? { let progress = Progress(parent: nil, userInfo: nil) - _ = self.recursiveList(path: path, useMLST: true, foundItemsHandler: { items in - if let foundItemHandler = foundItemHandler { - for item in items where query.evaluate(with: item.mapPredicate()) { - foundItemHandler(item) + if recursive { + return self.recursiveList(path: path, useMLST: true, foundItemsHandler: { items in + if let foundItemHandler = foundItemHandler { + for item in items where query.evaluate(with: item.mapPredicate()) { + foundItemHandler(item) + } + progress.totalUnitCount = Int64(items.count) } - progress.totalUnitCount = Int64(items.count) - } - }, completionHandler: {files, error in - if let error = error { - completionHandler([], error) - return - } - - let foundFiles = files.filter { query.evaluate(with: $0.mapPredicate()) } - completionHandler(foundFiles, nil) - }) + }, completionHandler: {files, error in + if let error = error { + completionHandler([], error) + return + } + + let foundFiles = files.filter { query.evaluate(with: $0.mapPredicate()) } + completionHandler(foundFiles, nil) + }) + } else { + self.contentsOfDirectory(path: path, completionHandler: { (items, error) in + if let error = error { + completionHandler([], error) + return + } + + var result = [FileObject]() + for item in items where query.evaluate(with: item.mapPredicate()) { + foundItemHandler?(item) + result.append(item) + } + completionHandler(result, nil) + }) + } + return progress } diff --git a/Sources/OneDriveFileProvider.swift b/Sources/OneDriveFileProvider.swift index fb35bda..661e0a2 100644 --- a/Sources/OneDriveFileProvider.swift +++ b/Sources/OneDriveFileProvider.swift @@ -115,7 +115,7 @@ open class OneDriveFileProvider: HTTPFileProvider { guard let finalQueryStr = queryStr else { return nil } let progress = Progress(parent: nil, userInfo: nil) progress.setUserInfoObject(url(of: path), forKey: .fileURLKey) - search(path, query: finalQueryStr, progress: progress, foundItem: { (file) in + search(path, query: finalQueryStr, recursive: recursive, progress: progress, foundItem: { (file) in if query.evaluate(with: file.mapPredicate()) { foundFiles.append(file) foundItemHandler?(file) diff --git a/Sources/OneDriveHelper.swift b/Sources/OneDriveHelper.swift index 6a1403e..bf2a16c 100644 --- a/Sources/OneDriveHelper.swift +++ b/Sources/OneDriveHelper.swift @@ -114,14 +114,15 @@ internal extension OneDriveFileProvider { task.resume() } - func search(_ startPath: String = "", query: String, next: URL? = nil, progress: Progress, foundItem: @escaping ((_ file: OneDriveFileObject) -> Void), completionHandler: @escaping ((_ error: Error?) -> Void)) { + func search(_ startPath: String = "", query: String, recursive: Bool, next: URL? = nil, progress: Progress, foundItem: @escaping ((_ file: OneDriveFileObject) -> Void), completionHandler: @escaping ((_ error: Error?) -> Void)) { if progress.isCancelled { return } let url: URL let q = query.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! - url = next ?? self.url(of: startPath, modifier: "view.search?q=\(q)") + let expanded = recursive ? "&expand=children" : "" + url = next ?? self.url(of: startPath, modifier: "view.search?q=\(q)\(expanded)") var request = URLRequest(url: url) request.httpMethod = "GET" request.set(httpAuthentication: credential, with: .oAuth2) @@ -140,7 +141,7 @@ internal extension OneDriveFileProvider { } let next: URL? = (json["@odata.nextLink"] as? String).flatMap { URL(string: $0) } if !progress.isCancelled, let next = next { - self.search(startPath, query: query, next: next, progress: progress, foundItem: foundItem, completionHandler: completionHandler) + self.search(startPath, query: query, recursive: recursive, next: next, progress: progress, foundItem: foundItem, completionHandler: completionHandler) } else { completionHandler(responseError ?? error) } diff --git a/Sources/WebDAVFileProvider.swift b/Sources/WebDAVFileProvider.swift index 17f4e5b..9a27bee 100644 --- a/Sources/WebDAVFileProvider.swift +++ b/Sources/WebDAVFileProvider.swift @@ -124,7 +124,7 @@ open class WebDAVFileProvider: HTTPFileProvider { let url = self.url(of: path) var request = URLRequest(url: url) request.httpMethod = "PROPFIND" - request.setValue("1", forHTTPHeaderField: "Depth") + request.setValue("0", forHTTPHeaderField: "Depth") request.set(httpAuthentication: credential, with: credentialType) request.set(contentType: .xml) request.httpBody = "\n\n\(WebDavFileObject.propString(including))\n".data(using: .utf8) @@ -177,7 +177,8 @@ open class WebDAVFileProvider: HTTPFileProvider { let url = self.url(of: path) var request = URLRequest(url: url) request.httpMethod = "PROPFIND" - //request.setValue("1", forHTTPHeaderField: "Depth") + // Depth infinity is disabled on some servers. Implement workaround?! + request.setValue(recursive ? "infinity" : "1", forHTTPHeaderField: "Depth") request.set(httpAuthentication: credential, with: credentialType) request.set(contentType: .xml) request.httpBody = "\n\n".data(using: .utf8)