29
29
import com .google .android .gms .tasks .OnFailureListener ;
30
30
import com .google .android .gms .tasks .OnSuccessListener ;
31
31
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 ;
32
36
import com .google .firebase .storage .StreamDownloadTask ;
33
37
import com .google .firebase .storage .UploadTask ;
34
38
import com .google .firebase .storage .FirebaseStorage ;
@@ -55,13 +59,11 @@ public class FirestackStorage extends ReactContextBaseJavaModule {
55
59
private static final String FileTypeRegular = "FILETYPE_REGULAR" ;
56
60
private static final String FileTypeDirectory = "FILETYPE_DIRECTORY" ;
57
61
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" ;
65
67
private static final String STORAGE_DOWNLOAD_SUCCESS = "download_success" ;
66
68
private static final String STORAGE_DOWNLOAD_FAILURE = "download_failure" ;
67
69
@@ -204,30 +206,30 @@ public void doInBackground(StreamDownloadTask.TaskSnapshot taskSnapshot, InputSt
204
206
public void onProgress (StreamDownloadTask .TaskSnapshot taskSnapshot ) {
205
207
Log .d (TAG , "Got download progress " + taskSnapshot );
206
208
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 );
210
210
}
211
211
}).addOnPausedListener (new OnPausedListener <StreamDownloadTask .TaskSnapshot >() {
212
212
@ Override
213
213
public void onPaused (StreamDownloadTask .TaskSnapshot taskSnapshot ) {
214
214
Log .d (TAG , "Download is paused " + taskSnapshot );
215
215
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 );
219
217
}
220
218
}).addOnSuccessListener (new OnSuccessListener <StreamDownloadTask .TaskSnapshot >() {
221
219
@ Override
222
220
public void onSuccess (StreamDownloadTask .TaskSnapshot taskSnapshot ) {
223
221
Log .d (TAG , "Successfully downloaded file " + taskSnapshot );
224
222
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 );
225
226
callback .invoke (null , resp );
226
227
}
227
228
}).addOnFailureListener (new OnFailureListener () {
228
229
@ Override
229
230
public void onFailure (@ NonNull Exception exception ) {
230
231
Log .e (TAG , "Failed to download file " + exception .getMessage ());
232
+ //TODO: JS Error event
231
233
callback .invoke (makeErrorPayload (1 , exception ));
232
234
}
233
235
});
@@ -258,6 +260,7 @@ public void putFile(final String path, final String localPath, final ReadableMap
258
260
public void onFailure (@ NonNull Exception exception ) {
259
261
// handle unsuccessful uploads
260
262
Log .e (TAG , "Failed to upload file " + exception .getMessage ());
263
+ //TODO: JS Error event
261
264
callback .invoke (makeErrorPayload (1 , exception ));
262
265
}
263
266
})
@@ -266,6 +269,9 @@ public void onFailure(@NonNull Exception exception) {
266
269
public void onSuccess (UploadTask .TaskSnapshot taskSnapshot ) {
267
270
Log .d (TAG , "Successfully uploaded file " + taskSnapshot );
268
271
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 );
269
275
callback .invoke (null , resp );
270
276
}
271
277
})
@@ -274,19 +280,15 @@ public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
274
280
public void onProgress (UploadTask .TaskSnapshot taskSnapshot ) {
275
281
Log .d (TAG , "Got upload progress " + taskSnapshot );
276
282
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 );
280
284
}
281
285
})
282
286
.addOnPausedListener (new OnPausedListener <UploadTask .TaskSnapshot >() {
283
287
@ Override
284
288
public void onPaused (UploadTask .TaskSnapshot taskSnapshot ) {
285
289
Log .d (TAG , "Upload is paused " + taskSnapshot );
286
290
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 );
290
292
}
291
293
});
292
294
} catch (Exception ex ) {
@@ -296,11 +298,6 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
296
298
}
297
299
298
300
//Firebase.Storage methods
299
- @ ReactMethod
300
- public void refFromURL (final String url , final Callback callback ) {
301
-
302
- }
303
-
304
301
@ ReactMethod
305
302
public void setMaxDownloadRetryTime (final double milliseconds ) {
306
303
FirebaseStorage .getInstance ().setMaxDownloadRetryTimeMillis ((long )milliseconds );
@@ -387,6 +384,7 @@ private WritableMap getDownloadTaskAsMap(final StreamDownloadTask.TaskSnapshot t
387
384
WritableMap resp = Arguments .createMap ();
388
385
resp .putDouble ("bytesTransferred" , taskSnapshot .getBytesTransferred ());
389
386
resp .putString ("ref" , taskSnapshot .getStorage ().getPath ());
387
+ resp .putString ("state" , this .getTaskStatus (taskSnapshot .getTask ()));
390
388
resp .putDouble ("totalBytes" , taskSnapshot .getTotalByteCount ());
391
389
392
390
return resp ;
@@ -399,6 +397,7 @@ private WritableMap getUploadTaskAsMap(final UploadTask.TaskSnapshot taskSnapsho
399
397
resp .putDouble ("bytesTransferred" , taskSnapshot .getBytesTransferred ());
400
398
resp .putString ("downloadUrl" , taskSnapshot .getDownloadUrl () != null ? taskSnapshot .getDownloadUrl ().toString () : null );
401
399
resp .putString ("ref" , taskSnapshot .getStorage ().getPath ());
400
+ resp .putString ("state" , this .getTaskStatus (taskSnapshot .getTask ()));
402
401
resp .putDouble ("totalBytes" , taskSnapshot .getTotalByteCount ());
403
402
404
403
if (taskSnapshot .getMetadata () != null ) {
@@ -409,6 +408,43 @@ private WritableMap getUploadTaskAsMap(final UploadTask.TaskSnapshot taskSnapsho
409
408
return resp ;
410
409
}
411
410
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
+
412
448
private WritableMap makeErrorPayload (double code , Exception ex ) {
413
449
WritableMap error = Arguments .createMap ();
414
450
error .putDouble ("code" , code );
0 commit comments