|
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.storage.StorageException; |
33 | 32 | import com.google.firebase.storage.StreamDownloadTask;
|
34 | 33 | import com.google.firebase.storage.UploadTask;
|
35 | 34 | import com.google.firebase.storage.FirebaseStorage;
|
@@ -237,44 +236,32 @@ public void doInBackground(StreamDownloadTask.TaskSnapshot taskSnapshot, InputSt
|
237 | 236 | }).addOnProgressListener(new OnProgressListener<StreamDownloadTask.TaskSnapshot>() {
|
238 | 237 | @Override
|
239 | 238 | public void onProgress(StreamDownloadTask.TaskSnapshot taskSnapshot) {
|
240 |
| - WritableMap data = Arguments.createMap(); |
241 |
| - data.putString("ref", taskSnapshot.getStorage().getBucket()); |
242 |
| - double percentComplete = taskSnapshot.getTotalByteCount() == 0 ? 0.0f : 100.0f * (taskSnapshot.getBytesTransferred()) / (taskSnapshot.getTotalByteCount()); |
243 |
| - data.putDouble("progress", percentComplete); |
244 |
| - Utils.sendEvent(mReactContext, STORAGE_DOWNLOAD_PROGRESS, data); |
| 239 | + Log.d(TAG, "Got download progress " + taskSnapshot); |
| 240 | + WritableMap event = getDownloadTaskAsMap(taskSnapshot); |
| 241 | + //TODO: No need for this if JS listeners are separated |
| 242 | + event.putString("eventName", STORAGE_DOWNLOAD_PROGRESS); |
| 243 | + Utils.sendEvent(getReactApplicationContext(), STORAGE_DOWNLOAD_PROGRESS, event); |
245 | 244 | }
|
246 | 245 | }).addOnPausedListener(new OnPausedListener<StreamDownloadTask.TaskSnapshot>() {
|
247 | 246 | @Override
|
248 | 247 | public void onPaused(StreamDownloadTask.TaskSnapshot taskSnapshot) {
|
249 |
| - WritableMap data = Arguments.createMap(); |
250 |
| - data.putString("ref", taskSnapshot.getStorage().getBucket()); |
251 |
| - Utils.sendEvent(mReactContext, STORAGE_DOWNLOAD_PAUSED, data); |
| 248 | + Log.d(TAG, "Download is paused " + taskSnapshot); |
| 249 | + WritableMap event = getDownloadTaskAsMap(taskSnapshot); |
| 250 | + //TODO: No need for this if JS listeners are separated |
| 251 | + event.putString("eventName", STORAGE_DOWNLOAD_PAUSED); |
| 252 | + Utils.sendEvent(getReactApplicationContext(), STORAGE_DOWNLOAD_PAUSED, event); |
252 | 253 | }
|
253 | 254 | }).addOnSuccessListener(new OnSuccessListener<StreamDownloadTask.TaskSnapshot>() {
|
254 | 255 | @Override
|
255 | 256 | public void onSuccess(StreamDownloadTask.TaskSnapshot taskSnapshot) {
|
256 |
| - final WritableMap data = Arguments.createMap(); |
257 |
| - StorageReference ref = taskSnapshot.getStorage(); |
258 |
| - data.putString("fullPath", ref.getPath()); |
259 |
| - data.putString("bucket", ref.getBucket()); |
260 |
| - data.putString("name", ref.getName()); |
261 |
| - ref.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() { |
262 |
| - @Override |
263 |
| - public void onSuccess(final StorageMetadata storageMetadata) { |
264 |
| - data.putMap("metadata", getMetadataAsMap(storageMetadata)); |
265 |
| - callback.invoke(null, data); |
266 |
| - } |
267 |
| - }) |
268 |
| - .addOnFailureListener(new OnFailureListener() { |
269 |
| - @Override |
270 |
| - public void onFailure(@NonNull Exception exception) { |
271 |
| - callback.invoke(makeErrorPayload(1, exception)); |
272 |
| - } |
273 |
| - }); |
| 257 | + Log.d(TAG, "Successfully downloaded file " + taskSnapshot); |
| 258 | + WritableMap resp = getDownloadTaskAsMap(taskSnapshot); |
| 259 | + callback.invoke(null, resp); |
274 | 260 | }
|
275 | 261 | }).addOnFailureListener(new OnFailureListener() {
|
276 | 262 | @Override
|
277 | 263 | public void onFailure(@NonNull Exception exception) {
|
| 264 | + Log.e(TAG, "Failed to download file " + exception.getMessage()); |
278 | 265 | callback.invoke(makeErrorPayload(1, exception));
|
279 | 266 | }
|
280 | 267 | });
|
@@ -305,49 +292,35 @@ public void putFile(final String path, final String localPath, final ReadableMap
|
305 | 292 | public void onFailure(@NonNull Exception exception) {
|
306 | 293 | // handle unsuccessful uploads
|
307 | 294 | Log.e(TAG, "Failed to upload file " + exception.getMessage());
|
308 |
| - |
309 |
| - WritableMap err = Arguments.createMap(); |
310 |
| - err.putString("description", exception.getLocalizedMessage()); |
311 |
| - |
312 |
| - callback.invoke(err); |
| 295 | + callback.invoke(makeErrorPayload(1, exception)); |
313 | 296 | }
|
314 | 297 | })
|
315 | 298 | .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
|
316 | 299 | @Override
|
317 | 300 | public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
|
318 | 301 | Log.d(TAG, "Successfully uploaded file " + taskSnapshot);
|
319 |
| - // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL. |
320 |
| - WritableMap resp = getDownloadData(taskSnapshot); |
| 302 | + WritableMap resp = getUploadTaskAsMap(taskSnapshot); |
321 | 303 | callback.invoke(null, resp);
|
322 | 304 | }
|
323 | 305 | })
|
324 | 306 | .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
|
325 | 307 | @Override
|
326 | 308 | public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
|
327 |
| - double totalBytes = taskSnapshot.getTotalByteCount(); |
328 |
| - double bytesTransferred = taskSnapshot.getBytesTransferred(); |
329 |
| - double progress = (100.0 * bytesTransferred) / totalBytes; |
330 |
| - |
331 |
| - System.out.println("Transferred " + bytesTransferred + "/" + totalBytes + "(" + progress + "% complete)"); |
332 |
| - |
333 |
| - if (progress >= 0) { |
334 |
| - WritableMap data = Arguments.createMap(); |
335 |
| - data.putString("eventName", STORAGE_UPLOAD_PROGRESS); |
336 |
| - data.putDouble("progress", progress); |
337 |
| - Utils.sendEvent(getReactApplicationContext(), STORAGE_UPLOAD_PROGRESS, data); |
338 |
| - } |
| 309 | + Log.d(TAG, "Got upload progress " + taskSnapshot); |
| 310 | + WritableMap event = getUploadTaskAsMap(taskSnapshot); |
| 311 | + //TODO: No need for this if JS listeners are separated |
| 312 | + event.putString("eventName", STORAGE_UPLOAD_PROGRESS); |
| 313 | + Utils.sendEvent(getReactApplicationContext(), STORAGE_UPLOAD_PROGRESS, event); |
339 | 314 | }
|
340 | 315 | })
|
341 | 316 | .addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
|
342 | 317 | @Override
|
343 | 318 | public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
|
344 |
| - System.out.println("Upload is paused"); |
345 |
| - StorageMetadata d = taskSnapshot.getMetadata(); |
346 |
| - String bucket = d.getBucket(); |
347 |
| - WritableMap data = Arguments.createMap(); |
348 |
| - data.putString("eventName", STORAGE_UPLOAD_PAUSED); |
349 |
| - data.putString("ref", bucket); |
350 |
| - Utils.sendEvent(getReactApplicationContext(), STORAGE_UPLOAD_PAUSED, data); |
| 319 | + Log.d(TAG, "Upload is paused " + taskSnapshot); |
| 320 | + WritableMap event = getUploadTaskAsMap(taskSnapshot); |
| 321 | + //TODO: No need for this if JS listeners are separated |
| 322 | + event.putString("eventName", STORAGE_UPLOAD_PAUSED); |
| 323 | + Utils.sendEvent(getReactApplicationContext(), STORAGE_UPLOAD_PAUSED, event); |
351 | 324 | }
|
352 | 325 | });
|
353 | 326 | } catch (Exception ex) {
|
@@ -444,21 +417,28 @@ private String getRealPathFromURI(final String uri) {
|
444 | 417 | }
|
445 | 418 | }
|
446 | 419 |
|
447 |
| - private WritableMap getDownloadData(final UploadTask.TaskSnapshot taskSnapshot) { |
448 |
| - Uri downloadUrl = taskSnapshot.getDownloadUrl(); |
| 420 | + private WritableMap getDownloadTaskAsMap(final StreamDownloadTask.TaskSnapshot taskSnapshot) { |
| 421 | + WritableMap resp = Arguments.createMap(); |
| 422 | + resp.putDouble("bytesTransferred", taskSnapshot.getBytesTransferred()); |
| 423 | + resp.putString("ref", taskSnapshot.getStorage().getPath()); |
| 424 | + resp.putDouble("totalBytes", taskSnapshot.getTotalByteCount()); |
| 425 | + |
| 426 | + return resp; |
| 427 | + } |
| 428 | + |
| 429 | + private WritableMap getUploadTaskAsMap(final UploadTask.TaskSnapshot taskSnapshot) { |
449 | 430 | StorageMetadata d = taskSnapshot.getMetadata();
|
450 | 431 |
|
451 | 432 | WritableMap resp = Arguments.createMap();
|
452 |
| - resp.putString("downloadUrl", downloadUrl.toString()); |
453 |
| - resp.putString("fullPath", d.getPath()); |
454 |
| - resp.putString("bucket", d.getBucket()); |
455 |
| - resp.putString("name", d.getName()); |
456 |
| - |
457 |
| - WritableMap metadataObj = Arguments.createMap(); |
458 |
| - metadataObj.putString("cacheControl", d.getCacheControl()); |
459 |
| - metadataObj.putString("contentDisposition", d.getContentDisposition()); |
460 |
| - metadataObj.putString("contentType", d.getContentType()); |
461 |
| - resp.putMap("metadata", metadataObj); |
| 433 | + resp.putDouble("bytesTransferred", taskSnapshot.getBytesTransferred()); |
| 434 | + resp.putString("downloadUrl", taskSnapshot.getDownloadUrl() != null ? taskSnapshot.getDownloadUrl().toString() : null); |
| 435 | + resp.putString("ref", taskSnapshot.getStorage().getPath()); |
| 436 | + resp.putDouble("totalBytes", taskSnapshot.getTotalByteCount()); |
| 437 | + |
| 438 | + if (taskSnapshot.getMetadata() != null) { |
| 439 | + WritableMap metadata = getMetadataAsMap(taskSnapshot.getMetadata()); |
| 440 | + resp.putMap("metadata", metadata); |
| 441 | + } |
462 | 442 |
|
463 | 443 | return resp;
|
464 | 444 | }
|
|
0 commit comments