Skip to content

Commit f81924f

Browse files
chore: fix file not found exception
1 parent 3f2dd63 commit f81924f

File tree

4 files changed

+67
-147
lines changed

4 files changed

+67
-147
lines changed

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PODS:
88
- hermes-engine (0.77.3):
99
- hermes-engine/Pre-built (= 0.77.3)
1010
- hermes-engine/Pre-built (0.77.3)
11-
- MendixNative (0.1.2):
11+
- MendixNative (0.1.3):
1212
- DoubleConversion
1313
- glog
1414
- hermes-engine
@@ -1825,7 +1825,7 @@ SPEC CHECKSUMS:
18251825
fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd
18261826
glog: eb93e2f488219332457c3c4eafd2738ddc7e80b8
18271827
hermes-engine: b2187dbe13edb0db8fcb2a93a69c1987a30d98a4
1828-
MendixNative: 0405210432ee514e2d7906fe5714f719eb6d7c75
1828+
MendixNative: 36190d86a65cb57b351c6396bc1349a7823206b0
18291829
op-sqlite: 12554de3e1a0cb86cbad3cf1f0c50450f57d3855
18301830
OpenSSL-Universal: 6082b0bf950e5636fe0d78def171184e2b3899c2
18311831
RCT-Folly: e78785aa9ba2ed998ea4151e314036f6c49e6d82

ios/Modules/Helper/StorageHelper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public class StorageHelper {
1919

2020
public static func clearDataAt(url: URL, component: String) {
2121
let path = url.appendingPathComponent(component).path
22-
_ = NativeFsModule.remove(path, error: nil)
22+
try? NativeFsModule.remove(path)
2323
}
2424
}

ios/Modules/NativeFsModule/NativeFsModule.swift

Lines changed: 61 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -49,87 +49,38 @@ public class NativeFsModule: NSObject {
4949
}
5050
}
5151

