|
@@ -261,18 +261,44 @@ void runOnMainQueueWithoutDeadlocking(void (^block)(void))
|
261
|
261
|
// still have data in stream
|
262
|
262
|
if(len) {
|
263
|
263
|
[chunkData appendBytes:(const void *)buf length:len];
|
264
|
|
- // TODO : file read progress ?
|
265
|
264
|
// dispatch data event
|
266
|
265
|
NSString * encodedChunk = [NSString alloc];
|
267
|
266
|
if( [[self.encoding lowercaseString] isEqualToString:@"utf8"] ) {
|
268
|
267
|
encodedChunk = [encodedChunk initWithData:chunkData encoding:NSUTF8StringEncoding];
|
269
|
268
|
}
|
|
269
|
+ // when encoding is ASCII, send byte array data
|
270
|
270
|
else if ( [[self.encoding lowercaseString] isEqualToString:@"ascii"] ) {
|
271
|
|
- encodedChunk = [encodedChunk initWithData:chunkData encoding:NSASCIIStringEncoding];
|
|
271
|
+ // RCTBridge only emits string data, so we have to create JSON byte array string
|
|
272
|
+ NSString * asciiStr = @"[";
|
|
273
|
+ if (chunkData.length > 0)
|
|
274
|
+ {
|
|
275
|
+ unsigned char *bytePtr = (unsigned char *)[chunkData bytes];
|
|
276
|
+ NSInteger byteLen = chunkData.length/sizeof(uint8_t);
|
|
277
|
+ for (int i = 0; i < byteLen; i++)
|
|
278
|
+ {
|
|
279
|
+ uint8_t * byteFromArray = chunkData.bytes;
|
|
280
|
+ NSInteger val = bytePtr[i];
|
|
281
|
+ if(i+1 < byteLen)
|
|
282
|
+ asciiStr = [asciiStr stringByAppendingFormat:@"%d,", val];
|
|
283
|
+ else
|
|
284
|
+ asciiStr = [asciiStr stringByAppendingFormat:@"%d", val];
|
|
285
|
+ }
|
|
286
|
+ }
|
|
287
|
+ asciiStr = [asciiStr stringByAppendingString:@"]"];
|
|
288
|
+ [self.bridge.eventDispatcher
|
|
289
|
+ sendDeviceEventWithName:streamEventCode
|
|
290
|
+ body:@{
|
|
291
|
+ @"event": FS_EVENT_DATA,
|
|
292
|
+ @"detail": asciiStr
|
|
293
|
+ }
|
|
294
|
+ ];
|
|
295
|
+ return;
|
272
|
296
|
}
|
|
297
|
+ // convert byte array to base64 data chunks
|
273
|
298
|
else if ( [[self.encoding lowercaseString] isEqualToString:@"base64"] ) {
|
274
|
299
|
encodedChunk = [chunkData base64EncodedStringWithOptions:0];
|
275
|
300
|
}
|
|
301
|
+ // unknown encoding, send erro event
|
276
|
302
|
else {
|
277
|
303
|
[self.bridge.eventDispatcher
|
278
|
304
|
sendDeviceEventWithName:streamEventCode
|
|
@@ -676,6 +702,26 @@ RCT_EXPORT_METHOD(createFile:(NSString *)path data:(NSString *)data encoding:(NS
|
676
|
702
|
|
677
|
703
|
}
|
678
|
704
|
|
|
705
|
+// method for create file with ASCII content
|
|
706
|
+RCT_EXPORT_METHOD(createFileASCII:(NSString *)path data:(NSArray *)dataArray callback:(RCTResponseSenderBlock)callback) {
|
|
707
|
+
|
|
708
|
+ NSFileManager * fm = [NSFileManager defaultManager];
|
|
709
|
+ NSMutableData * fileContent = [NSMutableData alloc];
|
|
710
|
+
|
|
711
|
+ char bytes[[dataArray count]];
|
|
712
|
+ for(int i = 0; i < dataArray.count; i++) {
|
|
713
|
+ bytes[i] = [[dataArray objectAtIndex:i] charValue];
|
|
714
|
+ }
|
|
715
|
+ [fileContent appendBytes:bytes length:dataArray.count];
|
|
716
|
+ BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
|
|
717
|
+
|
|
718
|
+ if(success == YES)
|
|
719
|
+ callback(@[[NSNull null]]);
|
|
720
|
+ else
|
|
721
|
+ callback(@[[NSString stringWithFormat:@"failed to create new file at path %@ please ensure the folder exists"]]);
|
|
722
|
+
|
|
723
|
+}
|
|
724
|
+
|
679
|
725
|
|
680
|
726
|
RCT_EXPORT_METHOD(exists:(NSString *)path callback:(RCTResponseSenderBlock)callback) {
|
681
|
727
|
BOOL isDir = NO;
|
|
@@ -709,6 +755,18 @@ RCT_EXPORT_METHOD(writeStream:(NSString *)path withEncoding:(NSString *)encoding
|
709
|
755
|
callback(@[[NSNull null], streamId]);
|
710
|
756
|
}
|
711
|
757
|
|
|
758
|
+RCT_EXPORT_METHOD(writeArrayChunk:(NSString *)streamId withArray:(NSArray *)dataArray callback:(RCTResponseSenderBlock) callback) {
|
|
759
|
+ FetchBlobFS *fs = [[FetchBlobFS getFileStreams] valueForKey:streamId];
|
|
760
|
+ char bytes[[dataArray count]];
|
|
761
|
+ for(int i = 0; i < dataArray.count; i++) {
|
|
762
|
+ bytes[i] = [[dataArray objectAtIndex:i] charValue];
|
|
763
|
+ }
|
|
764
|
+ NSMutableData * data = [NSMutableData alloc];
|
|
765
|
+ [data appendBytes:bytes length:dataArray.count];
|
|
766
|
+ [fs write:data];
|
|
767
|
+ callback(@[[NSNull null]]);
|
|
768
|
+}
|
|
769
|
+
|
712
|
770
|
RCT_EXPORT_METHOD(writeChunk:(NSString *)streamId withData:(NSString *)data callback:(RCTResponseSenderBlock) callback) {
|
713
|
771
|
FetchBlobFS *fs = [[FetchBlobFS getFileStreams] valueForKey:streamId];
|
714
|
772
|
[fs writeEncodeChunk:data];
|