alpha0010 / react-native-file-access

Filesystem access for React Native
MIT License
298 stars 18 forks source link

Error when calling statDir function with many files #69

Closed Arjan-Zuidema closed 1 year ago

Arjan-Zuidema commented 1 year ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch react-native-file-access@3.0.3 for the project I'm working on.

Sometimes when requesting the statDir function (with many files) some of the resulting files from contentsOfDirectory would not exists (anymore) which results in an error when trying to call statFile.

This patch fixes that issue. I don't know if this patch is the right way to handle the errors. Please let me know so I can create a pull request if needed.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-file-access/ios/FileAccess.swift b/node_modules/react-native-file-access/ios/FileAccess.swift
index a518891..02202d7 100644
--- a/node_modules/react-native-file-access/ios/FileAccess.swift
+++ b/node_modules/react-native-file-access/ios/FileAccess.swift
@@ -285,7 +285,16 @@ public class FileAccess : NSObject {
             let base = URL(fileURLWithPath: path.path())
             do {
                 try resolve(FileManager.default.contentsOfDirectory(atPath: path.path())
-                    .map { try self.statFile(path: base.appendingPathComponent($0).path) }
+                    .map {
+                        var isDir: ObjCBool = false
+                        let exists = FileManager.default.fileExists(atPath: base.appendingPathComponent($0).path, isDirectory: &isDir)
+                        
+                        if exists {
+                            return try self.statFile(path: base.appendingPathComponent($0).path)
+                        }
+                        
+                        return [:];
+                    }
                 )
             } catch {
                 reject("ERR", "Failed to list '\(path)'.", error)

This issue body was partially generated by patch-package.

alpha0010 commented 1 year ago

Could you test if the following fixes your issue?

diff --git a/ios/FileAccess.swift b/ios/FileAccess.swift
index a518891..e6ec35c 100644
--- a/ios/FileAccess.swift
+++ b/ios/FileAccess.swift
@@ -285,7 +285,7 @@ public class FileAccess : NSObject {
             let base = URL(fileURLWithPath: path.path())
             do {
                 try resolve(FileManager.default.contentsOfDirectory(atPath: path.path())
-                    .map { try self.statFile(path: base.appendingPathComponent($0).path) }
+                    .compactMap { try? self.statFile(path: base.appendingPathComponent($0).path) }
                 )
             } catch {
                 reject("ERR", "Failed to list '\(path)'.", error)
Arjan-Zuidema commented 1 year ago

Your patch also works fine and seems a bit cleaner :)