Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support file:// and library content:// files #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 57 additions & 11 deletions android/src/main/java/com/rnfs/RNFSManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.ArrayList;

import android.net.Uri;
import android.os.Environment;
import android.os.AsyncTask;
import android.util.Base64;
Expand All @@ -14,10 +15,8 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
Expand Down Expand Up @@ -47,8 +46,57 @@ public class RNFSManager extends ReactContextBaseJavaModule {

private SparseArray<Downloader> downloaders = new SparseArray<Downloader>();

private static ReactApplicationContext mreactContext;

public RNFSManager(ReactApplicationContext reactContext) {
super(reactContext);
mreactContext = reactContext;
}

/**
* get bytes array from Uri.
*
* @param context current context.
* @param uri uri fo the file to read.
* @return a bytes array.
* @throws IOException
*/
private static byte[] getBytes(Context context, Uri uri) throws IOException {
InputStream iStream = context.getContentResolver().openInputStream(uri);
try {
return getBytes(iStream);
} finally {
// close the stream
try {
iStream.close();
} catch (IOException ignored) { /* do nothing */ }
}
}

/**
* get bytes from input stream.
*
* @param inputStream inputStream.
* @return byte array read from the inputStream.
* @throws IOException
*/
private static byte[] getBytes(InputStream inputStream) throws IOException {

byte[] bytesResult = null;
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
try {
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
bytesResult = byteBuffer.toByteArray();
} finally {
// close the stream
try{ byteBuffer.close(); } catch (IOException ignored){ /* do nothing */ }
}
return bytesResult;
}

@Override
Expand All @@ -75,15 +123,13 @@ public void writeFile(String filepath, String base64Content, ReadableMap options
@ReactMethod
public void readFile(String filepath, Callback callback) {
try {
File file = new File(filepath);

if (!file.exists()) throw new Exception("File does not exist");
Copy link
Owner

@itinance itinance Jul 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add please again the File-Exists-check and throw an Exception, if the file does not exists? To not throw an exception would be compatibility break for all those that rely on this check in the thrown exception (instead of calling .exists() by themself in JS-layer)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also recommend not adding file:// without checking if the filepath already starts with file:// to avoid double file://file://

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually doesn't start with file:// since the uri scheme is null


FileInputStream inputStream = new FileInputStream(filepath);
byte[] buffer = new byte[(int)file.length()];
inputStream.read(buffer);
Uri uri = Uri.parse(filepath);
if (uri.getScheme() == null) {
uri = Uri.parse("file://" + filepath);
}
byte[] inputData = getBytes(mreactContext, uri);

String base64Content = Base64.encodeToString(buffer, Base64.NO_WRAP);
String base64Content = Base64.encodeToString(inputData, Base64.NO_WRAP);

callback.invoke(null, base64Content);
} catch (Exception ex) {
Expand Down