Browse Source

#2 ios implementation wip commit

Ben Hsieh 8 years ago
parent
commit
53d6eb23de
2 changed files with 74 additions and 15 deletions
  1. 16
    0
      src/ios/RNFetchBlob/RNFetchBlob.h
  2. 58
    15
      src/ios/RNFetchBlob/RNFetchBlob.m

+ 16
- 0
src/ios/RNFetchBlob/RNFetchBlob.h View File

9
 #import <Foundation/Foundation.h>
9
 #import <Foundation/Foundation.h>
10
 #import "RCTBridgeModule.h"
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
 @interface FetchBlobFS : NSObject <NSStreamDelegate>  {
28
 @interface FetchBlobFS : NSObject <NSStreamDelegate>  {
13
     NSOutputStream * outStream;
29
     NSOutputStream * outStream;
14
     NSInputStream * inStream;
30
     NSInputStream * inStream;

+ 58
- 15
src/ios/RNFetchBlob/RNFetchBlob.m View File

1
 //
1
 //
2
 //  RNFetchBlob.m
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
 #import "RNFetchBlob.h"
7
 #import "RNFetchBlob.h"
11
 #import "RCTBridge.h"
11
 #import "RCTBridge.h"
12
 #import "RCTEventDispatcher.h"
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
 @synthesize taskId;
38
 @synthesize taskId;
28
 @synthesize path;
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
 + (NSString *) getTempPath:(NSString*)taskId {
65
 + (NSString *) getTempPath:(NSString*)taskId {
31
 
66
 
32
-    NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
67
+    NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
33
     NSString * filename = [NSString stringWithFormat:@"RNFetchBlobTmp_%s", taskId];
68
     NSString * filename = [NSString stringWithFormat:@"RNFetchBlobTmp_%s", taskId];
34
     NSString * tempPath = [documentDir stringByAppendingString: filename];
69
     NSString * tempPath = [documentDir stringByAppendingString: filename];
35
     return tempPath;
70
     return tempPath;
99
     
134
     
100
 }
135
 }
101
 
136
 
137
+#pragma mark RNFetchBlobFS read stream delegate
138
+
102
 - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
139
 - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
103
 
140
 
104
     switch(eventCode) {
141
     switch(eventCode) {
115
             // still have data in stream
152
             // still have data in stream
116
             if(len) {
153
             if(len) {
117
                 [chunkData appendBytes:(const void *)buf length:len];
154
                 [chunkData appendBytes:(const void *)buf length:len];
118
-                // TODO : read file progress ?
155
+                // TODO : file read progress ?
119
 //                [bytesRead setIntValue:[bytesRead intValue]+len];
156
 //                [bytesRead setIntValue:[bytesRead intValue]+len];
120
                 
157
                 
121
                 // dispatch data event
158
                 // dispatch data event
133
                     [self.bridge.eventDispatcher
170
                     [self.bridge.eventDispatcher
134
                      sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
171
                      sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
135
                      body:@{
172
                      body:@{
136
-                            @"event": @"error",
173
+                            @"event": FS_EVENT_ERROR,
137
                             @"detail": @"unrecognized encoding"
174
                             @"detail": @"unrecognized encoding"
138
                         }
175
                         }
139
                      ];
176
                      ];
142
                 [self.bridge.eventDispatcher
179
                 [self.bridge.eventDispatcher
143
                  sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
180
                  sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
144
                  body:@{
181
                  body:@{
145
-                        @"event": @"data",
182
+                        @"event": FS_EVENT_DATA,
146
                         @"detail": encodedChunk
183
                         @"detail": encodedChunk
147
                     }
184
                     }
148
                  ];
185
                  ];
152
                 [self.bridge.eventDispatcher
189
                 [self.bridge.eventDispatcher
153
                 sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
190
                 sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
154
                 body:@{
191
                 body:@{
155
-                       @"event": @"end",
192
+                       @"event": FS_EVENT_END,
156
                        @"detail": @""
193
                        @"detail": @""
157
                     }
194
                     }
158
                 ];
195
                 ];
164
             [self.bridge.eventDispatcher
201
             [self.bridge.eventDispatcher
165
              sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
202
              sendAppEventWithName: [NSString stringWithFormat:@"RNFetchBlobStream%s", self.taskId]
166
              body:@{
203
              body:@{
167
-                    @"event": @"error",
204
+                    @"event": FS_EVENT_ERROR,
168
                     @"detail": @"error when read file with stream"
205
                     @"detail": @"error when read file with stream"
169
-                    }
206
+                }
170
              ];
207
              ];
171
             break;
208
             break;
172
         }
209
         }
221
     self.receivedBytes = 0;
258
     self.receivedBytes = 0;
222
     self.options = options;
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
     // open file stream for write
263
     // open file stream for write
227
     if( path != nil) {
264
     if( path != nil) {
228
         self.fileStream = [[FetchBlobFS alloc]initWithCallback:self.callback];
265
         self.fileStream = [[FetchBlobFS alloc]initWithCallback:self.callback];
229
         [self.fileStream openWithPath:path];
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
         self.fileStream = [[FetchBlobFS alloc]initWithCallback:self.callback];
269
         self.fileStream = [[FetchBlobFS alloc]initWithCallback:self.callback];
233
         [self.fileStream openWithId:taskId];
270
         [self.fileStream openWithId:taskId];
234
     }
271
     }
255
 - (void) connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data {
292
 - (void) connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data {
256
     receivedBytes += data.length;
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
     // write to tmp file
298
     // write to tmp file
262
     if( fileCache == YES || path != nil ) {
299
     if( fileCache == YES || path != nil ) {
321
     else
358
     else
322
         data = [[NSData alloc] init];
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
     [self.fileStream closeInStream];
363
     [self.fileStream closeInStream];
327
     
364
     
330
         callback(@[[NSNull null], path]);
367
         callback(@[[NSNull null], path]);
331
     }
368
     }
332
     // when fileCache option is set but no path specified, save to tmp path
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
         NSString * tmpPath = [FetchBlobFS getTempPath:taskId];
371
         NSString * tmpPath = [FetchBlobFS getTempPath:taskId];
335
         callback(@[[NSNull null], tmpPath]);
372
         callback(@[[NSNull null], tmpPath]);
336
     }
373
     }
461
 }
498
 }
462
 
499
 
463
 RCT_EXPORT_METHOD(flush:(NSString *)taskId withPath:(NSString *)path) {
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
 @end
510
 @end