Skip to content

Commit 8cebde2

Browse files
author
Preben Ludviksen
committed
Added a constant-time aboslutePath-method, and separated Auxiliary and Important-storages on iOS by the cunning use of folders.
1 parent 83a3724 commit 8cebde2

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

android/src/main/java/com/benwixen/rnfilesystem/RNFileSystem.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.io.FileOutputStream;
1111
import java.io.IOException;
1212
import java.io.OutputStreamWriter;
13+
import java.util.HashMap;
14+
import java.util.Map;
1315
import java.util.Scanner;
1416

1517
public class RNFileSystem extends ReactContextBaseJavaModule {
@@ -25,6 +27,16 @@ public enum Storage {
2527
TEMPORARY
2628
}
2729

30+
@Override
31+
public Map<String, Object> getConstants() {
32+
final Map<String, Object> constants = new HashMap<>();
33+
constants.put(Storage.BACKED_UP.toString(), baseDirForStorage(Storage.BACKED_UP));
34+
constants.put(Storage.IMPORTANT.toString(), baseDirForStorage(Storage.IMPORTANT));
35+
constants.put(Storage.AUXILIARY.toString(), baseDirForStorage(Storage.AUXILIARY));
36+
constants.put(Storage.TEMPORARY.toString(), baseDirForStorage(Storage.TEMPORARY));
37+
return constants;
38+
}
39+
2840
@Override
2941
public String getName() {
3042
return "RNFileSystem";

ios/RNFileSystem/RNFileSystem.m

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@
77

88
@implementation RNFileSystem
99

10+
- (NSDictionary<NSString *, NSString *> *)constantsToExport {
11+
return @{STORAGE_BACKED_UP: [[RNFileSystem baseDirForStorage:STORAGE_BACKED_UP] path],
12+
STORAGE_IMPORTANT: [[RNFileSystem baseDirForStorage:STORAGE_IMPORTANT] path],
13+
STORAGE_AUXILIARY: [[RNFileSystem baseDirForStorage:STORAGE_AUXILIARY] path],
14+
STORAGE_TEMPORARY: [[RNFileSystem baseDirForStorage:STORAGE_TEMPORARY] path]};
15+
}
16+
17+
1018
+ (NSURL*)baseDirForStorage:(NSString*)storage {
1119
NSFileManager *fileManager = [NSFileManager defaultManager];
1220
if ([storage isEqual:STORAGE_BACKED_UP]) {
1321
return [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
14-
} else if ([storage isEqual:STORAGE_IMPORTANT] || [storage isEqual:STORAGE_AUXILIARY]) {
15-
return [fileManager URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
22+
} else if ([storage isEqual:STORAGE_IMPORTANT]) {
23+
NSURL *cachesDir = [fileManager URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
24+
return [cachesDir URLByAppendingPathComponent:@"Important"];
25+
} else if ([storage isEqual:STORAGE_AUXILIARY]) {
26+
NSURL *cachesDir = [fileManager URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
27+
return [cachesDir URLByAppendingPathComponent:@"Auxiliary"];
1628
} else if ([storage isEqual:STORAGE_TEMPORARY]) {
1729
return [NSURL fileURLWithPath:NSTemporaryDirectory()];
1830
} else {
@@ -45,7 +57,8 @@ + (NSString*)readFile:(NSString*)relativePath inStorage:(NSString*)storage error
4557
NSFileManager *fileManager = [NSFileManager defaultManager];
4658
BOOL fileExists = [fileManager fileExistsAtPath:[fullPath path]];
4759
if (!fileExists) {
48-
NSDictionary *errorDetail = @{NSLocalizedDescriptionKey: @"File does not exist."};
60+
NSString* errorMessage = [NSString stringWithFormat:@"File '%@' does not exist in storage: %@", relativePath, storage];
61+
NSDictionary *errorDetail = @{NSLocalizedDescriptionKey: errorMessage};
4962
*error = [NSError errorWithDomain:@"FSComponent" code:1 userInfo:errorDetail];
5063
return nil;
5164
}

modules/FileSystem.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ export default class FileSystem {
3131
static async directoryExists(path, storage = FileSystem.storage.backedUp) {
3232
return await RNFileSystem.directoryExists(path, storage);
3333
}
34+
35+
static absolutePath(path, storage = FileSystem.storage.backedUp) {
36+
return RNFileSystem[storage] + '/' + path;
37+
}
3438
}

0 commit comments

Comments
 (0)