12
12
import java .io .FileNotFoundException ;
13
13
14
14
import android .net .Uri ;
15
+ import android .provider .MediaStore ;
16
+ import android .database .Cursor ;
15
17
import android .support .annotation .NonNull ;
16
18
import android .support .annotation .Nullable ;
17
19
@@ -80,18 +82,49 @@ public void downloadUrl(final String javascriptStorageBucket,
80
82
FirebaseStorage storage = FirebaseStorage .getInstance ();
81
83
String storageBucket = storage .getApp ().getOptions ().getStorageBucket ();
82
84
String storageUrl = "gs://" +storageBucket ;
83
- StorageReference storageRef = storage .getReferenceFromUrl (storageUrl );
84
- StorageReference fileRef = storageRef .child (path );
85
+ Log .d (TAG , "Storage url " + storageUrl + path );
86
+ final StorageReference storageRef = storage .getReferenceFromUrl (storageUrl );
87
+ final StorageReference fileRef = storageRef .child (path );
85
88
86
89
Task <Uri > downloadTask = fileRef .getDownloadUrl ();
87
90
downloadTask .addOnSuccessListener (new OnSuccessListener <Uri >() {
88
91
@ Override
89
92
public void onSuccess (Uri uri ) {
90
- WritableMap res = Arguments .createMap ();
93
+ final WritableMap res = Arguments .createMap ();
94
+
91
95
res .putString ("status" , "success" );
96
+ res .putString ("bucket" , storageRef .getBucket ());
97
+ res .putString ("fullPath" , uri .toString ());
92
98
res .putString ("path" , uri .getPath ());
93
- res .putString ("url" , uri .toString ());
94
- callback .invoke (null , res );
99
+
100
+ storageRef .getMetadata ()
101
+ .addOnSuccessListener (new OnSuccessListener <StorageMetadata >() {
102
+ @ Override
103
+ public void onSuccess (final StorageMetadata storageMetadata ) {
104
+ Log .d (TAG , "getMetadata success " + storageMetadata );
105
+ res .putString ("name" , storageMetadata .getName ());
106
+
107
+ WritableMap metadata = Arguments .createMap ();
108
+ metadata .putString ("getBucket" , storageMetadata .getBucket ());
109
+ metadata .putString ("getName" , storageMetadata .getName ());
110
+ metadata .putDouble ("sizeBytes" , storageMetadata .getSizeBytes ());
111
+ metadata .putDouble ("created_at" , storageMetadata .getCreationTimeMillis ());
112
+ metadata .putDouble ("updated_at" , storageMetadata .getUpdatedTimeMillis ());
113
+ metadata .putString ("md5hash" , storageMetadata .getMd5Hash ());
114
+ metadata .putString ("encoding" , storageMetadata .getContentEncoding ());
115
+ metadata .putString ("downloadUrl" , storageMetadata .getDownloadUrl ().toString ());
116
+
117
+ res .putMap ("metadata" , metadata );
118
+ callback .invoke (null , res );
119
+ }
120
+ }).addOnFailureListener (new OnFailureListener () {
121
+ @ Override
122
+ public void onFailure (@ NonNull Exception exception ) {
123
+ Log .e (TAG , "Failure in download " + exception );
124
+ callback .invoke (makeErrorPayload (1 , exception ));
125
+ }
126
+ });
127
+
95
128
}
96
129
}).addOnFailureListener (new OnFailureListener () {
97
130
@ Override
@@ -144,20 +177,7 @@ public void onFailure(@NonNull Exception exception) {
144
177
public void onSuccess (UploadTask .TaskSnapshot taskSnapshot ) {
145
178
Log .d (TAG , "Successfully uploaded file " + taskSnapshot );
146
179
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
147
- Uri downloadUrl = taskSnapshot .getDownloadUrl ();
148
- StorageMetadata d = taskSnapshot .getMetadata ();
149
-
150
- WritableMap resp = Arguments .createMap ();
151
- resp .putString ("downloadUrl" , downloadUrl .toString ());
152
- resp .putString ("fullPath" , d .getPath ());
153
- resp .putString ("bucket" , d .getBucket ());
154
- resp .putString ("name" , d .getName ());
155
-
156
- WritableMap metadataObj = Arguments .createMap ();
157
- metadataObj .putString ("cacheControl" , d .getCacheControl ());
158
- metadataObj .putString ("contentDisposition" , d .getContentDisposition ());
159
- metadataObj .putString ("contentType" , d .getContentType ());
160
- resp .putMap ("metadata" , metadataObj );
180
+ WritableMap resp = getDownloadData (taskSnapshot );
161
181
// NSDictionary *props = @{
162
182
// @"fullPath": ref.fullPath,
163
183
// @"bucket": ref.bucket,
@@ -171,13 +191,18 @@ public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
171
191
.addOnProgressListener (new OnProgressListener <UploadTask .TaskSnapshot >() {
172
192
@ Override
173
193
public void onProgress (UploadTask .TaskSnapshot taskSnapshot ) {
174
- double progress = (100.0 * taskSnapshot .getBytesTransferred ()) / taskSnapshot .getTotalByteCount ();
175
- System .out .println ("Upload is " + progress + "% done" );
176
-
177
- WritableMap data = Arguments .createMap ();
178
- data .putString ("eventName" , "upload_progress" );
179
- data .putDouble ("progress" , progress );
180
- FirestackUtils .sendEvent (mReactContext , "upload_progress" , data );
194
+ double totalBytes = taskSnapshot .getTotalByteCount ();
195
+ double bytesTransferred = taskSnapshot .getBytesTransferred ();
196
+ double progress = (100.0 * bytesTransferred ) / totalBytes ;
197
+
198
+ System .out .println ("Transferred " + bytesTransferred + "/" + totalBytes + "(" +progress + "% complete)" );
199
+
200
+ if (progress >= 0 ) {
201
+ WritableMap data = Arguments .createMap ();
202
+ data .putString ("eventName" , "upload_progress" );
203
+ data .putDouble ("progress" , progress );
204
+ FirestackUtils .sendEvent (mReactContext , "upload_progress" , data );
205
+ }
181
206
}
182
207
}).addOnPausedListener (new OnPausedListener <UploadTask .TaskSnapshot >() {
183
208
@ Override
@@ -193,14 +218,54 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
193
218
});
194
219
}
195
220
catch (Exception ex ) {
196
- WritableMap err = Arguments . createMap ( );
197
-
198
- err . putString ( "error" , "FileNotFoundException" );
221
+ callback . invoke ( makeErrorPayload ( 2 , ex ) );
222
+ }
223
+ }
199
224
200
- callback .invoke (err );
225
+ @ ReactMethod
226
+ public void getRealPathFromURI (final String uri , final Callback callback ) {
227
+ try {
228
+ Context context = getReactApplicationContext ();
229
+ String [] proj = {MediaStore .Images .Media .DATA };
230
+ Cursor cursor = context .getContentResolver ().query (Uri .parse (uri ), proj , null , null , null );
231
+ int column_index = cursor .getColumnIndexOrThrow (MediaStore .Images .Media .DATA );
232
+ cursor .moveToFirst ();
233
+ String path = cursor .getString (column_index );
234
+ cursor .close ();
235
+
236
+ callback .invoke (null , path );
237
+ } catch (Exception ex ) {
238
+ ex .printStackTrace ();
239
+ callback .invoke (makeErrorPayload (1 , ex ));
201
240
}
202
241
}
203
242
243
+ private WritableMap getDownloadData (final UploadTask .TaskSnapshot taskSnapshot ) {
244
+ Uri downloadUrl = taskSnapshot .getDownloadUrl ();
245
+ StorageMetadata d = taskSnapshot .getMetadata ();
246
+
247
+ WritableMap resp = Arguments .createMap ();
248
+ resp .putString ("downloadUrl" , downloadUrl .toString ());
249
+ resp .putString ("fullPath" , d .getPath ());
250
+ resp .putString ("bucket" , d .getBucket ());
251
+ resp .putString ("name" , d .getName ());
252
+
253
+ WritableMap metadataObj = Arguments .createMap ();
254
+ metadataObj .putString ("cacheControl" , d .getCacheControl ());
255
+ metadataObj .putString ("contentDisposition" , d .getContentDisposition ());
256
+ metadataObj .putString ("contentType" , d .getContentType ());
257
+ resp .putMap ("metadata" , metadataObj );
258
+
259
+ return resp ;
260
+ }
261
+
262
+ private WritableMap makeErrorPayload (double code , Exception ex ) {
263
+ WritableMap error = Arguments .createMap ();
264
+ error .putDouble ("code" , code );
265
+ error .putString ("message" , ex .getMessage ());
266
+ return error ;
267
+ }
268
+
204
269
// Comes almost directory from react-native-fs
205
270
@ Override
206
271
public Map <String , Object > getConstants () {
0 commit comments