From 4f3d375979250cf8d7302a781ad65ee821c76fe5 Mon Sep 17 00:00:00 2001 From: zenoxs Date: Sat, 15 Jun 2024 09:32:40 +0200 Subject: [PATCH 1/5] implement security scoped url with bookmark for readDir on iOS --- ios/ReactNativeFs.mm | 49 ++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/ios/ReactNativeFs.mm b/ios/ReactNativeFs.mm index 4e70bbe6..84667e28 100644 --- a/ios/ReactNativeFs.mm +++ b/ios/ReactNativeFs.mm @@ -45,29 +45,42 @@ - (instancetype) init resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) { - NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error = nil; + NSURL *dirUrl = [ReactNativeFs pathToUrl:dirPath error:&error]; + if (error) return [[RNFSException fromError:error] reject:reject]; - NSArray *contents = [fileManager contentsOfDirectoryAtPath:dirPath error:&error]; - NSMutableArray *tagetContents = [[NSMutableArray alloc] init]; - for (NSString *obj in contents) { - NSString *path = [dirPath stringByAppendingPathComponent:obj]; - NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:nil]; - if(attributes != nil) { - [tagetContents addObject:@{ - @"ctime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileCreationDate]], - @"mtime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileModificationDate]], - @"name": obj, - @"path": path, - @"size": [attributes objectForKey:NSFileSize], - @"type": [attributes objectForKey:NSFileType] - }]; + BOOL allowed = [dirUrl startAccessingSecurityScopedResource]; + + NSFileManager *fileManager = [NSFileManager defaultManager]; + + @try { + NSArray *contents = [fileManager contentsOfDirectoryAtPath:dirPath error:&error]; + NSMutableArray *tagetContents = [[NSMutableArray alloc] init]; + for (NSString *obj in contents) { + NSString *path = [dirUrl stringByAppendingPathComponent:obj]; + NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:nil]; + if(attributes != nil) { + [tagetContents addObject:@{ + @"ctime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileCreationDate]], + @"mtime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileModificationDate]], + @"name": obj, + @"path": path, + @"size": [attributes objectForKey:NSFileSize], + @"type": [attributes objectForKey:NSFileType] + }]; + } } - } - if (error) return [[RNFSException fromError:error] reject:reject]; + if (error) return [[RNFSException fromError:error] reject:reject]; - resolve(tagetContents); + resolve(tagetContents); + } + @catch (NSException *exception) { + reject(@"exception", exception.reason, nil); + } + @finally { + if (allowed) [dirUrl stopAccessingSecurityScopedResource]; + } } RCT_EXPORT_METHOD(exists:(NSString *)filepath From 71c9f2995cd50909d2fa3081f4cc1f07a3545cda Mon Sep 17 00:00:00 2001 From: "Dr. Sergey Pogodin" Date: Sat, 15 Jun 2024 14:28:46 +0200 Subject: [PATCH 2/5] v2.27.1: Version bump in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 301e31f5..ce9b46bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dr.pogodin/react-native-fs", - "version": "2.27.0", + "version": "2.27.1", "description": "Native filesystem access for React Native", "main": "lib/commonjs/index", "module": "lib/module/index", From 49bcc7e5695df46d4696565fab5d20a449f4fd5c Mon Sep 17 00:00:00 2001 From: "Dr. Sergey Pogodin" Date: Sat, 15 Jun 2024 14:33:34 +0200 Subject: [PATCH 3/5] iOS updates --- example/ios/Podfile.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 9f98cb36..3724a7ce 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1,7 +1,7 @@ PODS: - boost (1.83.0) - DoubleConversion (1.1.6) - - dr-pogodin-react-native-fs (2.27.0): + - dr-pogodin-react-native-fs (2.27.1): - DoubleConversion - glog - hermes-engine @@ -1391,7 +1391,7 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: boost: d3f49c53809116a5d38da093a8aa78bf551aed09 DoubleConversion: 76ab83afb40bddeeee456813d9c04f67f78771b5 - dr-pogodin-react-native-fs: ca26a09ad4b7feb4d7a27b2862273fa2c93396c4 + dr-pogodin-react-native-fs: 38c2789dd841955bfe1cc7b6ecef80277086c879 dr-pogodin-react-native-static-server: 064d84bba53f863504c6ea874549ea5a70405c42 FBLazyVector: 4bc164e5b5e6cfc288d2b5ff28643ea15fa1a589 fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120 From 650786b72535b9aadc2011321eee2d94967b1505 Mon Sep 17 00:00:00 2001 From: "Dr. Sergey Pogodin" Date: Sat, 15 Jun 2024 16:07:16 +0200 Subject: [PATCH 4/5] iOS: readDir() is refactored to correctly support security-scoped bookmark URLs --- ios/ReactNativeFs.mm | 48 ++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/ios/ReactNativeFs.mm b/ios/ReactNativeFs.mm index 84667e28..09c477dd 100644 --- a/ios/ReactNativeFs.mm +++ b/ios/ReactNativeFs.mm @@ -54,20 +54,42 @@ - (instancetype) init NSFileManager *fileManager = [NSFileManager defaultManager]; @try { - NSArray *contents = [fileManager contentsOfDirectoryAtPath:dirPath error:&error]; + NSArray *contents = [fileManager contentsOfDirectoryAtURL:dirUrl + includingPropertiesForKeys:@[ + NSURLContentModificationDateKey, NSURLCreationDateKey, + NSURLFileSizeKey, NSURLIsDirectoryKey, NSURLIsRegularFileKey + ] options:0 error:&error]; NSMutableArray *tagetContents = [[NSMutableArray alloc] init]; - for (NSString *obj in contents) { - NSString *path = [dirUrl stringByAppendingPathComponent:obj]; - NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:nil]; - if(attributes != nil) { - [tagetContents addObject:@{ - @"ctime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileCreationDate]], - @"mtime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileModificationDate]], - @"name": obj, - @"path": path, - @"size": [attributes objectForKey:NSFileSize], - @"type": [attributes objectForKey:NSFileType] - }]; + for (NSURL *url in contents) { + NSDictionary *attrs = [url resourceValuesForKeys:@[ + NSURLContentModificationDateKey, NSURLCreationDateKey, + NSURLFileSizeKey, NSURLIsDirectoryKey, NSURLIsRegularFileKey + ] error:nil]; + + if(attrs != nil) { + NSNumber *size = [attrs objectForKey:NSURLFileSizeKey]; + if (size == nil) size = @(64); + + NSString *path = url.resourceSpecifier; + + NSString *type = @"N/A"; + if ([[attrs objectForKey:NSURLIsRegularFileKey] boolValue]) + type = NSFileTypeRegular; + else if ([[attrs objectForKey:NSURLIsDirectoryKey] boolValue]) { + type = NSFileTypeDirectory; + + // Trims closing dash from the end of folder paths. + path = [path substringToIndex:[path length] - 1]; + } + + [tagetContents addObject:@{ + @"ctime": [self dateToTimeIntervalNumber:(NSDate *)[attrs objectForKey:NSURLCreationDateKey]], + @"mtime": [self dateToTimeIntervalNumber:(NSDate *)[attrs objectForKey:NSURLContentModificationDateKey]], + @"name": url.lastPathComponent, + @"path": path, + @"size": size, + @"type": type + }]; } } From aedb9f37a3344038d7b2262c2c7f006a07c048ba Mon Sep 17 00:00:00 2001 From: "Dr. Sergey Pogodin" Date: Sat, 15 Jun 2024 16:47:35 +0200 Subject: [PATCH 5/5] README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 31c9da3e..ee240aa4 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ and [old][Old Architecture] [RN][React Native] architectures. [](https://github.com/Crare) ### [Contributors](https://github.com/birdofpreyru/react-native-fs/graphs/contributors) +[](https://github.com/zenoxs) [](https://github.com/Crare) [](https://github.com/stetbern) [](https://github.com/raphaelheinz)