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-11-12 18:42:10 +03:00
|
|
|
|
|
|
|
var parent: URL {
|
|
|
|
if self.path == "/" {
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.deletingLastPathComponent()
|
|
|
|
}
|
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-11-03 21:57:19 +03:00
|
|
|
} catch let error as NSError {
|
2016-08-30 21:44:58 +03:00
|
|
|
// FIXME error handling
|
2016-11-03 21:57:19 +03:00
|
|
|
print("\(#function): \(self) -> ERROR while getting \(key): \(error)")
|
2016-08-30 21:44:58 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if let result = rsrc as? NSNumber {
|
|
|
|
return result.boolValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:42:10 +03:00
|
|
|
var isDir: Bool {
|
2016-09-25 18:50:33 +03:00
|
|
|
return self.resourceValue(URLResourceKey.isDirectoryKey.rawValue)
|
2016-08-30 21:44:58 +03:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:42:10 +03:00
|
|
|
var isHidden: 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
|
|
|
|
2016-11-12 18:42:10 +03:00
|
|
|
var isPackage: 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
|
|
|
}
|