generated from rafaelesantos/swift-readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5020b85
commit 34b838b
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
|
||
public enum RefdsFileManagerPrivacy: String { | ||
case `public` = "" | ||
case `private` = ".private" | ||
} | ||
|
||
public final class RefdsFileManager { | ||
public static var `default` = RefdsFileManager() | ||
private var fileManager = FileManager.default | ||
|
||
private init() {} | ||
|
||
public func path( | ||
with name: String, | ||
privacy: RefdsFileManagerPrivacy = .public | ||
) -> URL? { | ||
guard let documentsURL = fileManager.urls( | ||
for: .documentDirectory, | ||
in: .userDomainMask | ||
).first else { return nil } | ||
|
||
switch privacy { | ||
case .public: | ||
return documentsURL.appendingPathComponent(name) | ||
|
||
case .private: | ||
let privateURL = documentsURL.appendingPathComponent(privacy.rawValue) | ||
do { | ||
try fileManager.createDirectory( | ||
at: privateURL, | ||
withIntermediateDirectories: true, | ||
attributes: nil | ||
) | ||
} catch { return nil } | ||
return privateURL.appendingPathComponent(name) | ||
} | ||
} | ||
} |