File tree Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -8,9 +8,14 @@ var _readDir = Promise.promisify(RNFSManager.readDir);
8
8
var _stat = Promise . promisify ( RNFSManager . stat ) ;
9
9
var _readFile = Promise . promisify ( RNFSManager . readFile ) ;
10
10
var _writeFile = Promise . promisify ( RNFSManager . writeFile ) ;
11
+ var _unlink = Promise . promisify ( RNFSManager . unlink ) ;
11
12
12
13
var convertError = ( err ) => {
13
- var error = new Error ( err . description ) ;
14
+ if ( err . isOperational ) {
15
+ err = err . cause ;
16
+ }
17
+
18
+ var error = new Error ( err . description || err . message ) ;
14
19
error . code = err . code ;
15
20
throw error ;
16
21
} ;
@@ -57,6 +62,11 @@ var RNFS = {
57
62
. catch ( convertError ) ;
58
63
} ,
59
64
65
+ unlink ( filepath ) {
66
+ return _unlink ( filepath )
67
+ . catch ( convertError ) ;
68
+ } ,
69
+
60
70
MainBundle : RNFSManager . MainBundleDirectory ,
61
71
CachesDirectory : RNFSManager . NSCachesDirectory ,
62
72
DocumentDirectory : RNFSManager . NSDocumentDirectory ,
Original file line number Diff line number Diff line change @@ -69,7 +69,6 @@ @implementation RNFSManager
69
69
}
70
70
71
71
RCT_EXPORT_METHOD (writeFile:(NSString *)filepath contents:(NSString *)base64Content attributes:(NSDictionary *)attributes callback:(RCTResponseSenderBlock)callback){
72
-
73
72
NSData *data = [[NSData alloc ] initWithBase64EncodedString: base64Content options: NSDataBase64DecodingIgnoreUnknownCharacters ];
74
73
BOOL success = [[NSFileManager defaultManager ] createFileAtPath: filepath contents: data attributes: attributes];
75
74
@@ -80,10 +79,28 @@ @implementation RNFSManager
80
79
callback (@[[NSNull null ], [NSNumber numberWithBool: success]]);
81
80
}
82
81
82
+ RCT_EXPORT_METHOD (unlink:(NSString *)filepath callback:(RCTResponseSenderBlock)callback) {
83
+ NSFileManager *manager = [NSFileManager defaultManager ];
84
+ BOOL exists = [manager fileExistsAtPath: filepath isDirectory: false ];
85
+
86
+ if (!exists) {
87
+ return callback (@[[NSString stringWithFormat: @" File at path %@ does not exist" , filepath]]);
88
+ }
89
+ NSError *error;
90
+ BOOL success = [manager removeItemAtPath: filepath error: &error];
91
+
92
+ if (!success) {
93
+ NSLog (@" %@ " , error);
94
+ return callback ([self makeErrorPayload: error]);
95
+ }
96
+
97
+ callback (@[[NSNull null ], [NSNumber numberWithBool: success], filepath]);
98
+ }
99
+
83
100
RCT_EXPORT_METHOD (readFile:(NSString *)filepath callback:(RCTResponseSenderBlock)callback){
84
101
NSData *content = [[NSFileManager defaultManager ] contentsAtPath: filepath];
85
102
NSString *base64Content = [content base64EncodedStringWithOptions: NSDataBase64EncodingEndLineWithLineFeed ];
86
- NSLog ( @" %@ " , base64Content);
103
+
87
104
if (!base64Content) {
88
105
return callback (@[[NSString stringWithFormat: @" Could not read file at path %@ " , filepath]]);
89
106
}
You can’t perform that action at this time.
0 commit comments