2016-08-30 21:44:58 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
extension URL {
|
2016-09-07 21:12:18 +03:00
|
|
|
|
2016-10-08 20:58:21 +03:00
|
|
|
func isParent(of url: URL) -> Bool {
|
2016-09-25 18:50:33 +03:00
|
|
|
guard self.isFileURL && url.isFileURL else {
|
2016-09-07 21:12:18 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
let myPathComps = self.pathComponents
|
|
|
|
let targetPathComps = url.pathComponents
|
2016-09-07 21:12:18 +03:00
|
|
|
|
|
|
|
guard targetPathComps.count > myPathComps.count else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return Array(targetPathComps[0..<myPathComps.count]) == myPathComps
|
|
|
|
}
|
2016-08-30 21:44:58 +03:00
|
|
|
|
|
|
|
/// Wrapper function for NSURL.getResourceValue for Bool values.
|
|
|
|
/// Returns also `false` when
|
|
|
|
/// - there is no value for the given `key` or
|
|
|
|
/// - the value cannot be converted to `NSNumber`.
|
|
|
|
///
|
|
|
|
/// - parameters:
|
|
|
|
/// - key: The `key`-parameter of `NSURL.getResourceValue`.
|
2016-09-25 18:50:33 +03:00
|
|
|
func resourceValue(_ key: String) -> Bool {
|
2016-08-30 21:44:58 +03:00
|
|
|
var rsrc: AnyObject?
|
|
|
|
|
|
|
|
do {
|
2016-09-25 18:50:33 +03:00
|
|
|
try (self as NSURL).getResourceValue(&rsrc, forKey: URLResourceKey(rawValue: key))
|
2016-08-30 21:44:58 +03:00
|
|
|
} catch {
|
|
|
|
// FIXME error handling
|
|
|
|
print("\(#function): \(self) -> ERROR while getting \(key)")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if let result = rsrc as? NSNumber {
|
|
|
|
return result.boolValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var dir: Bool {
|
2016-09-25 18:50:33 +03:00
|
|
|
return self.resourceValue(URLResourceKey.isDirectoryKey.rawValue)
|
2016-08-30 21:44:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var hidden: Bool {
|
2016-09-25 18:50:33 +03:00
|
|
|
return self.resourceValue(URLResourceKey.isHiddenKey.rawValue)
|
2016-08-30 21:44:58 +03:00
|
|
|
}
|
2016-09-11 15:37:04 +03:00
|
|
|
|
|
|
|
var package: Bool {
|
2016-09-25 18:50:33 +03:00
|
|
|
return self.resourceValue(URLResourceKey.isPackageKey.rawValue)
|
2016-09-11 15:37:04 +03:00
|
|
|
}
|
2016-08-30 21:44:58 +03:00
|
|
|
}
|