Sfoglia il codice sorgente

Add slice implementation #44

Ben Hsieh 8 anni fa
parent
commit
5d4e3b0bc5

+ 5
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java Vedi File

@@ -160,6 +160,11 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
160 160
         }
161 161
     }
162 162
 
163
+    @ReactMethod
164
+    public void slice(String src, String dest, int start, int end, String encode, Callback callback) {
165
+
166
+    }
167
+
163 168
     @ReactMethod
164 169
     public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
165 170
         new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();

+ 34
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java Vedi File

@@ -24,6 +24,7 @@ import com.facebook.react.modules.core.DeviceEventManagerModule;
24 24
 
25 25
 import java.io.File;
26 26
 import java.io.FileInputStream;
27
+import java.io.FileNotFoundException;
27 28
 import java.io.FileOutputStream;
28 29
 import java.io.IOException;
29 30
 import java.io.InputStream;
@@ -536,6 +537,39 @@ public class RNFetchBlobFS {
536 537
         callback.invoke(null, arg);
537 538
     }
538 539
 
540
+    /**
541
+     * Create a file by slicing given file path
542
+     * @param src   Source file path
543
+     * @param dest  Destination of created file
544
+     * @param start Start byte offset in source file
545
+     * @param end   End byte offset
546
+     * @param encode
547
+     * @param callback
548
+     */
549
+    public static void slice(String src, String dest, int start, int end, String encode, Callback callback) {
550
+        try {
551
+            long expected = end - start;
552
+            long now = 0;
553
+            FileInputStream in = new FileInputStream(new File(src));
554
+            FileOutputStream out = new FileOutputStream(new File(dest));
555
+            in.skip(start);
556
+            byte [] buffer = new byte[10240];
557
+            while(now < expected) {
558
+                long read = in.read(buffer, 0, 10240);
559
+                if(read <= 0) {
560
+                    break;
561
+                }
562
+                now += read;
563
+                out.write(buffer, 0, (int) read);
564
+            }
565
+            in.close();
566
+            out.close();
567
+            callback.invoke(null, dest);
568
+        } catch (Exception e) {
569
+            e.printStackTrace();
570
+        }
571
+    }
572
+
539 573
     static void lstat(String path, final Callback callback) {
540 574
         path = normalizePath(path);
541 575
 

+ 5
- 0
src/ios/RNFetchBlob/RNFetchBlob.m Vedi File

@@ -358,6 +358,11 @@ RCT_EXPORT_METHOD(cancelRequest:(NSString *)taskId callback:(RCTResponseSenderBl
358 358
 
359 359
 }
360 360
 
361
+RCT_EXPORT_METHOD(slice:(NSString *)src dest:(NSString *)dest start:(NSNumber *)start end:(NSNumber *)end resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
362
+{
363
+    [RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];
364
+})
365
+
361 366
 #pragma mark RNFetchBlob private methods
362 367
 
363 368
 

+ 1
- 0
src/ios/RNFetchBlobFS.h Vedi File

@@ -55,6 +55,7 @@
55 55
 + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
56 56
 + (void) readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject onComplete:(void (^)(NSData * content))onComplete;
57 57
 + (void) readAssetFile:(NSData *)assetUrl completionBlock:(void(^)(NSData * content))completionBlock failBlock:(void(^)(NSError * err))failBlock;
58
++ (void) slice:(NSString *)path dest:(NSString *)dest start:(NSNumber *)start end:(NSNumber *)end encode:(NSString *)encode resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
58 59
 //+ (void) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append;
59 60
 
60 61
 // constructor

+ 46
- 1
src/ios/RNFetchBlobFS.m Vedi File

@@ -498,8 +498,53 @@ NSMutableDictionary *fileStreams = nil;
498 498
     });
499 499
 }
500 500
 
501
+// Slice a file into another file, generally for support Blob implementation.
502
+- (void)slice:(NSString *)path
503
+         dest:(NSString *)dest
504
+        start:(NSNumber *)start
505
+          end:(NSNumber *)end
506
+        encod:(NSString *)encode
507
+     resolver:(RCTPromiseResolveBlock)resolve
508
+     rejecter:(RCTPromiseRejectBlock)reject
509
+{
510
+    long expected = [end longValue] - [start longValue];
511
+    long read = 0;
512
+    NSFileHandle * handle = [NSFileHandle fileHandleForReadingAtPath:path];
513
+    NSFileManager * fm = [NSFileManager defaultManager];
514
+    NSOutputStream * os = [[NSOutputStream alloc] initToFileAtPath:dest append:NO];
515
+    [os open];
516
+    // abort for the source file not exists
517
+    if([fm fileExistsAtPath:path] == NO)
518
+    {
519
+        reject(@"RNFetchBlob slice failed", @"the file does not exists", path);
520
+        return;
521
+    }
522
+    long size = [fm attributesOfItemAtPath:path error:nil].fileSize;
523
+    // abort for the file size is less than start
524
+    if(size < start)
525
+    {
526
+        reject(@"RNFetchBlob slice failed", @"start is greater than file size", @"");
527
+        return;
528
+    }
529
+    if(![fm fileExistsAtPath:dest]) {
530
+        [fm createFileAtPath:dest contents:@"" attributes:nil];
531
+    }
532
+    [handle seekToFileOffset:start];
533
+    while(read < expected)
534
+    {
535
+        NSData * chunk = [handle readDataOfLength:10240];
536
+        read += [chunk length];
537
+        [os write:[chunk bytes] maxLength:10240];
538
+    }
539
+    [handle closeFile];
540
+    [os close];
541
+    resolve(dest);
542
+    
543
+}
544
+
501 545
 // close file read stream
502
-- (void)closeInStream {
546
+- (void)closeInStream
547
+{
503 548
     if(self.inStream != nil) {
504 549
         [self.inStream close];
505 550
         [self.inStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];