@@ -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+
0 commit comments