2929import com .google .android .gms .tasks .OnFailureListener ;
3030import com .google .android .gms .tasks .OnSuccessListener ;
3131
32+ import com .google .firebase .database .DataSnapshot ;
33+ import com .google .firebase .database .DatabaseError ;
34+ import com .google .firebase .storage .StorageException ;
35+ import com .google .firebase .storage .StorageTask ;
3236import com .google .firebase .storage .StreamDownloadTask ;
3337import com .google .firebase .storage .UploadTask ;
3438import com .google .firebase .storage .FirebaseStorage ;
@@ -55,13 +59,11 @@ public class FirestackStorage extends ReactContextBaseJavaModule {
5559 private static final String FileTypeRegular = "FILETYPE_REGULAR" ;
5660 private static final String FileTypeDirectory = "FILETYPE_DIRECTORY" ;
5761
58- private static final String STORAGE_UPLOAD_PROGRESS = "upload_progress" ;
59- private static final String STORAGE_UPLOAD_PAUSED = "upload_paused" ;
60- private static final String STORAGE_UPLOAD_RESUMED = "upload_resumed" ;
61-
62- private static final String STORAGE_DOWNLOAD_PROGRESS = "download_progress" ;
63- private static final String STORAGE_DOWNLOAD_PAUSED = "download_paused" ;
64- private static final String STORAGE_DOWNLOAD_RESUMED = "download_resumed" ;
62+ private static final String STORAGE_EVENT = "storage_event" ;
63+ private static final String STORAGE_ERROR = "storage_error" ;
64+ private static final String STORAGE_STATE_CHANGED = "state_changed" ;
65+ private static final String STORAGE_UPLOAD_SUCCESS = "upload_success" ;
66+ private static final String STORAGE_UPLOAD_FAILURE = "upload_failure" ;
6567 private static final String STORAGE_DOWNLOAD_SUCCESS = "download_success" ;
6668 private static final String STORAGE_DOWNLOAD_FAILURE = "download_failure" ;
6769
@@ -204,30 +206,30 @@ public void doInBackground(StreamDownloadTask.TaskSnapshot taskSnapshot, InputSt
204206 public void onProgress (StreamDownloadTask .TaskSnapshot taskSnapshot ) {
205207 Log .d (TAG , "Got download progress " + taskSnapshot );
206208 WritableMap event = getDownloadTaskAsMap (taskSnapshot );
207- //TODO: No need for this if JS listeners are separated
208- event .putString ("eventName" , STORAGE_DOWNLOAD_PROGRESS );
209- Utils .sendEvent (getReactApplicationContext (), STORAGE_DOWNLOAD_PROGRESS , event );
209+ handleStorageEvent (STORAGE_STATE_CHANGED , path , event );
210210 }
211211 }).addOnPausedListener (new OnPausedListener <StreamDownloadTask .TaskSnapshot >() {
212212 @ Override
213213 public void onPaused (StreamDownloadTask .TaskSnapshot taskSnapshot ) {
214214 Log .d (TAG , "Download is paused " + taskSnapshot );
215215 WritableMap event = getDownloadTaskAsMap (taskSnapshot );
216- //TODO: No need for this if JS listeners are separated
217- event .putString ("eventName" , STORAGE_DOWNLOAD_PAUSED );
218- Utils .sendEvent (getReactApplicationContext (), STORAGE_DOWNLOAD_PAUSED , event );
216+ handleStorageEvent (STORAGE_STATE_CHANGED , path , event );
219217 }
220218 }).addOnSuccessListener (new OnSuccessListener <StreamDownloadTask .TaskSnapshot >() {
221219 @ Override
222220 public void onSuccess (StreamDownloadTask .TaskSnapshot taskSnapshot ) {
223221 Log .d (TAG , "Successfully downloaded file " + taskSnapshot );
224222 WritableMap resp = getDownloadTaskAsMap (taskSnapshot );
223+ handleStorageEvent (STORAGE_DOWNLOAD_SUCCESS , path , resp );
224+ //TODO: A little hacky, but otherwise throws a not consumed exception
225+ resp = getDownloadTaskAsMap (taskSnapshot );
225226 callback .invoke (null , resp );
226227 }
227228 }).addOnFailureListener (new OnFailureListener () {
228229 @ Override
229230 public void onFailure (@ NonNull Exception exception ) {
230231 Log .e (TAG , "Failed to download file " + exception .getMessage ());
232+ //TODO: JS Error event
231233 callback .invoke (makeErrorPayload (1 , exception ));
232234 }
233235 });
@@ -258,6 +260,7 @@ public void putFile(final String path, final String localPath, final ReadableMap
258260 public void onFailure (@ NonNull Exception exception ) {
259261 // handle unsuccessful uploads
260262 Log .e (TAG , "Failed to upload file " + exception .getMessage ());
263+ //TODO: JS Error event
261264 callback .invoke (makeErrorPayload (1 , exception ));
262265 }
263266 })
@@ -266,6 +269,9 @@ public void onFailure(@NonNull Exception exception) {
266269 public void onSuccess (UploadTask .TaskSnapshot taskSnapshot ) {
267270 Log .d (TAG , "Successfully uploaded file " + taskSnapshot );
268271 WritableMap resp = getUploadTaskAsMap (taskSnapshot );
272+ handleStorageEvent (STORAGE_UPLOAD_SUCCESS , path , resp );
273+ //TODO: A little hacky, but otherwise throws a not consumed exception
274+ resp = getUploadTaskAsMap (taskSnapshot );
269275 callback .invoke (null , resp );
270276 }
271277 })
@@ -274,19 +280,15 @@ public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
274280 public void onProgress (UploadTask .TaskSnapshot taskSnapshot ) {
275281 Log .d (TAG , "Got upload progress " + taskSnapshot );
276282 WritableMap event = getUploadTaskAsMap (taskSnapshot );
277- //TODO: No need for this if JS listeners are separated
278- event .putString ("eventName" , STORAGE_UPLOAD_PROGRESS );
279- Utils .sendEvent (getReactApplicationContext (), STORAGE_UPLOAD_PROGRESS , event );
283+ handleStorageEvent (STORAGE_STATE_CHANGED , path , event );
280284 }
281285 })
282286 .addOnPausedListener (new OnPausedListener <UploadTask .TaskSnapshot >() {
283287 @ Override
284288 public void onPaused (UploadTask .TaskSnapshot taskSnapshot ) {
285289 Log .d (TAG , "Upload is paused " + taskSnapshot );
286290 WritableMap event = getUploadTaskAsMap (taskSnapshot );
287- //TODO: No need for this if JS listeners are separated
288- event .putString ("eventName" , STORAGE_UPLOAD_PAUSED );
289- Utils .sendEvent (getReactApplicationContext (), STORAGE_UPLOAD_PAUSED , event );
291+ handleStorageEvent (STORAGE_STATE_CHANGED , path , event );
290292 }
291293 });
292294 } catch (Exception ex ) {
@@ -296,11 +298,6 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
296298 }
297299
298300 //Firebase.Storage methods
299- @ ReactMethod
300- public void refFromURL (final String url , final Callback callback ) {
301-
302- }
303-
304301 @ ReactMethod
305302 public void setMaxDownloadRetryTime (final double milliseconds ) {
306303 FirebaseStorage .getInstance ().setMaxDownloadRetryTimeMillis ((long )milliseconds );
@@ -387,6 +384,7 @@ private WritableMap getDownloadTaskAsMap(final StreamDownloadTask.TaskSnapshot t
387384 WritableMap resp = Arguments .createMap ();
388385 resp .putDouble ("bytesTransferred" , taskSnapshot .getBytesTransferred ());
389386 resp .putString ("ref" , taskSnapshot .getStorage ().getPath ());
387+ resp .putString ("state" , this .getTaskStatus (taskSnapshot .getTask ()));
390388 resp .putDouble ("totalBytes" , taskSnapshot .getTotalByteCount ());
391389
392390 return resp ;
@@ -399,6 +397,7 @@ private WritableMap getUploadTaskAsMap(final UploadTask.TaskSnapshot taskSnapsho
399397 resp .putDouble ("bytesTransferred" , taskSnapshot .getBytesTransferred ());
400398 resp .putString ("downloadUrl" , taskSnapshot .getDownloadUrl () != null ? taskSnapshot .getDownloadUrl ().toString () : null );
401399 resp .putString ("ref" , taskSnapshot .getStorage ().getPath ());
400+ resp .putString ("state" , this .getTaskStatus (taskSnapshot .getTask ()));
402401 resp .putDouble ("totalBytes" , taskSnapshot .getTotalByteCount ());
403402
404403 if (taskSnapshot .getMetadata () != null ) {
@@ -409,6 +408,43 @@ private WritableMap getUploadTaskAsMap(final UploadTask.TaskSnapshot taskSnapsho
409408 return resp ;
410409 }
411410
411+ private String getTaskStatus (StorageTask <?> task ) {
412+ if (task .isInProgress ()) {
413+ return "RUNNING" ;
414+ } else if (task .isPaused ()) {
415+ return "PAUSED" ;
416+ } else if (task .isSuccessful () || task .isComplete ()) {
417+ return "SUCCESS" ;
418+ } else if (task .isCanceled ()) {
419+ return "CANCELLED" ;
420+ } else if (task .getException () != null ) {
421+ return "ERROR" ;
422+ } else {
423+ return "UNKNOWN" ;
424+ }
425+ }
426+
427+ private void handleStorageEvent (final String name , final String path , WritableMap body ) {
428+ WritableMap evt = Arguments .createMap ();
429+ evt .putString ("eventName" , name );
430+ evt .putString ("path" , path );
431+ evt .putMap ("body" , body );
432+
433+ Utils .sendEvent (this .getReactApplicationContext (), STORAGE_EVENT , evt );
434+ }
435+
436+ private void handleStorageError (final String path , final StorageException error ) {
437+ WritableMap body = Arguments .createMap ();
438+ body .putString ("path" , path );
439+ body .putString ("message" , error .getMessage ());
440+
441+ WritableMap evt = Arguments .createMap ();
442+ evt .putString ("eventName" , STORAGE_ERROR );
443+ evt .putMap ("body" , body );
444+
445+ Utils .sendEvent (this .getReactApplicationContext (), STORAGE_ERROR , evt );
446+ }
447+
412448 private WritableMap makeErrorPayload (double code , Exception ex ) {
413449 WritableMap error = Arguments .createMap ();
414450 error .putDouble ("code" , code );
0 commit comments