From bf7043de293c84f15d9d5fa73e7829f52fb884ff Mon Sep 17 00:00:00 2001 From: Amir Abbas Date: Tue, 11 Apr 2017 19:43:34 +0430 Subject: [PATCH] Fixed `relativePath(of:)` bug, made it overridable - Fixed StreamTask.taskDescription bug --- Sources/FPSStreamTask.swift | 27 ++++++++++++++++++++++++++ Sources/FileProvider.swift | 38 ++++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/Sources/FPSStreamTask.swift b/Sources/FPSStreamTask.swift index ff52c1a..2f806a7 100644 --- a/Sources/FPSStreamTask.swift +++ b/Sources/FPSStreamTask.swift @@ -23,6 +23,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate { return (_underlyingSession.delegate as? FPSStreamDelegate) } fileprivate var _taskIdentifier: Int + fileprivate var _taskDescription: String? /// Force using `URLSessionStreamTask` for iOS 9 and later public var useURLSession = true @@ -50,6 +51,32 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate { return _taskIdentifier } + /// An app-provided description of the current task. + /// + /// This value may be nil. It is intended to contain human-readable strings that you can + /// then display to the user as part of your app’s user interface. + open override var taskDescription: String? { + get { + if #available(iOS 9.0, OSX 10.11, *) { + if self.useURLSession { + return _underlyingTask!.taskDescription + } + } + + return _taskDescription + } + set { + if #available(iOS 9.0, OSX 10.11, *) { + if self.useURLSession { + _underlyingTask!.taskDescription = newValue + return + } + } + + _taskDescription = newValue + } + } + fileprivate var _state: URLSessionTask.State = .suspended /** * The current state of the task—active, suspended, in the process diff --git a/Sources/FileProvider.swift b/Sources/FileProvider.swift index 8a62434..29408d8 100644 --- a/Sources/FileProvider.swift +++ b/Sources/FileProvider.swift @@ -132,6 +132,15 @@ public protocol FileProviderBasic: class, NSCoding, NSSecureCoding { */ func url(of path: String?) -> URL + + /// Returns the relative path of url, wothout percent encoding. Even if url is absolute or + /// retrieved from another provider, it will try to resolve the url against `baseURL` of + /// current provider. It's highly recomended to use this method for displaying purposes. + /// + /// - Parameter url: Absolute url to file or directory. + /// - Returns: A `String` contains relative path of url against base url. + func relativePathOf(url: URL) -> String + /// Checks the connection to server or permission on local func isReachable(completionHandler: @escaping(_ success: Bool) -> Void) } @@ -625,26 +634,29 @@ extension FileProviderBasic { } } - - /// Returns the relative path of url, wothout percent encoding. Even if url is absolute or - /// retrieved from another provider, it will try to resolve the url against `baseURL` of - /// current provider. It's highly recomended to use this method for displaying purposes. - /// - /// - Parameter url: Absolute url to file or directory. - /// - Returns: A `String` contains relative path of url against base url. public func relativePathOf(url: URL) -> String { // check if url derieved from current base url let relativePath = url.relativePath if !relativePath.isEmpty, url.baseURL == self.baseURL { - return relativePath.removingPercentEncoding ?? relativePath + return (relativePath.removingPercentEncoding ?? relativePath).replacingOccurrences(of: "/", with: "", options: .anchored) } // resolve url string against baseurl - guard let baseURL = self.baseURL?.standardizedFileURL else { return url.absoluteString } - let standardPath = url.absoluteString.replacingOccurrences(of: "file:///private/var/", with: "file:///var/", options: .anchored) - let standardBase = baseURL.absoluteString.replacingOccurrences(of: "file:///private/var/", with: "file:///var/", options: .anchored) - let standardRelativePath = standardPath.replacingOccurrences(of: standardBase, with: "/") - return standardRelativePath.removingPercentEncoding ?? standardRelativePath + if baseURL?.isFileURL ?? false { + guard let baseURL = self.baseURL?.standardizedFileURL else { return url.absoluteString } + let standardPath = url.absoluteString.replacingOccurrences(of: "file:///private/var/", with: "file:///var/", options: .anchored) + let standardBase = baseURL.absoluteString.replacingOccurrences(of: "file:///private/var/", with: "file:///var/", options: .anchored) + let standardRelativePath = standardPath.replacingOccurrences(of: standardBase, with: "/").replacingOccurrences(of: "/", with: "", options: .anchored) + return standardRelativePath.removingPercentEncoding ?? standardRelativePath + } else { + guard let baseURL = self.baseURL else { return url.absoluteString } + let standardRelativePath = url.absoluteString.replacingOccurrences(of: baseURL.absoluteString, with: "/").replacingOccurrences(of: "/", with: "", options: .anchored) + if URLComponents(string: standardRelativePath)?.host?.isEmpty ?? true { + return standardRelativePath.removingPercentEncoding ?? standardRelativePath + } else { + return relativePath.replacingOccurrences(of: "/", with: "", options: .anchored) + } + } } internal func correctPath(_ path: String?) -> String? {