浏览代码

#2 ios implementation wip commit

Ben Hsieh 8 年前
父节点
当前提交
53d6eb23de
共有 2 个文件被更改,包括 74 次插入15 次删除
  1. 16
    0
      src/ios/RNFetchBlob/RNFetchBlob.h
  2. 58
    15
      src/ios/RNFetchBlob/RNFetchBlob.m

+ 16
- 0
src/ios/RNFetchBlob/RNFetchBlob.h 查看文件

@@ -9,6 +9,22 @@
9 9
 #import <Foundation/Foundation.h>
10 10
 #import "RCTBridgeModule.h"
11 11
 
12
+// lib event
13
+extern NSString *const MSG_EVENT;
14
+extern NSString *const MSG_EVENT_LOG;
15
+extern NSString *const MSG_EVENT_WARN;
16
+extern NSString *const MSG_EVENT_ERROR;
17
+
18
+// config
19
+extern NSString *const CONFIG_USE_TEMP;
20
+extern NSString *const CONFIG_FILE_PATH;
21
+
22
+// fs events
23
+extern NSString *const FS_EVENT_DATA;
24
+extern NSString *const FS_EVENT_END;
25
+extern NSString *const FS_EVENT_WARN;
26
+extern NSString *const FS_EVENT_ERROR;
27
+
12 28
 @interface FetchBlobFS : NSObject <NSStreamDelegate>  {
13 29
     NSOutputStream * outStream;
14 30
     NSInputStream * inStream;

+ 58
- 15
src/ios/RNFetchBlob/RNFetchBlob.m 查看文件

@@ -1,7 +1,7 @@
1 1
 //
2 2
 //  RNFetchBlob.m
3 3
 //
4
-//  Created by suzuri04x2 on 2016/4/28.
4
+//  Created by wkh237 on 2016/4/28.
5 5
 //
6 6
 
7 7
 #import "RNFetchBlob.h"
@@ -11,6 +11,17 @@
11 11
 #import "RCTBridge.h"
12 12
 #import "RCTEventDispatcher.h"
13 13
 
14
+// lib event
15
+NSString *const MSG_EVENT = @"RNFetchBlobMessage";
16
+NSString *const MSG_EVENT_LOG = @"log";
17
+NSString *const MSG_EVENT_WARN = @"warn";
18
+NSString *const MSG_EVENT_ERROR = @"error";
19
+NSString *const CONFIG_USE_TEMP = @"fileCache";
20
+NSString *const CONFIG_FILE_PATH = @"path";
21
+NSString *const FS_EVENT_DATA = @"data";
22
+NSString *const FS_EVENT_END = @"end";
23
+NSString *const FS_EVENT_WARN = @"warn";
24
+NSString *const FS_EVENT_ERROR = @"error";
14 25
 
15 26
 ////////////////////////////////////////
16 27
 //
@@ -27,9 +38,33 @@
27 38
 @synthesize taskId;
28 39
 @synthesize path;
29 40
 
41
+
42
+
43
++ (NSString *) getCacheDir {
44
+
45
+    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
46
+}
47
+
48
++ (NSString *) getDocumentDir {
49
+
50
+    return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
51
+}
52
+
53
++ (NSString *) getMusicDir {
54
+    return NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES);
55
+}
56
+
57
++ (NSString *) getMovieDir {
58
+    return NSSearchPathForDirectoriesInDomains(NSMoviesDirectory, NSUserDomainMask, YES);
59
+}
60
+
61
++ (NSString *) getPictureDir {
62
+    return NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);
63
+}
64
+
30 65
 + (NSString *) getTempPath:(NSString*)taskId {
31 66
 
32
-    NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
67
+    NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
33 68
     NSString * filename = [NSString stringWithFormat:@"RNFetchBlobTmp_%s", taskId];
34 69
     NSString * tempPath = [documentDir stringByAppendingString: filename];
35 70
     return tempPath;
@@ -99,6 +134,8 @@
99 134
     
100 135
 }
101 136
 
137
+#pragma mark RNFetchBlobFS read stream delegate
138
+
102 139
 - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
103 140
 
