Skip to content

Commit c33a8bd

Browse files
committed
feat: support read data to base 64 encoded string
Fixes #12
1 parent 316a0a2 commit c33a8bd

File tree

6 files changed

+20
-9
lines changed

6 files changed

+20
-9
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ type FetchResult = {
8686
`FileSystem.mv(source: string, target: string): Promise<void>`
8787
- Move a file.
8888

89-
`FileSystem.readFile(path: string): Promise<string>`
89+
`FileSystem.readFile(path: string, encoding?: 'utf8' | 'base64'): Promise<string>`
9090
- Read the content of a file.
91+
- Default encoding of returned string is utf8.
9192

9293
```
9394
FileSystem.stat(path: string): Promise<FileStat>

android/src/main/java/com/alpha0010/fs/FileAccessModule.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,13 @@ class FileAccessModule(reactContext: ReactApplicationContext) : ReactContextBase
317317
}
318318

319319
@ReactMethod
320-
fun readFile(path: String, promise: Promise) {
320+
fun readFile(path: String, encoding: String, promise: Promise) {
321321
try {
322-
promise.resolve(File(path).readText())
322+
if (encoding == "base64") {
323+
promise.resolve(Base64.encodeToString(File(path).readBytes(), Base64.DEFAULT))
324+
} else {
325+
promise.resolve(File(path).readText())
326+
}
323327
} catch (e: Throwable) {
324328
promise.reject(e)
325329
}

ios/FileAccess.m

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ @interface RCT_EXTERN_REMAP_MODULE(RNFileAccess, FileAccess, NSObject)
5858
withRejecter:(RCTPromiseRejectBlock)reject)
5959

6060
RCT_EXTERN_METHOD(readFile:(NSString *)path
61+
withEncoding:(NSString *)encoding
6162
withResolver:(RCTPromiseResolveBlock)resolve
6263
withRejecter:(RCTPromiseRejectBlock)reject)
6364

ios/FileAccess.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,15 @@ class FileAccess: NSObject {
253253
}
254254
}
255255

256-
@objc(readFile:withResolver:withRejecter:)
257-
func readFile(path: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
256+
@objc(readFile:withEncoding:withResolver:withRejecter:)
257+
func readFile(path: String, encoding: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
258258
do {
259-
try resolve(String(contentsOfFile: path))
259+
if encoding == "base64" {
260+
let binaryData = try Data(contentsOf: URL(fileURLWithPath: path))
261+
resolve(binaryData.base64EncodedString())
262+
} else {
263+
try resolve(String(contentsOfFile: path))
264+
}
260265
} catch {
261266
reject("ERR", "Failed to read '\(path)'.", error)
262267
}

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ export const FileSystem = {
130130
/**
131131
* Read the content of a file.
132132
*/
133-
readFile(path: string) {
134-
return FileAccessNative.readFile(path);
133+
readFile(path: string, encoding: Encoding = 'utf8') {
134+
return FileAccessNative.readFile(path, encoding);
135135
},
136136

137137
/**

src/native.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type FileAccessType = {
3434
ls(path: string): Promise<string[]>;
3535
mkdir(path: string): Promise<void>;
3636
mv(source: string, target: string): Promise<void>;
37-
readFile(path: string): Promise<string>;
37+
readFile(path: string, encoding: Encoding): Promise<string>;
3838
stat(path: string): Promise<FileStat>;
3939
unlink(path: string): Promise<void>;
4040
writeFile(path: string, data: string, encoding: Encoding): Promise<void>;

0 commit comments

Comments
 (0)