forked from itinance/react-native-fs
/
RNFSManager.m
182 lines (148 loc) · 5.62 KB
/
RNFSManager.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
//
// RNFSManager.m
// RNFSManager
//
// Created by Johannes Lumpe on 08/05/15.
// Copyright (c) 2015 Johannes Lumpe. All rights reserved.
//
#import "RNFSManager.h"
#import "RCTBridge.h"
#import "NSArray+Map.h"
@implementation RNFSManager
static int MainBundleDirectory = 999;
RCT_EXPORT_MODULE();
- (dispatch_queue_t)methodQueue
{
return dispatch_queue_create("pe.lum.rnfs", DISPATCH_QUEUE_SERIAL);
}
RCT_EXPORT_METHOD(readDir:(NSString *)directory
inFolder:(nonnull NSNumber *)folder
callback:(RCTResponseSenderBlock)callback)
{
NSString *path;
NSInteger folderInt = [folder integerValue];
if (folderInt == MainBundleDirectory) {
path = [[NSBundle mainBundle] bundlePath];
} else {
NSArray *paths = NSSearchPathForDirectoriesInDomains(folderInt, NSUserDomainMask, YES);
path = [paths firstObject];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSString *dirPath = [path stringByAppendingPathComponent:directory];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:dirPath error:&error];
contents = [contents rnfs_mapObjectsUsingBlock:^id(NSString *obj, NSUInteger idx) {
return @{
@"name": obj,
@"path": [dirPath stringByAppendingPathComponent:obj]
};
}];
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]]);
}
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(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(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 @{
@"NSCachesDirectoryPath": [self getPathForDirectory:NSCachesDirectory],
@"NSDocumentDirectoryPath": [self getPathForDirectory:NSDocumentDirectory],
@"NSCachesDirectory": [NSNumber numberWithInteger:NSCachesDirectory],
@"NSDocumentDirectory": [NSNumber numberWithInteger:NSDocumentDirectory],
@"MainBundleDirectory": [NSNumber numberWithInteger:MainBundleDirectory],
@"NSFileTypeRegular": NSFileTypeRegular,
@"NSFileTypeDirectory": NSFileTypeDirectory
};
}
@end