From a39c22be81f0c1f2263dbe60f3cd6cfcc902d2ac Mon Sep 17 00:00:00 2001 From: Samet Demir Date: Sun, 16 Jul 2017 15:18:55 +0300 Subject: [PATCH] add read method with lenght and position params --- FS.common.js | 30 +++++++++++++ README.md | 6 +++ RNFSManager.m | 45 +++++++++++++++++++ .../src/main/java/com/rnfs/RNFSManager.java | 29 ++++++++++++ 4 files changed, 110 insertions(+) mode change 100644 => 100755 FS.common.js mode change 100644 => 100755 RNFSManager.m mode change 100644 => 100755 android/src/main/java/com/rnfs/RNFSManager.java diff --git a/FS.common.js b/FS.common.js old mode 100644 new mode 100755 index 0f89b2a7..f5b76386 --- a/FS.common.js +++ b/FS.common.js @@ -260,6 +260,36 @@ var RNFS = { return readFileGeneric(filepath, encodingOrOptions, RNFSManager.readFile); }, + read(filepath: string, length = 0, position = 0, encodingOrOptions?: any): Promise { + var options = { + encoding: 'utf8' + }; + + if (encodingOrOptions) { + if (typeof encodingOrOptions === 'string') { + options.encoding = encodingOrOptions; + } else if (typeof encodingOrOptions === 'object') { + options = encodingOrOptions; + } + } + + return RNFSManager.read(normalizeFilePath(filepath), length, position).then((b64) => { + var contents; + + if (options.encoding === 'utf8') { + contents = utf8.decode(base64.decode(b64)); + } else if (options.encoding === 'ascii') { + contents = base64.decode(b64); + } else if (options.encoding === 'base64') { + contents = b64; + } else { + throw new Error('Invalid encoding type "' + String(options.encoding) + '"'); + } + + return contents; + }); + }, + // Android only readFileAssets(filepath: string, encodingOrOptions?: any): Promise { if (!RNFSManager.readFileAssets) { diff --git a/README.md b/README.md index 1ffa7013..2fc695e1 100644 --- a/README.md +++ b/README.md @@ -363,6 +363,12 @@ Reads the file at `path` and return contents. `encoding` can be one of `utf8` (d Note: you will take quite a performance hit if you are reading big files +### `read(filepath: string, length = 0, position = 0, encodingOrOptions?: any): Promise` + +Reads length bits from the given position of the file and returns contents. `encoding` can be one of `utf8` (default), `ascii`, `base64`. Use `base64` for reading binary files. + +Note: reading big files piece by piece using this method may be useful in terms of performance. + ### `readFileAssets(filepath:string, encoding?: string): Promise` Reads the file at `path` in the Android app's assets folder and return contents. `encoding` can be one of `utf8` (default), `ascii`, `base64`. Use `base64` for reading binary files. diff --git a/RNFSManager.m b/RNFSManager.m old mode 100644 new mode 100755 index 20a0f9ab..2301473f --- a/RNFSManager.m +++ b/RNFSManager.m @@ -261,6 +261,51 @@ - (dispatch_queue_t)methodQueue resolve(base64Content); } +RCT_EXPORT_METHOD(read:(NSString *)filepath + length: (NSInteger *)length + position: (NSInteger *)position + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) +{ + BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath]; + + if (!fileExists) { + return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil); + } + + NSError *error = nil; + + NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error]; + + if (error) { + return [self reject:reject withError:error]; + } + + if ([attributes objectForKey:NSFileType] == NSFileTypeDirectory) { + return reject(@"EISDIR", @"EISDIR: illegal operation on a directory, read", nil); + } + + // Open the file handler. + NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:filepath]; + if (file == nil) { + return reject(@"EISDIR", @"EISDIR: Could not open file for reading", nil); + } + + // Seek to the position if there is one. + [file seekToFileOffset: (int)position]; + + NSData *content; + if ((int)length > 0) { + content = [file readDataOfLength: (int)length]; + } else { + content = [file readDataToEndOfFile]; + } + + NSString *base64Content = [content base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; + + resolve(base64Content); +} + RCT_EXPORT_METHOD(hash:(NSString *)filepath algorithm:(NSString *)algorithm resolver:(RCTPromiseResolveBlock)resolve diff --git a/android/src/main/java/com/rnfs/RNFSManager.java b/android/src/main/java/com/rnfs/RNFSManager.java old mode 100644 new mode 100755 index 31cee322..40af6194 --- a/android/src/main/java/com/rnfs/RNFSManager.java +++ b/android/src/main/java/com/rnfs/RNFSManager.java @@ -150,6 +150,35 @@ public void readFile(String filepath, Promise promise) { } } + @ReactMethod + public void read(String filepath, int length, int position, Promise promise){ + try { + File file = new File(filepath); + + if (file.isDirectory()) { + rejectFileIsDirectory(promise); + return; + } + + if (!file.exists()) { + rejectFileNotFound(promise, filepath); + return; + } + + FileInputStream inputStream = new FileInputStream(filepath); + byte[] buffer = new byte[length]; + inputStream.skip(position); + inputStream.read(buffer,0,length); + + String base64Content = Base64.encodeToString(buffer, Base64.NO_WRAP); + + promise.resolve(base64Content); + } catch (Exception ex) { + ex.printStackTrace(); + reject(promise, filepath, ex); + } + } + @ReactMethod public void readFileAssets(String filepath, Promise promise) { InputStream stream = null;