Skip to content

Commit bacb952

Browse files
authored
Merge pull request #1 from houserater/brennan/file-protection-options
IOS: File Protection Options
2 parents 342b76d + f1448fa commit bacb952

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

FS.common.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ var normalizeFilePath = (path: string) => (path.startsWith('file://') ? path.sli
2929

3030
type MkdirOptions = {
3131
NSURLIsExcludedFromBackupKey?: boolean; // iOS only
32+
NSFileProtectionKey?: string; // IOS only
33+
};
34+
35+
type FileOptions = {
36+
NSFileProtectionKey?: string; // IOS only
3237
};
3338

3439
type ReadDirItem = {
@@ -185,12 +190,12 @@ var RNFS = {
185190
return RNFSManager.mkdir(normalizeFilePath(filepath), options).then(() => void 0);
186191
},
187192

188-
moveFile(filepath: string, destPath: string): Promise<void> {
189-
return RNFSManager.moveFile(normalizeFilePath(filepath), normalizeFilePath(destPath)).then(() => void 0);
193+
moveFile(filepath: string, destPath: string, options: FileOptions = {}): Promise<void> {
194+
return RNFSManager.moveFile(normalizeFilePath(filepath), normalizeFilePath(destPath), options).then(() => void 0);
190195
},
191196

192-
copyFile(filepath: string, destPath: string): Promise<void> {
193-
return RNFSManager.copyFile(normalizeFilePath(filepath), normalizeFilePath(destPath)).then(() => void 0);
197+
copyFile(filepath: string, destPath: string, options: FileOptions = {}): Promise<void> {
198+
return RNFSManager.copyFile(normalizeFilePath(filepath), normalizeFilePath(destPath), options).then(() => void 0);
194199
},
195200

196201
pathForBundle(bundleNamed: string): Promise<string> {
@@ -368,7 +373,10 @@ var RNFS = {
368373
if (typeof encodingOrOptions === 'string') {
369374
options.encoding = encodingOrOptions;
370375
} else if (typeof encodingOrOptions === 'object') {
371-
options = encodingOrOptions;
376+
options = {
377+
...options,
378+
...encodingOrOptions
379+
};
372380
}
373381
}
374382

@@ -382,7 +390,7 @@ var RNFS = {
382390
throw new Error('Invalid encoding type "' + options.encoding + '"');
383391
}
384392

385-
return RNFSManager.writeFile(normalizeFilePath(filepath), b64).then(() => void 0);
393+
return RNFSManager.writeFile(normalizeFilePath(filepath), b64, options).then(() => void 0);
386394
},
387395

388396
appendFile(filepath: string, contents: string, encodingOrOptions?: any): Promise<void> {
@@ -567,8 +575,8 @@ var RNFS = {
567575
ExternalStorageDirectoryPath: RNFSManager.RNFSExternalStorageDirectoryPath,
568576
TemporaryDirectoryPath: RNFSManager.RNFSTemporaryDirectoryPath,
569577
LibraryDirectoryPath: RNFSManager.RNFSLibraryDirectoryPath,
570-
PicturesDirectoryPath: RNFSManager.RNFSPicturesDirectoryPath
571-
578+
PicturesDirectoryPath: RNFSManager.RNFSPicturesDirectoryPath,
579+
FileProtectionKeys: RNFSManager.RNFSFileProtectionKeys
572580
};
573581

574582
module.exports = RNFS;

RNFSManager.m

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,19 @@ + (BOOL)requiresMainQueueSetup
109109

110110
RCT_EXPORT_METHOD(writeFile:(NSString *)filepath
111111
contents:(NSString *)base64Content
112+
options:(NSDictionary *)options
112113
resolver:(RCTPromiseResolveBlock)resolve
113114
rejecter:(RCTPromiseRejectBlock)reject)
114115
{
115116
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Content options:NSDataBase64DecodingIgnoreUnknownCharacters];
116117

117-
BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:nil];
118+
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
119+
120+
if ([options objectForKey:@"NSFileProtectionKey"]) {
121+
[attributes setValue:[options objectForKey:@"NSFileProtectionKey"] forKey:@"NSFileProtectionKey"];
122+
}
123+
124+
BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:attributes];
118125

119126
if (!success) {
120127
return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil);
@@ -220,8 +227,14 @@ + (BOOL)requiresMainQueueSetup
220227
{
221228
NSFileManager *manager = [NSFileManager defaultManager];
222229

230+
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
231+
232+
if ([options objectForKey:@"NSFileProtectionKey"]) {
233+
[attributes setValue:[options objectForKey:@"NSFileProtectionKey"] forKey:@"NSFileProtectionKey"];
234+
}
235+
223236
NSError *error = nil;
224-
BOOL success = [manager createDirectoryAtPath:filepath withIntermediateDirectories:YES attributes:nil error:&error];
237+
BOOL success = [manager createDirectoryAtPath:filepath withIntermediateDirectories:YES attributes:attributes error:&error];
225238

226239
if (!success) {
227240
return [self reject:reject withError:error];
@@ -385,6 +398,7 @@ + (BOOL)requiresMainQueueSetup
385398

386399
RCT_EXPORT_METHOD(moveFile:(NSString *)filepath
387400
destPath:(NSString *)destPath
401+
options:(NSDictionary *)options
388402
resolver:(RCTPromiseResolveBlock)resolve
389403
rejecter:(RCTPromiseRejectBlock)reject)
390404
{
@@ -397,11 +411,22 @@ + (BOOL)requiresMainQueueSetup
397411
return [self reject:reject withError:error];
398412
}
399413

414+
if ([options objectForKey:@"NSFileProtectionKey"]) {
415+
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
416+
[attributes setValue:[options objectForKey:@"NSFileProtectionKey"] forKey:@"NSFileProtectionKey"];
417+
BOOL updateSuccess = [manager setAttributes:attributes ofItemAtPath:destPath error:&error];
418+
419+
if (!updateSuccess) {
420+
return [self reject:reject withError:error];
421+
}
422+
}
423+
400424
resolve(nil);
401425
}
402426

403427
RCT_EXPORT_METHOD(copyFile:(NSString *)filepath
404428
destPath:(NSString *)destPath
429+
options:(NSDictionary *)options
405430
resolver:(RCTPromiseResolveBlock)resolve
406431
rejecter:(RCTPromiseRejectBlock)reject)
407432
{
@@ -414,6 +439,16 @@ + (BOOL)requiresMainQueueSetup
414439
return [self reject:reject withError:error];
415440
}
416441

442+
if ([options objectForKey:@"NSFileProtectionKey"]) {
443+
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
444+
[attributes setValue:[options objectForKey:@"NSFileProtectionKey"] forKey:@"NSFileProtectionKey"];
445+
BOOL updateSuccess = [manager setAttributes:attributes ofItemAtPath:destPath error:&error];
446+
447+
if (!updateSuccess) {
448+
return [self reject:reject withError:error];
449+
}
450+
}
451+
417452
resolve(nil);
418453
}
419454

@@ -868,8 +903,12 @@ - (NSDictionary *)constantsToExport
868903
@"RNFSTemporaryDirectoryPath": NSTemporaryDirectory(),
869904
@"RNFSLibraryDirectoryPath": [self getPathForDirectory:NSLibraryDirectory],
870905
@"RNFSFileTypeRegular": NSFileTypeRegular,
871-
@"RNFSFileTypeDirectory": NSFileTypeDirectory
872-
};
906+
@"RNFSFileTypeDirectory": NSFileTypeDirectory,
907+
@"RNFSFileProtectionComplete": NSFileProtectionComplete,
908+
@"RNFSFileProtectionCompleteUnlessOpen": NSFileProtectionCompleteUnlessOpen,
909+
@"RNFSFileProtectionCompleteUntilFirstUserAuthentication": NSFileProtectionCompleteUntilFirstUserAuthentication,
910+
@"RNFSFileProtectionNone": NSFileProtectionNone
911+
};
873912
}
874913

875914
+(void)setCompletionHandlerForIdentifier: (NSString *)identifier completionHandler: (CompletionHandler)completionHandler

0 commit comments

Comments
 (0)