Skip to content

Commit

Permalink
Create RefdsFileManager.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelesantos committed Jul 17, 2024
1 parent 5020b85 commit 34b838b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Sources/RefdsShared/File/RefdsFileManager.swift
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)
}
}
}

0 comments on commit 34b838b

Please sign in to comment.