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

Fix ENOENT (No such file or directory) #20

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
80 changes: 50 additions & 30 deletions android/src/main/java/com/yoloci/fileupload/FileUploadModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySeyIterator;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand All @@ -20,20 +20,29 @@

import com.facebook.react.bridge.WritableMap;
import java.io.FileInputStream;
import com.facebook.internal.BundleJSONConverter;


import org.json.JSONObject;

//
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.net.Uri;
import android.content.Context;
import android.database.Cursor;
import java.net.URL;

public class FileUploadModule extends ReactContextBaseJavaModule {

private ReactApplicationContext reactContext;

@Override
public String getName() {
return "FileUpload";
}

public FileUploadModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}

@ReactMethod
Expand All @@ -54,8 +63,6 @@ public void upload(final ReadableMap options, final Callback callback) {
ReadableArray files = options.getArray("files");
ReadableMap fields = options.getMap("fields");



HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
Expand All @@ -70,7 +77,6 @@ public void upload(final ReadableMap options, final Callback callback) {

connectURL = new URL(uploadUrl);


connection = (HttpURLConnection) connectURL.openConnection();

// Allow Inputs & Outputs.
Expand All @@ -81,21 +87,19 @@ public void upload(final ReadableMap options, final Callback callback) {
connection.setRequestMethod(method);

// set headers
ReadableMapKeySeyIterator iterator = headers.keySetIterator();
ReadableMapKeySetIterator iterator = headers.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
connection.setRequestProperty(key, headers.getString(key));
}



connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

outputStream = new DataOutputStream( connection.getOutputStream() );

// set fields
ReadableMapKeySeyIterator fieldIterator = fields.keySetIterator();
ReadableMapKeySetIterator fieldIterator = fields.keySetIterator();
while (fieldIterator.hasNextKey()) {
outputStream.writeBytes(twoHyphens + boundary + lineEnd);

Expand All @@ -110,12 +114,15 @@ public void upload(final ReadableMap options, final Callback callback) {

ReadableMap file = files.getMap(i);
String filename = file.getString("filename");
String filepath = file.getString("filepath");
// transform filepath to "realPathFromURI"
String filepath = getRealPathFromURI(this.reactContext, file.getString("filepath"));

String name = file.getString("name");
filepath = filepath.replace("file://", "");
fileInputStream = new FileInputStream(filepath);

outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"image\";filename=\"" + filename + "\"" + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + name + "\";filename=\"" + filename + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();
Expand All @@ -141,7 +148,7 @@ public void upload(final ReadableMap options, final Callback callback) {

int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
if (serverResponseCode != 200) {
if (serverResponseCode > 206) {
fileInputStream.close();
outputStream.flush();
outputStream.close();
Expand All @@ -154,22 +161,19 @@ public void upload(final ReadableMap options, final Callback callback) {
sb.append(line);
}
br.close();
JSONObject mainObject = new JSONObject(sb.toString());
if (mainObject != null) {
BundleJSONConverter bjc = new BundleJSONConverter();
Bundle bundle = bjc.convertToBundle(mainObject);
WritableMap map = Arguments.fromBundle(bundle);

fileInputStream.close();
outputStream.flush();
outputStream.close();
callback.invoke(null, map);
} else {
fileInputStream.close();
outputStream.flush();
outputStream.close();
callback.invoke(null, null);
}
String data = sb.toString();
JSONObject mainObject = new JSONObject();
mainObject.put("data", data);
mainObject.put("status", serverResponseCode);

BundleJSONConverter bjc = new BundleJSONConverter();
Bundle bundle = bjc.convertToBundle(mainObject);
WritableMap map = Arguments.fromBundle(bundle);

fileInputStream.close();
outputStream.flush();
outputStream.close();
callback.invoke(null, map);
}


Expand All @@ -178,6 +182,22 @@ public void upload(final ReadableMap options, final Callback callback) {
callback.invoke("Error happened: " + ex.getMessage(), null);
}
}
}

// Need this to transform gallery uri to "realPath" from android Filesystem
public String getRealPathFromURI(ReactApplicationContext context, String filePath) {
Cursor cursor = null;
Uri contentUri = Uri.parse(filePath);

try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}