52-
static func readJson(_ filePath: String, error: NSErrorPointer) -> [String: Any]? {
52+
static func readJson(_ filePath: String) throws -> [String: Any]? {
5353
guard let data = readData(filePath) else {
5454
return nil
5555
}
56-
57-
do {
58-
let result = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
59-
return result as? [String: Any]
60-
} catch let jsonError {
61-
error?.pointee = jsonError as NSError
62-
return nil
63-
}
56+
let result = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
57+
return result as? [String: Any]
6458
}
6559

66-
static func save(_ data: Data, filepath: String, error: NSErrorPointer) -> Bool {
60+
static func save(_ data: Data, filepath: String) throws {
6761
let directoryURL = URL(fileURLWithPath: (filepath as NSString).deletingLastPathComponent)
68-
69-
do {
70-
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
71-
} catch let directoryError {
72-
error?.pointee = directoryError as NSError
73-
return false
74-
}
75-
76-
var options: Data.WritingOptions = .atomic
77-
if encryptionEnabled {
78-
options = [.atomic, .completeFileProtection]
79-
}
80-
81-
do {
82-
try data.write(to: URL(fileURLWithPath: filepath), options: options)
83-
return true
84-
} catch let writeError {
85-
error?.pointee = writeError as NSError
86-
return false
87-
}
62+
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
63+
let options: Data.WritingOptions = encryptionEnabled ? [.atomic, .completeFileProtection] : .atomic
64+
try data.write(to: URL(fileURLWithPath: filepath), options: options)
8865
}
8966

90-
static func move(_ filepath: String, newPath: String, error: NSErrorPointer) -> Bool {
67+
static func move(_ filepath: String, newPath: String) throws {
9168
let fileManager = FileManager.default
92-
9369
guard fileManager.fileExists(atPath: filepath) else {
94-
error?.pointee = NSError(domain: NativeFsErrorDomain, code: -1, userInfo: [NSLocalizedDescriptionKey: "File does not exist"])
95-
return false
70+
throw NSError(domain: NativeFsErrorDomain, code: -1, userInfo: [NSLocalizedDescriptionKey: "File does not exist"])
9671
}
97-
9872
let directoryURL = URL(fileURLWithPath: (newPath as NSString).deletingLastPathComponent)
99-
100-
do {
101-
try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
102-
} catch let directoryError {
103-
error?.pointee = directoryError as NSError
104-
return false
105-
}
106-
107-
do {
108-
try fileManager.moveItem(atPath: filepath, toPath: newPath)
109-
return true
110-
} catch let moveError {
111-
error?.pointee = moveError as NSError
112-
return false
113-
}
73+
try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
74+
try fileManager.moveItem(atPath: filepath, toPath: newPath)
11475
}
11576

116-
static func remove(_ filepath: String, error: NSErrorPointer) -> Bool {
77+
static func remove(_ filepath: String) throws {
11778
let fileManager = FileManager.default
118-
119-
guard fileManager.fileExists(atPath: filepath) else {
120-
return false
121-
}
122-
123-
do {
124-
try fileManager.removeItem(atPath: filepath)
125-
return true
126-
} catch let removeError {
127-
error?.pointee = removeError as NSError
128-
return false
129-
}
79+
guard fileManager.fileExists(atPath: filepath) else { return }
80+
try fileManager.removeItem(atPath: filepath)
13081
}
13182

132-
static func ensureWhiteListedPath(_ paths: [String], error: NSErrorPointer) -> Bool {
83+
static func ensureWhiteListedPath(_ paths: [String]) throws {
13384
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first ?? ""
13485
let cachesPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first ?? ""
13586
let tempPath = (NSTemporaryDirectory() as NSString).standardizingPath
@@ -138,15 +89,13 @@ public class NativeFsModule: NSObject {
13889
if !path.hasPrefix(documentsPath) &&
13990
!path.hasPrefix(cachesPath) &&
14091
!path.hasPrefix(tempPath) {
141-
error?.pointee = NSError(
92+
throw NSError(
14293
domain: NativeFsErrorDomain,
14394
code: 999,
14495
userInfo: [NSLocalizedDescriptionKey: "The path \(path) does not point to the documents directory"]
14596
)
146-
return false
14797
}
14898
}
149-
return true
15099
}
151100

152101
static func list(_ dirPath: String) -> [String] {
@@ -169,34 +118,26 @@ public class NativeFsModule: NSObject {
169118
reject: @escaping RCTPromiseRejectBlock
170119
) {
171120

172-
var error: NSError?
173-
if !NativeFsModule.ensureWhiteListedPath([filepath], error: &error) {
174-
reject(NativeFsModule.INVALID_PATH, NativeFsModule.formatError("Path not accessible"), error)
175-
return
176-
}
121+
guard isWhiteListedPath(filepath, reject: reject) else { return }
177122

178123
guard let data = readBlobRefAsData(blob) else {
179124
reject(NativeFsModule.ERROR_READ_FAILED, NativeFsModule.formatError("Failed to read blob"), nil)
180125
return
181126
}
182127

183-
if !NativeFsModule.save(data, filepath: filepath, error: &error) {
128+
do {
129+
try NativeFsModule.save(data, filepath: filepath)
130+
resolve(nil)
131+
} catch {
184132
reject(NativeFsModule.ERROR_SAVE_FAILED, NativeFsModule.formatError("Save failed"), error)
185-
return
186133
}
187-
188-
resolve(nil)
189134
}
190135

191136
public func read(_ filepath: String,
192137
resolve: @escaping RCTPromiseResolveBlock,
193138
reject: @escaping RCTPromiseRejectBlock) {
194139

195-
var error: NSError?
196-
if !NativeFsModule.ensureWhiteListedPath([filepath], error: &error) {
197-
reject(NativeFsModule.INVALID_PATH, NativeFsModule.formatError("Path not accessible"), error)
198-
return
199-
}
140+
guard isWhiteListedPath(filepath, reject: reject) else { return }
200141

201142
guard let data = NativeFsModule.readData(filepath) else {
202143
resolve(nil)
@@ -216,45 +157,33 @@ public class NativeFsModule: NSObject {
216157
resolve: @escaping RCTPromiseResolveBlock,
217158
reject: @escaping RCTPromiseRejectBlock) {
218159

219-
var error: NSError?
220-
if !NativeFsModule.ensureWhiteListedPath([filepath, newPath], error: &error) {
221-
reject(NativeFsModule.INVALID_PATH, NativeFsModule.formatError("Path not accessible"), error)
222-
return
223-
}
160+
guard isWhiteListedPath(filepath, newPath, reject: reject) else { return }
224161

225-
if !NativeFsModule.move(filepath, newPath: newPath, error: &error) {
162+
do {
163+
try NativeFsModule.move(filepath, newPath: newPath)
164+
resolve(nil)
165+
} catch {
226166
reject(NativeFsModule.ERROR_MOVE_FAILED, NativeFsModule.formatError("Failed to move file"), error)
227-
return
228167
}
229-
230-
resolve(nil)
231168
}
232169

233170
public func remove(_ filepath: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
234171

235-
var error: NSError?
236-
if !NativeFsModule.ensureWhiteListedPath([filepath], error: &error) {
237-
reject(NativeFsModule.INVALID_PATH, NativeFsModule.formatError("Path not accessible"), error)
238-
return
239-
}
172+
guard isWhiteListedPath(filepath, reject: reject) else { return }
240173

241-
if !NativeFsModule.remove(filepath, error: &error) {
174+
do {
175+
try NativeFsModule.remove(filepath)
176+
resolve(nil)
177+
} catch {
242178
reject(NativeFsModule.ERROR_DELETE_FAILED, NativeFsModule.formatError("Failed to delete file"), error)
243-
return
244179
}
245-
246-
resolve(nil)
247180
}
248181

249182
public func list(_ dirPath: String,
250183
resolve: @escaping RCTPromiseResolveBlock,
251184
reject: @escaping RCTPromiseRejectBlock) {
252185

253-
var error: NSError?
254-
if !NativeFsModule.ensureWhiteListedPath([dirPath], error: &error) {
255-
reject(NativeFsModule.INVALID_PATH, NativeFsModule.formatError("Path not accessible"), error)
256-
return
257-
}
186+
guard isWhiteListedPath(dirPath, reject: reject) else { return }
258187

259188
resolve(NativeFsModule.list(dirPath))
260189
}
@@ -263,11 +192,7 @@ public class NativeFsModule: NSObject {
263192
resolve: @escaping RCTPromiseResolveBlock,
264193
reject: @escaping RCTPromiseRejectBlock) {
265194

266-
var error: NSError?
267-
if !NativeFsModule.ensureWhiteListedPath([filePath], error: &error) {
268-
reject(NativeFsModule.INVALID_PATH, NativeFsModule.formatError("Path not accessible"), error)
269-
return
270-
}
195+
guard isWhiteListedPath(filePath, reject: reject) else { return }
271196

272197
guard let data = NativeFsModule.readData(filePath) else {
273198
resolve(nil)
@@ -282,12 +207,7 @@ public class NativeFsModule: NSObject {
282207
public func fileExists(_ filepath: String,
283208
resolve: @escaping RCTPromiseResolveBlock,
284209
reject: @escaping RCTPromiseRejectBlock) {
285-
286-
var error: NSError?
287-
if !NativeFsModule.ensureWhiteListedPath([filepath], error: &error) {
288-
reject(NativeFsModule.INVALID_PATH, NativeFsModule.formatError("Path not accessible"), error)
289-
return
290-
}
210+
guard isWhiteListedPath(filepath, reject: reject) else { return }
291211

292212
let exists = FileManager.default.fileExists(atPath: filepath)
293213
resolve(NSNumber(value: exists))
@@ -297,46 +217,35 @@ public class NativeFsModule: NSObject {
297217
resolve: @escaping RCTPromiseResolveBlock,
298218
reject: @escaping RCTPromiseRejectBlock) {
299219

300-
var error: NSError?
301-
if !NativeFsModule.ensureWhiteListedPath([filepath], error: &error) {
302-
reject(NativeFsModule.INVALID_PATH, NativeFsModule.formatError("Path not accessible"), error)
303-
return
304-
}
220+
guard isWhiteListedPath(filepath, reject: reject) else { return }
305221

306-
guard let data = NativeFsModule.readJson(filepath, error: &error) else {
307-
if let error = error {
308-
reject(NativeFsModule.ERROR_SERIALIZATION_FAILED, NativeFsModule.formatError("Failed to deserialize JSON"), error)
309-
} else {
310-
resolve(nil)
311-
}
312-
return
222+
do {
223+
let data = try NativeFsModule.readJson(filepath)
224+
resolve(data)
225+
} catch {
226+
reject(NativeFsModule.ERROR_SERIALIZATION_FAILED, NativeFsModule.formatError("Failed to deserialize JSON"), error)
313227
}
314-
315-
resolve(data)
316228
}
317229

318230
public func writeJson(_ data: [String: Any],
319231
filepath: String,
320232
resolve: @escaping RCTPromiseResolveBlock,
321233
reject: @escaping RCTPromiseRejectBlock) {
322234

323-
var error: NSError?
324-
if !NativeFsModule.ensureWhiteListedPath([filepath], error: &error) {
325-
reject(NativeFsModule.INVALID_PATH, "Path not accessible", error)
235+
guard isWhiteListedPath(filepath, reject: reject) else { return }
236+
237+
var jsonData: Data
238+
do {
239+
jsonData = try JSONSerialization.data(withJSONObject: data, options: .prettyPrinted)
240+
} catch {
241+
reject(NativeFsModule.ERROR_SERIALIZATION_FAILED, NativeFsModule.formatError("Failed to serialize JSON"), error)
326242
return
327243
}
328244

329245
do {
330-
let jsonData = try JSONSerialization.data(withJSONObject: data, options: .prettyPrinted)
331-
332-
if !NativeFsModule.save(jsonData, filepath: filepath, error: &error) {
333-
reject(NativeFsModule.ERROR_SAVE_FAILED, NativeFsModule.formatError("Failed to write JSON"), error)
334-
return
335-
}
336-
337-
resolve(nil)
246+
try NativeFsModule.save(jsonData, filepath: filepath)
338247
} catch {
339-
reject(NativeFsModule.ERROR_SERIALIZATION_FAILED, NativeFsModule.formatError("Failed to serialize JSON"), error)
248+
reject(NativeFsModule.ERROR_SAVE_FAILED, NativeFsModule.formatError("Failed to write JSON"), error)
340249
}
341250
}
342251

@@ -345,4 +254,15 @@ public class NativeFsModule: NSObject {
345254
"SUPPORTS_DIRECTORY_MOVE": true,
346255
"SUPPORTS_ENCRYPTION": true
347256
]
257+
258+
private func isWhiteListedPath(_ paths: String..., reject: RCTPromiseRejectBlock) -> Bool {
259+
do {
260+
try NativeFsModule.ensureWhiteListedPath(paths)
261+
return true
262+
} catch let error {
263+
reject(NativeFsModule.INVALID_PATH, NativeFsModule.formatError("Path not accessible"), error)
264+
return false
265+
}
266+
}
348267
}
268+

ios/Modules/NativeOtaModule/OtaHelpers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class OtaHelpers: NSObject {
5050
}
5151

5252
static func getNativeDependencies() -> [String: Any] {
53-
guard let path = Bundle.main.path(forResource: "native_dependencies", ofType: "json") else {
53+
guard let path = Bundle.main.path(forResource: "native_dependencies", ofType: "json"),
54+
let data = try? NativeFsModule.readJson(path) else {
5455
return [:]
5556
}
56-
57-
return NativeFsModule.readJson(path, error: nil) ?? [:]
57+
return data
5858
}
5959
}

0 commit comments

Comments
 (0)