104 141
     switch(eventCode) {
@@ -115,7 +152,7 @@
115 152
             // still have data in stream
116 153
             if(len) {
117 154
                 [chunkData appendBytes:(const void *)buf length:len];
118
-                // TODO : read file progress ?
155
+                // TODO : file read progress ?
119 156
 //                [bytesRead setIntValue:[bytesRead intValue]+len];
120 157
                 
121 158
                 // dispatch data event
@@ -133,7 +170,7 @@
133 170
                     [self.bridge.eventDispatcher
134 171
                      sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
135 172
                      body:@{
136
-                            @"event": @"error",
173
+                            @"event": FS_EVENT_ERROR,
137 174
                             @"detail": @"unrecognized encoding"
138 175
                         }
139 176
                      ];
@@ -142,7 +179,7 @@
142 179
                 [self.bridge.eventDispatcher
143 180
                  sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
144 181
                  body:@{
145
-                        @"event": @"data",
182
+                        @"event": FS_EVENT_DATA,
146 183
                         @"detail": encodedChunk
147 184
                     }
148 185
                  ];
@@ -152,7 +189,7 @@
152 189
                 [self.bridge.eventDispatcher
153 190
                 sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
154 191
                 body:@{
155
-                       @"event": @"end",
192
+                       @"event": FS_EVENT_END,
156 193
                        @"detail": @""
157 194
                     }
158 195
                 ];
@@ -164,9 +201,9 @@
164 201
             [self.bridge.eventDispatcher
165 202
              sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
166 203
              body:@{
167
-                    @"event": @"error",
204
+                    @"event": FS_EVENT_ERROR,
168 205
                     @"detail": @"error when read file with stream"
169
-                    }
206
+                }
170 207
              ];
171 208
             break;
172 209
         }
@@ -221,14 +258,14 @@
221 258
     self.receivedBytes = 0;
222 259
     self.options = options;
223 260
     
224
-    NSString * path = [self.options valueForKey:@"path"];
261
+    NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
225 262
     
226 263
     // open file stream for write
227 264
     if( path != nil) {
228 265
         self.fileStream = [[FetchBlobFS alloc]initWithCallback:self.callback];
229 266
         [self.fileStream openWithPath:path];
230 267
     }
231
-    else if ( [self.options valueForKey:@"fileCache"] == YES ) {
268
+    else if ( [self.options valueForKey:CONFIG_USE_TEMP] == YES ) {
232 269
         self.fileStream = [[FetchBlobFS alloc]initWithCallback:self.callback];
233 270
         [self.fileStream openWithId:taskId];
234 271
     }
@@ -255,8 +292,8 @@
255 292
 - (void) connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data {
256 293
     receivedBytes += data.length;
257 294
     
258
-    Boolean fileCache = [self.options valueForKey:@"fileCache"];
259
-    NSString * path = [self.options valueForKey:@"path"];
295
+    Boolean fileCache = [self.options valueForKey:CONFIG_USE_TEMP];
296
+    NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
260 297
     
261 298
     // write to tmp file
262 299
     if( fileCache == YES || path != nil ) {
@@ -321,7 +358,7 @@
321 358
     else
322 359
         data = [[NSData alloc] init];
323 360
     
324
-    NSString * path = [NSString stringWithString:[self.options valueForKey:@"path"]];
361
+    NSString * path = [NSString stringWithString:[self.options valueForKey:CONFIG_FILE_PATH]];
325 362
     
326 363
     [self.fileStream closeInStream];
327 364
     
@@ -330,7 +367,7 @@
330 367
         callback(@[[NSNull null], path]);
331 368
     }
332 369
     // when fileCache option is set but no path specified, save to tmp path
333
-    else if( [self.options valueForKey:@"fileCache"] == YES || path != nil ) {
370
+    else if( [self.options valueForKey:CONFIG_USE_TEMP] == YES || path != nil ) {
334 371
         NSString * tmpPath = [FetchBlobFS getTempPath:taskId];
335 372
         callback(@[[NSNull null], tmpPath]);
336 373
     }
@@ -461,7 +498,13 @@ RCT_EXPORT_METHOD(readStream:(NSString *)taskId withPath:(NSString *)path withEn
461 498
 }
462 499
 
463 500
 RCT_EXPORT_METHOD(flush:(NSString *)taskId withPath:(NSString *)path) {
464
-    // TODO : remove file
501
+    NSError * error = nil;
502
+    NSString * tmpPath = nil;
503
+    if(path != nil)
504
+        tmpPath = path;
505
+    else
506
+        tmpPath = [FetchBlobFS getTempPath:taskId];
507
+    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
465 508
 }
466 509
 
467 510
 @end