mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-24 04:24:52 +08:00
[both] continued work on multi app support - storage now full supported and auth WIP
This commit is contained in:
@@ -29,14 +29,15 @@ import com.google.android.gms.tasks.Task;
|
||||
import com.google.android.gms.tasks.OnFailureListener;
|
||||
import com.google.android.gms.tasks.OnSuccessListener;
|
||||
|
||||
import com.google.firebase.storage.StorageException;
|
||||
import com.google.firebase.storage.StorageTask;
|
||||
import com.google.firebase.storage.StreamDownloadTask;
|
||||
import com.google.firebase.FirebaseApp;
|
||||
import com.google.firebase.storage.UploadTask;
|
||||
import com.google.firebase.storage.StorageTask;
|
||||
import com.google.firebase.storage.FirebaseStorage;
|
||||
import com.google.firebase.storage.StorageMetadata;
|
||||
import com.google.firebase.storage.StorageException;
|
||||
import com.google.firebase.storage.StorageReference;
|
||||
import com.google.firebase.storage.OnPausedListener;
|
||||
import com.google.firebase.storage.StreamDownloadTask;
|
||||
import com.google.firebase.storage.OnProgressListener;
|
||||
|
||||
import io.invertase.firebase.Utils;
|
||||
@@ -109,8 +110,8 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#delete
|
||||
*/
|
||||
@ReactMethod
|
||||
public void delete(final String path, final Promise promise) {
|
||||
StorageReference reference = this.getReference(path);
|
||||
public void delete(String appName, final String path, final Promise promise) {
|
||||
StorageReference reference = this.getReference(path, appName);
|
||||
reference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
|
||||
@Override
|
||||
public void onSuccess(Void aVoid) {
|
||||
@@ -132,9 +133,9 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#getDownloadURL
|
||||
*/
|
||||
@ReactMethod
|
||||
public void getDownloadURL(final String path, final Promise promise) {
|
||||
public void getDownloadURL(String appName, final String path, final Promise promise) {
|
||||
Log.d(TAG, "getDownloadURL path " + path);
|
||||
final StorageReference reference = this.getReference(path);
|
||||
final StorageReference reference = this.getReference(path, appName);
|
||||
|
||||
Task<Uri> downloadTask = reference.getDownloadUrl();
|
||||
downloadTask
|
||||
@@ -160,8 +161,8 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#getMetadata
|
||||
*/
|
||||
@ReactMethod
|
||||
public void getMetadata(final String path, final Promise promise) {
|
||||
StorageReference reference = this.getReference(path);
|
||||
public void getMetadata(String appName, final String path, final Promise promise) {
|
||||
StorageReference reference = this.getReference(path, appName);
|
||||
reference.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
|
||||
@Override
|
||||
public void onSuccess(StorageMetadata storageMetadata) {
|
||||
@@ -184,8 +185,8 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#updateMetadata
|
||||
*/
|
||||
@ReactMethod
|
||||
public void updateMetadata(final String path, final ReadableMap metadata, final Promise promise) {
|
||||
StorageReference reference = this.getReference(path);
|
||||
public void updateMetadata(String appName, final String path, final ReadableMap metadata, final Promise promise) {
|
||||
StorageReference reference = this.getReference(path, appName);
|
||||
StorageMetadata md = buildMetadataFromMap(metadata);
|
||||
|
||||
reference.updateMetadata(md).addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
|
||||
@@ -212,7 +213,7 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#downloadFile
|
||||
*/
|
||||
@ReactMethod
|
||||
public void downloadFile(final String path, final String localPath, final Promise promise) {
|
||||
public void downloadFile(final String appName, final String path, final String localPath, final Promise promise) {
|
||||
if (!isExternalStorageWritable()) {
|
||||
promise.reject(
|
||||
"storage/invalid-device-file-path",
|
||||
@@ -223,7 +224,7 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
Log.d(TAG, "downloadFile path: " + path);
|
||||
StorageReference reference = this.getReference(path);
|
||||
StorageReference reference = this.getReference(path, appName);
|
||||
|
||||
reference.getStream(new StreamDownloadTask.StreamProcessor() {
|
||||
@Override
|
||||
@@ -252,21 +253,21 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
public void onProgress(StreamDownloadTask.TaskSnapshot taskSnapshot) {
|
||||
Log.d(TAG, "downloadFile progress " + taskSnapshot);
|
||||
WritableMap event = getDownloadTaskAsMap(taskSnapshot);
|
||||
sendJSEvent(STORAGE_STATE_CHANGED, path, event);
|
||||
sendJSEvent(appName, STORAGE_STATE_CHANGED, path, event);
|
||||
}
|
||||
}).addOnPausedListener(new OnPausedListener<StreamDownloadTask.TaskSnapshot>() {
|
||||
@Override
|
||||
public void onPaused(StreamDownloadTask.TaskSnapshot taskSnapshot) {
|
||||
Log.d(TAG, "downloadFile paused " + taskSnapshot);
|
||||
WritableMap event = getDownloadTaskAsMap(taskSnapshot);
|
||||
sendJSEvent(STORAGE_STATE_CHANGED, path, event);
|
||||
sendJSEvent(appName, STORAGE_STATE_CHANGED, path, event);
|
||||
}
|
||||
}).addOnSuccessListener(new OnSuccessListener<StreamDownloadTask.TaskSnapshot>() {
|
||||
@Override
|
||||
public void onSuccess(StreamDownloadTask.TaskSnapshot taskSnapshot) {
|
||||
Log.d(TAG, "downloadFile success" + taskSnapshot);
|
||||
WritableMap resp = getDownloadTaskAsMap(taskSnapshot);
|
||||
sendJSEvent(STORAGE_DOWNLOAD_SUCCESS, path, resp);
|
||||
sendJSEvent(appName, STORAGE_DOWNLOAD_SUCCESS, path, resp);
|
||||
resp = getDownloadTaskAsMap(taskSnapshot);
|
||||
promise.resolve(resp);
|
||||
}
|
||||
@@ -287,8 +288,11 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Storage#setMaxDownloadRetryTime
|
||||
*/
|
||||
@ReactMethod
|
||||
public void setMaxDownloadRetryTime(final double milliseconds) {
|
||||
FirebaseStorage.getInstance().setMaxDownloadRetryTimeMillis((long) milliseconds);
|
||||
public void setMaxDownloadRetryTime(String appName, final double milliseconds) {
|
||||
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
|
||||
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance(firebaseApp);
|
||||
|
||||
firebaseStorage.setMaxDownloadRetryTimeMillis((long) milliseconds);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -298,8 +302,11 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Storage#setMaxOperationRetryTime
|
||||
*/
|
||||
@ReactMethod
|
||||
public void setMaxOperationRetryTime(final double milliseconds) {
|
||||
FirebaseStorage.getInstance().setMaxOperationRetryTimeMillis((long) milliseconds);
|
||||
public void setMaxOperationRetryTime(String appName, final double milliseconds) {
|
||||
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
|
||||
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance(firebaseApp);
|
||||
|
||||
firebaseStorage.setMaxOperationRetryTimeMillis((long) milliseconds);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -309,8 +316,11 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Storage#setMaxUploadRetryTime
|
||||
*/
|
||||
@ReactMethod
|
||||
public void setMaxUploadRetryTime(final double milliseconds) {
|
||||
FirebaseStorage.getInstance().setMaxUploadRetryTimeMillis((long) milliseconds);
|
||||
public void setMaxUploadRetryTime(String appName, final double milliseconds) {
|
||||
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
|
||||
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance(firebaseApp);
|
||||
|
||||
firebaseStorage.setMaxUploadRetryTimeMillis((long) milliseconds);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -323,8 +333,8 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @url https://firebase.google.com/docs/reference/js/firebase.storage.Reference#putFile
|
||||
*/
|
||||
@ReactMethod
|
||||
public void putFile(final String path, final String localPath, final ReadableMap metadata, final Promise promise) {
|
||||
StorageReference reference = this.getReference(path);
|
||||
public void putFile(final String appName, final String path, final String localPath, final ReadableMap metadata, final Promise promise) {
|
||||
StorageReference reference = this.getReference(path, appName);
|
||||
|
||||
Log.i(TAG, "putFile: " + localPath + " to " + path);
|
||||
|
||||
@@ -356,11 +366,11 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
|
||||
Log.d(TAG, "putFile success " + taskSnapshot);
|
||||
WritableMap resp = getUploadTaskAsMap(taskSnapshot);
|
||||
sendJSEvent(STORAGE_STATE_CHANGED, path, resp);
|
||||
sendJSEvent(appName, STORAGE_STATE_CHANGED, path, resp);
|
||||
|
||||
// to avoid readable map already consumed errors
|
||||
resp = getUploadTaskAsMap(taskSnapshot);
|
||||
sendJSEvent(STORAGE_UPLOAD_SUCCESS, path, resp);
|
||||
sendJSEvent(appName, STORAGE_UPLOAD_SUCCESS, path, resp);
|
||||
|
||||
resp = getUploadTaskAsMap(taskSnapshot);
|
||||
promise.resolve(resp);
|
||||
@@ -370,7 +380,7 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
@Override
|
||||
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
|
||||
Log.d(TAG, "putFile progress " + taskSnapshot);
|
||||
sendJSEvent(STORAGE_STATE_CHANGED, path, getUploadTaskAsMap(taskSnapshot));
|
||||
sendJSEvent(appName, STORAGE_STATE_CHANGED, path, getUploadTaskAsMap(taskSnapshot));
|
||||
}
|
||||
})
|
||||
.addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
|
||||
@@ -378,7 +388,7 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
|
||||
Log.d(TAG, "putFile paused " + taskSnapshot);
|
||||
WritableMap event = getUploadTaskAsMap(taskSnapshot);
|
||||
sendJSEvent(STORAGE_STATE_CHANGED, path, event);
|
||||
sendJSEvent(appName, STORAGE_STATE_CHANGED, path, event);
|
||||
}
|
||||
});
|
||||
} catch (Exception exception) {
|
||||
@@ -388,20 +398,25 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
|
||||
/**
|
||||
* Internal helper to detect if ref is from url or a path.
|
||||
*
|
||||
* @param path
|
||||
* @return
|
||||
*/
|
||||
private StorageReference getReference(String path) {
|
||||
private StorageReference getReference(String path, String appName) {
|
||||
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
|
||||
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance(firebaseApp);
|
||||
|
||||
if (path.startsWith("url::")) {
|
||||
String url = path.substring(5);
|
||||
return FirebaseStorage.getInstance().getReferenceFromUrl(url);
|
||||
return firebaseStorage.getReferenceFromUrl(url);
|
||||
} else {
|
||||
return FirebaseStorage.getInstance().getReference(path);
|
||||
return firebaseStorage.getReference(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal helper to convert content:// uri's to a real path
|
||||
*
|
||||
* @param uri
|
||||
* @return
|
||||
*/
|
||||
@@ -477,8 +492,8 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @return
|
||||
*/
|
||||
private WritableMap getUploadTaskAsMap(UploadTask.TaskSnapshot taskSnapshot) {
|
||||
|
||||
WritableMap resp = Arguments.createMap();
|
||||
|
||||
if (taskSnapshot != null) {
|
||||
resp.putDouble("bytesTransferred", taskSnapshot.getBytesTransferred());
|
||||
resp.putString("downloadUrl", taskSnapshot.getDownloadUrl() != null ? taskSnapshot.getDownloadUrl().toString() : null);
|
||||
@@ -566,9 +581,10 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
|
||||
* @param path
|
||||
* @param body
|
||||
*/
|
||||
private void sendJSEvent(final String name, final String path, WritableMap body) {
|
||||
private void sendJSEvent(String appName, final String name, final String path, WritableMap body) {
|
||||
WritableMap event = Arguments.createMap();
|
||||
|
||||
event.putString("appName", appName);
|
||||
event.putString("eventName", name);
|
||||
event.putString("path", path);
|
||||
event.putMap("body", body);
|
||||
|
||||
Reference in New Issue
Block a user