-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
FileManagerExtensions.swift
79 lines (69 loc) · 3.35 KB
/
FileManagerExtensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// FileManagerExtensions.swift - Copyright 2024 SwifterSwift
#if canImport(Foundation)
import Foundation
public extension FileManager {
/// SwifterSwift: Read from a JSON file at a given path.
///
/// - Parameters:
/// - path: JSON file path.
/// - readingOptions: JSONSerialization reading options.
/// - Throws: Throws any errors thrown by Data creation or JSON serialization.
/// - Returns: Optional dictionary.
func jsonFromFile(
atPath path: String,
readingOptions: JSONSerialization.ReadingOptions = .allowFragments) throws -> [String: Any]? {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let json = try JSONSerialization.jsonObject(with: data, options: readingOptions)
return json as? [String: Any]
}
#if !os(Linux)
/// SwifterSwift: Read from a JSON file with a given filename.
///
/// - Parameters:
/// - filename: File to read.
/// - bundleClass: Bundle where the file is associated.
/// - readingOptions: JSONSerialization reading options.
/// - Throws: Throws any errors thrown by Data creation or JSON serialization.
/// - Returns: Optional dictionary.
func jsonFromFile(
withFilename filename: String,
at bundleClass: AnyClass? = nil,
readingOptions: JSONSerialization.ReadingOptions = .allowFragments) throws -> [String: Any]? {
// https://stackoverflow.com/questions/24410881/reading-in-a-json-file-using-swift
// To handle cases that provided filename has an extension
let name = filename.components(separatedBy: ".")[0]
let bundle = bundleClass != nil ? Bundle(for: bundleClass!) : Bundle.main
if let path = bundle.path(forResource: name, ofType: "json") {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let json = try JSONSerialization.jsonObject(with: data, options: readingOptions)
return json as? [String: Any]
}
return nil
}
#endif
/// SwifterSwift: Creates a unique directory for saving temporary files. The directory can be used to create
/// multiple temporary files used for a common purpose.
///
/// let tempDirectory = try fileManager.createTemporaryDirectory()
/// let tempFile1URL = tempDirectory.appendingPathComponent(ProcessInfo().globallyUniqueString)
/// let tempFile2URL = tempDirectory.appendingPathComponent(ProcessInfo().globallyUniqueString)
///
/// - Throws: An error if a temporary directory cannot be found or created.
/// - Returns: A URL to a new directory for saving temporary files.
func createTemporaryDirectory() throws -> URL {
#if !os(Linux)
return try url(for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: temporaryDirectory,
create: true)
#else
let envs = ProcessInfo.processInfo.environment
let env = envs["TMPDIR"] ?? envs["TEMP"] ?? envs["TMP"] ?? "/tmp"
let dir = "/\(env)/file-temp.XXXXXX"
var template = [UInt8](dir.utf8).map { Int8($0) } + [Int8(0)]
guard mkdtemp(&template) != nil else { throw CocoaError.error(.featureUnsupported) }
return URL(fileURLWithPath: String(cString: template))
#endif
}
}
#endif