Last active
October 20, 2021 02:39
-
-
Save orchetect/bb901d3564d69cb69ae79222f0a08fde to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if os(macOS) | |
import Foundation | |
/// On init, parses Dropbox's info.json file and populates the pertinent data into variables. Object is nil if info.json file can't be found (which usually means Dropbox is not installed on the system) or there was a generic error parsing the JSON data. | |
/// | |
/// Properties are populated once upon the struct's init. There is no method to refresh the data. Instead, instance a new copy of `DropboxPaths`. | |
/// | |
/// - `personalPath`: POSIX path if found; nil if not discoverable | |
/// - `personalURL`: URL if found; nil if not discoverable | |
/// - `businessPath`: POSIX path if found; nil if not discoverable | |
/// - `businessURL`: URL if found; nil if not discoverable | |
public struct DropboxPaths { | |
/// POSIX path, `nil` if not discoverable. | |
/// | |
/// Safe to access repeatedly. (Value is polled from the system only once at the time of the struct's init, so to refresh the value, instance a new copy of `DropboxPaths`.) | |
public let personalPath: String? | |
/// POSIX path, nil if not discoverable. Safe to access repeatedly. | |
/// | |
/// Safe to access repeatedly. (Value is polled from the system only once at the time of the struct's init, so to refresh the value, instance a new copy of `DropboxPaths`.) | |
public let businessPath: String? | |
/// File URL path, `nil` if not discoverable. | |
/// | |
/// Safe to access repeatedly. (Value is polled from the system only once at the time of the struct's init, so to refresh the value, instance a new copy of `DropboxPaths`.) | |
public var personalURL: URL? | |
/// File URL path, `nil` if not discoverable. | |
/// | |
/// Safe to access repeatedly. (Value is polled from the system only once at the time of the struct's init, so to refresh the value, instance a new copy of `DropboxPaths`.) | |
public var businessURL: URL? | |
// Example info.json file: | |
// { | |
// "personal": { | |
// "path": "/Users/username/Dropbox", | |
// "subscription_type": "Pro", | |
// "is_team": false, | |
// "host": 1722515629 | |
// } | |
// } | |
public init?() { | |
// attempt to access Dropbox's json meta-data file (in user's home folder) | |
let url = FileManager.homeDirectoryForCurrentUserCompat | |
.appendingPathComponent(".dropbox") | |
.appendingPathComponent("info.json") | |
guard let readJSON = try? Data(contentsOf: url), | |
let json = try? JSONSerialization | |
.jsonObject(with: readJSON) as? [String: Any] else { | |
return nil | |
} | |
if let personalDict = json["personal"] as? [String: Any] { | |
personalPath = personalDict["path"] as? String | |
personalURL = personalPath != nil | |
? URL(fileURLWithPath: personalPath!) | |
: nil | |
} else { | |
personalPath = nil | |
personalURL = nil | |
} | |
if let businessDict = json["business"] as? [String: Any] { | |
businessPath = businessDict["path"] as? String | |
businessURL = businessPath != nil | |
? URL(fileURLWithPath: businessPath!) | |
: nil | |
} else { | |
businessPath = nil | |
businessURL = nil | |
} | |
} | |
} | |
extension FileManager { | |
public static var homeDirectoryForCurrentUserCompat: URL { | |
if #available(OSX 10.12, *) { | |
// only available on macOS | |
return FileManager.default.homeDirectoryForCurrentUser | |
} else { | |
// available on all Apple platforms | |
return URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true) | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment