-
Notifications
You must be signed in to change notification settings - Fork 4
/
RNFileManager.m
306 lines (241 loc) · 10 KB
/
RNFileManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// RNFSManager.m
// RNFSManager
//
// Created by Johannes Lumpe on 08/05/15.
// Copyright (c) 2015 Johannes Lumpe. All rights reserved.
//
#import "RNFileManager.h"
#import "RCTBridge.h"
#import "NSArray+Map.h"
#import "Downloader.h"
#import "RCTEventDispatcher.h"
@interface RNFileManager()
@property (retain) NSMutableDictionary* downloaders;
@end
@implementation RNFileManager
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();
- (dispatch_queue_t)methodQueue
{
return dispatch_queue_create("pe.lum.rnfs", DISPATCH_QUEUE_SERIAL);
}
RCT_EXPORT_METHOD(readDir:(NSString *)dirPath
callback:(RCTResponseSenderBlock)callback)
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *contents = [fileManager contentsOfDirectoryAtPath:dirPath error:&error];
contents = [contents rnfs_mapObjectsUsingBlock:^id(NSString *obj, NSUInteger idx) {
NSString *path = [dirPath stringByAppendingPathComponent:obj];
NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:nil];
return @{
@"name": obj,
@"path": path,
@"size": [attributes objectForKey:NSFileSize],
@"type": [attributes objectForKey:NSFileType]
};
}];
if (error) {
return callback([self makeErrorPayload:error]);
}
callback(@[[NSNull null], contents]);
}
RCT_EXPORT_METHOD(stat:(NSString *)filepath
callback:(RCTResponseSenderBlock)callback)
{
NSError *error = nil;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error];
if (error) {
return callback([self makeErrorPayload:error]);
}
attributes = @{
@"ctime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileCreationDate]],
@"mtime": [self dateToTimeIntervalNumber:(NSDate *)[attributes objectForKey:NSFileModificationDate]],
@"size": [attributes objectForKey:NSFileSize],
@"type": [attributes objectForKey:NSFileType],
@"mode": @([[NSString stringWithFormat:@"%ld", (long)[(NSNumber *)[attributes objectForKey:NSFilePosixPermissions] integerValue]] integerValue])
};
callback(@[[NSNull null], attributes]);
}
RCT_EXPORT_METHOD(writeFile:(NSString *)filepath
contents:(NSString *)base64Content
attributes:(NSDictionary *)attributes
callback:(RCTResponseSenderBlock)callback)
{
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Content options:NSDataBase64DecodingIgnoreUnknownCharacters];
BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:attributes];
if (!success) {
return callback(@[[NSString stringWithFormat:@"Could not write file at path %@", filepath]]);
}
callback(@[[NSNull null], [NSNumber numberWithBool:success], filepath]);
}
RCT_EXPORT_METHOD(unlink:(NSString*)filepath
callback:(RCTResponseSenderBlock)callback)
{
NSFileManager *manager = [NSFileManager defaultManager];
BOOL exists = [manager fileExistsAtPath:filepath isDirectory:false];
if (!exists) {
return callback(@[[NSString stringWithFormat:@"File at path %@ does not exist", filepath]]);
}
NSError *error = nil;
BOOL success = [manager removeItemAtPath:filepath error:&error];
if (!success) {
return callback([self makeErrorPayload:error]);
}
callback(@[[NSNull null], [NSNumber numberWithBool:success], filepath]);
}
RCT_EXPORT_METHOD(mkdir:(NSString*)filepath
excludeFromBackup:(BOOL)excludeFromBackup
callback:(RCTResponseSenderBlock)callback)
{
NSFileManager *manager = [NSFileManager defaultManager];
NSError *error = nil;
BOOL success = [manager createDirectoryAtPath:filepath withIntermediateDirectories:YES attributes:nil error:&error];
if (!success) {
return callback([self makeErrorPayload:error]);
}
NSURL *url = [NSURL fileURLWithPath:filepath];
success = [url setResourceValue: [NSNumber numberWithBool: excludeFromBackup] forKey: NSURLIsExcludedFromBackupKey error: &error];
if (!success) {
return callback([self makeErrorPayload:error]);
}
callback(@[[NSNull null], [NSNumber numberWithBool:success], filepath]);
}
RCT_EXPORT_METHOD(readFile:(NSString *)filepath
callback:(RCTResponseSenderBlock)callback)
{
NSData *content = [[NSFileManager defaultManager] contentsAtPath:filepath];
NSString *base64Content = [content base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
if (!base64Content) {
return callback(@[[NSString stringWithFormat:@"Could not read file at path %@", filepath]]);
}
callback(@[[NSNull null], base64Content]);
}
RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr
filepath:(NSString *)filepath
jobId:(nonnull NSNumber *)jobId
callback:(RCTResponseSenderBlock)callback)
{
DownloadParams* params = [DownloadParams alloc];
params.fromUrl = urlStr;
params.toFile = filepath;
params.callback = ^(NSNumber* statusCode, NSNumber* bytesWritten) {
if (bytesWritten == nil) {
NSError *error = [NSError errorWithDomain:@"vrrv" code:0 userInfo:nil];
return callback([self makeErrorPayload:error]);
}
return callback(@[[NSNull null], @{@"jobId": jobId,
@"statusCode": (statusCode ? statusCode : [NSNumber numberWithInt:0]),
@"bytesWritten": (bytesWritten ? bytesWritten : [NSNumber numberWithInt:0])}]);
};
params.errorCallback = ^(NSError* error) {
return callback([self makeErrorPayload:error]);
};
params.beginCallback = ^(NSNumber* statusCode, NSNumber* contentLength, NSDictionary* headers) {
[self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"DownloadBegin-%@", jobId]
body:@{@"jobId": jobId,
@"statusCode": statusCode,
@"contentLength": contentLength,
@"headers": headers}];
};
params.progressCallback = ^(NSNumber* contentLength, NSNumber* bytesWritten) {
[self.bridge.eventDispatcher sendAppEventWithName:[NSString stringWithFormat:@"DownloadProgress-%@", jobId]
body:@{@"contentLength": contentLength,
@"bytesWritten": bytesWritten}];
};
if (!self.downloaders) self.downloaders = [[NSMutableDictionary alloc] init];
Downloader* downloader = [Downloader alloc];
[downloader downloadFile:params];
[self.downloaders setValue:downloader forKey:[jobId stringValue]];
}
RCT_EXPORT_METHOD(stopDownload:(nonnull NSNumber *)jobId)
{
Downloader* downloader = [self.downloaders objectForKey:[jobId stringValue]];
if (downloader != nil) {
[downloader stopDownload];
}
}
RCT_EXPORT_METHOD(exists:(NSString *)filepath
callback:(RCTResponseSenderBlock)callback)
{
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:filepath];
callback(@[[NSNull null], [NSNumber numberWithBool:exists]]);
}
RCT_EXPORT_METHOD(folderExists:(NSString *)filepath
callback:(RCTResponseSenderBlock)callback)
{
BOOL isDirectory;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:filepath isDirectory:&isDirectory];
if (exists && isDirectory) {
callback(@[[NSNull null], [NSNumber numberWithBool:YES]]);
} else {
callback(@[[NSNull null], [NSNumber numberWithBool:NO]]);
}
}
RCT_EXPORT_METHOD(rename:(NSString *)filepath
to:(NSString *)newname
callback:(RCTResponseSenderBlock)callback)
{
NSString *newPath = [filepath.stringByDeletingLastPathComponent stringByAppendingPathComponent:newname];
NSError *error = nil;
BOOL success = [[NSFileManager defaultManager] moveItemAtPath:filepath toPath:newPath error:&error];
callback(@[[NSNull null], [NSNumber numberWithBool:success]]);
}
RCT_EXPORT_METHOD(moveFile:(NSString *)filepath
to:(NSString *)newPath
callback:(RCTResponseSenderBlock)callback)
{
NSError *error = nil;
BOOL success = [[NSFileManager defaultManager] moveItemAtPath:filepath toPath:newPath error:&error];
callback(@[[NSNull null], [NSNumber numberWithBool:success]]);
}
RCT_EXPORT_METHOD(pathForBundle:(NSString *)bundleNamed
callback:(RCTResponseSenderBlock)callback)
{
NSString *path = [[NSBundle mainBundle].bundlePath stringByAppendingFormat:@"/%@.bundle", bundleNamed];
NSBundle *bundle = [NSBundle bundleWithPath:path];
if (!bundle) {
bundle = [NSBundle bundleForClass:NSClassFromString(bundleNamed)];
path = bundle.bundlePath;
}
if (!bundle.isLoaded) {
[bundle load];
}
if (path) {
callback(@[[NSNull null], path]);
} else {
callback(@[[NSError errorWithDomain:NSPOSIXErrorDomain
code:NSFileNoSuchFileError
userInfo:nil].localizedDescription,
[NSNull null]]);
}
}
- (NSNumber *)dateToTimeIntervalNumber:(NSDate *)date
{
return @([date timeIntervalSince1970]);
}
- (NSArray *)makeErrorPayload:(NSError *)error
{
return @[@{
@"description": error.localizedDescription,
@"code": @(error.code)
}];
}
- (NSString *)getPathForDirectory:(int)directory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
return [paths firstObject];
}
- (NSDictionary *)constantsToExport
{
return @{
@"MainBundlePath": [[NSBundle mainBundle] bundlePath],
@"NSCachesDirectoryPath": [self getPathForDirectory:NSCachesDirectory],
@"NSDocumentDirectoryPath": [self getPathForDirectory:NSDocumentDirectory],
@"NSApplicationSupportDirectoryPath": [self getPathForDirectory:NSApplicationSupportDirectory],
@"NSFileTypeRegular": NSFileTypeRegular,
@"NSFileTypeDirectory": NSFileTypeDirectory
};
}
@end