1717
1818import com .facebook .react .bridge .Callback ;
1919import com .facebook .react .bridge .Arguments ;
20+ import com .facebook .react .bridge .WritableArray ;
2021import com .facebook .react .bridge .WritableMap ;
2122import com .facebook .react .bridge .ReactMethod ;
2223import com .facebook .react .bridge .ReadableMap ;
@@ -85,8 +86,122 @@ public boolean isExternalStorageWritable() {
8586 }
8687
8788 @ ReactMethod
88- public void downloadFile (final String remotePath ,
89- final String localFile ,
89+ public void delete (final String path ,
90+ final Callback callback ) {
91+ StorageReference reference = this .getReference (path );
92+ reference .delete ().addOnSuccessListener (new OnSuccessListener <Void >() {
93+ @ Override
94+ public void onSuccess (Void aVoid ) {
95+ WritableMap data = Arguments .createMap ();
96+ data .putString ("success" , "success" );
97+ data .putString ("path" , path );
98+ callback .invoke (null , data );
99+ }
100+ }).addOnFailureListener (new OnFailureListener () {
101+ @ Override
102+ public void onFailure (Exception exception ) {
103+ callback .invoke (makeErrorPayload (1 , exception ));
104+ }
105+ });
106+ }
107+
108+ @ ReactMethod
109+ public void getDownloadURL (final String path ,
110+ final Callback callback ) {
111+ Log .d (TAG , "Download url for remote path: " + path );
112+ final StorageReference reference = this .getReference (path );
113+
114+ Task <Uri > downloadTask = reference .getDownloadUrl ();
115+ downloadTask
116+ .addOnSuccessListener (new OnSuccessListener <Uri >() {
117+ @ Override
118+ public void onSuccess (Uri uri ) {
119+ final WritableMap res = Arguments .createMap ();
120+
121+ res .putString ("status" , "success" );
122+ res .putString ("bucket" , FirebaseStorage .getInstance ().getApp ().getOptions ().getStorageBucket ());
123+ res .putString ("fullPath" , uri .toString ());
124+ res .putString ("path" , uri .getPath ());
125+ res .putString ("url" , uri .toString ());
126+
127+ reference .getMetadata ()
128+ .addOnSuccessListener (new OnSuccessListener <StorageMetadata >() {
129+ @ Override
130+ public void onSuccess (final StorageMetadata storageMetadata ) {
131+ Log .d (TAG , "getMetadata success " + storageMetadata );
132+
133+ res .putMap ("metadata" , getMetadataAsMap (storageMetadata ));
134+ res .putString ("name" , storageMetadata .getName ());
135+ res .putString ("url" , storageMetadata .getDownloadUrl ().toString ());
136+ callback .invoke (null , res );
137+ }
138+ })
139+ .addOnFailureListener (new OnFailureListener () {
140+ @ Override
141+ public void onFailure (@ NonNull Exception exception ) {
142+ Log .e (TAG , "Failure in download " + exception );
143+ final int errorCode = 1 ;
144+ callback .invoke (makeErrorPayload (errorCode , exception ));
145+ }
146+ });
147+
148+ }
149+ })
150+ .addOnFailureListener (new OnFailureListener () {
151+ @ Override
152+ public void onFailure (@ NonNull Exception exception ) {
153+ Log .e (TAG , "Failed to download file " + exception .getMessage ());
154+
155+ WritableMap err = Arguments .createMap ();
156+ err .putString ("status" , "error" );
157+ err .putString ("description" , exception .getLocalizedMessage ());
158+
159+ callback .invoke (err );
160+ }
161+ });
162+ }
163+
164+ @ ReactMethod
165+ public void getMetadata (final String path ,
166+ final Callback callback ) {
167+ StorageReference reference = this .getReference (path );
168+ reference .getMetadata ().addOnSuccessListener (new OnSuccessListener <StorageMetadata >() {
169+ @ Override
170+ public void onSuccess (StorageMetadata storageMetadata ) {
171+ WritableMap data = getMetadataAsMap (storageMetadata );
172+ callback .invoke (null , data );
173+ }
174+ }).addOnFailureListener (new OnFailureListener () {
175+ @ Override
176+ public void onFailure (Exception exception ) {
177+ callback .invoke (makeErrorPayload (1 , exception ));
178+ }
179+ });
180+ }
181+
182+ @ ReactMethod
183+ public void updateMetadata (final String path ,
184+ final ReadableMap metadata ,
185+ final Callback callback ) {
186+ StorageReference reference = this .getReference (path );
187+ StorageMetadata md = buildMetadataFromMap (metadata );
188+ reference .updateMetadata (md ).addOnSuccessListener (new OnSuccessListener <StorageMetadata >() {
189+ @ Override
190+ public void onSuccess (StorageMetadata storageMetadata ) {
191+ WritableMap data = getMetadataAsMap (storageMetadata );
192+ callback .invoke (null , data );
193+ }
194+ }).addOnFailureListener (new OnFailureListener () {
195+ @ Override
196+ public void onFailure (Exception exception ) {
197+ callback .invoke (makeErrorPayload (1 , exception ));
198+ }
199+ });
200+ }
201+
202+ @ ReactMethod
203+ public void downloadFile (final String path ,
204+ final String localPath ,
90205 final Callback callback ) {
91206 if (!isExternalStorageWritable ()) {
92207 Log .w (TAG , "downloadFile failed: external storage not writable" );
@@ -97,16 +212,16 @@ public void downloadFile(final String remotePath,
97212 callback .invoke (error );
98213 return ;
99214 }
100- Log .d (TAG , "downloadFile from remote path: " + remotePath );
215+ Log .d (TAG , "downloadFile from remote path: " + path );
101216
102- StorageReference fileRef = FirebaseStorage . getInstance (). getReference (remotePath );
217+ StorageReference reference = this . getReference (path );
103218
104- fileRef .getStream (new StreamDownloadTask .StreamProcessor () {
219+ reference .getStream (new StreamDownloadTask .StreamProcessor () {
105220 @ Override
106221 public void doInBackground (StreamDownloadTask .TaskSnapshot taskSnapshot , InputStream inputStream ) throws IOException {
107- int indexOfLastSlash = localFile .lastIndexOf ("/" );
108- String pathMinusFileName = indexOfLastSlash >0 ? localFile .substring (0 , indexOfLastSlash ) + "/" : "/" ;
109- String filename = indexOfLastSlash >0 ? localFile .substring (indexOfLastSlash +1 ) : localFile ;
222+ int indexOfLastSlash = localPath .lastIndexOf ("/" );
223+ String pathMinusFileName = indexOfLastSlash >0 ? localPath .substring (0 , indexOfLastSlash ) + "/" : "/" ;
224+ String filename = indexOfLastSlash >0 ? localPath .substring (indexOfLastSlash +1 ) : localPath ;
110225 File fileWithJustPath = new File (pathMinusFileName );
111226 fileWithJustPath .mkdirs ();
112227 File fileWithFullPath = new File (pathMinusFileName , filename );
@@ -150,124 +265,38 @@ public void onSuccess(final StorageMetadata storageMetadata) {
150265 callback .invoke (null , data );
151266 }
152267 })
153- .addOnFailureListener (new OnFailureListener () {
154- @ Override
155- public void onFailure (@ NonNull Exception exception ) {
156- final int errorCode = 1 ;
157- WritableMap data = Arguments .createMap ();
158- StorageException storageException = StorageException .fromException (exception );
159- data .putString ("description" , storageException .getMessage ());
160- data .putInt ("code" , errorCode );
161- callback .invoke (makeErrorPayload (errorCode , exception ));
162- }
163- });
268+ .addOnFailureListener (new OnFailureListener () {
269+ @ Override
270+ public void onFailure (@ NonNull Exception exception ) {
271+ callback .invoke (makeErrorPayload (1 , exception ));
272+ }
273+ });
164274 }
165275 }).addOnFailureListener (new OnFailureListener () {
166276 @ Override
167277 public void onFailure (@ NonNull Exception exception ) {
168- final int errorCode = 1 ;
169- WritableMap data = Arguments .createMap ();
170- StorageException storageException = StorageException .fromException (exception );
171- data .putString ("description" , storageException .getMessage ());
172- data .putInt ("code" , errorCode );
173- callback .invoke (makeErrorPayload (errorCode , exception ));
278+ callback .invoke (makeErrorPayload (1 , exception ));
174279 }
175280 });
176281 }
177282
178283 @ ReactMethod
179- public void downloadUrl (final String remotePath ,
180- final Callback callback ) {
181- Log .d (TAG , "Download url for remote path: " + remotePath );
182- final StorageReference fileRef = FirebaseStorage .getInstance ().getReference (remotePath );
183-
184- Task <Uri > downloadTask = fileRef .getDownloadUrl ();
185- downloadTask
186- .addOnSuccessListener (new OnSuccessListener <Uri >() {
187- @ Override
188- public void onSuccess (Uri uri ) {
189- final WritableMap res = Arguments .createMap ();
190-
191- res .putString ("status" , "success" );
192- res .putString ("bucket" , FirebaseStorage .getInstance ().getApp ().getOptions ().getStorageBucket ());
193- res .putString ("fullPath" , uri .toString ());
194- res .putString ("path" , uri .getPath ());
195- res .putString ("url" , uri .toString ());
196-
197- fileRef .getMetadata ()
198- .addOnSuccessListener (new OnSuccessListener <StorageMetadata >() {
199- @ Override
200- public void onSuccess (final StorageMetadata storageMetadata ) {
201- Log .d (TAG , "getMetadata success " + storageMetadata );
202-
203- res .putMap ("metadata" , getMetadataAsMap (storageMetadata ));
204- res .putString ("name" , storageMetadata .getName ());
205- res .putString ("url" , storageMetadata .getDownloadUrl ().toString ());
206- callback .invoke (null , res );
207- }
208- })
209- .addOnFailureListener (new OnFailureListener () {
210- @ Override
211- public void onFailure (@ NonNull Exception exception ) {
212- Log .e (TAG , "Failure in download " + exception );
213- final int errorCode = 1 ;
214- callback .invoke (makeErrorPayload (errorCode , exception ));
215- }
216- });
217-
218- }
219- })
220- .addOnFailureListener (new OnFailureListener () {
221- @ Override
222- public void onFailure (@ NonNull Exception exception ) {
223- Log .e (TAG , "Failed to download file " + exception .getMessage ());
224-
225- WritableMap err = Arguments .createMap ();
226- err .putString ("status" , "error" );
227- err .putString ("description" , exception .getLocalizedMessage ());
228-
229- callback .invoke (err );
230- }
231- });
232- }
233-
234- private WritableMap getMetadataAsMap (StorageMetadata storageMetadata ) {
235- WritableMap metadata = Arguments .createMap ();
236- metadata .putString ("getBucket" , storageMetadata .getBucket ());
237- metadata .putString ("getName" , storageMetadata .getName ());
238- metadata .putDouble ("sizeBytes" , storageMetadata .getSizeBytes ());
239- metadata .putDouble ("created_at" , storageMetadata .getCreationTimeMillis ());
240- metadata .putDouble ("updated_at" , storageMetadata .getUpdatedTimeMillis ());
241- metadata .putString ("md5hash" , storageMetadata .getMd5Hash ());
242- metadata .putString ("encoding" , storageMetadata .getContentEncoding ());
243- return metadata ;
244- }
245-
246- // STORAGE
247- @ ReactMethod
248- public void uploadFile (final String remotePath , final String filepath , final ReadableMap metadata , final Callback callback ) {
249- StorageReference fileRef = FirebaseStorage .getInstance ().getReference (remotePath );
284+ public void putFile (final String path , final String localPath , final ReadableMap metadata , final Callback callback ) {
285+ StorageReference reference = this .getReference (path );
250286
251- Log .i (TAG , "Upload file: " + filepath + " to " + remotePath );
287+ Log .i (TAG , "Upload file: " + localPath + " to " + path );
252288
253289 try {
254290 Uri file ;
255- if (filepath .startsWith ("content://" )) {
256- String realPath = getRealPathFromURI (filepath );
291+ if (localPath .startsWith ("content://" )) {
292+ String realPath = getRealPathFromURI (localPath );
257293 file = Uri .fromFile (new File (realPath ));
258294 } else {
259- file = Uri .fromFile (new File (filepath ));
295+ file = Uri .fromFile (new File (localPath ));
260296 }
261297
262- StorageMetadata .Builder metadataBuilder = new StorageMetadata .Builder ();
263- Map <String , Object > m = Utils .recursivelyDeconstructReadableMap (metadata );
264-
265- for (Map .Entry <String , Object > entry : m .entrySet ()) {
266- metadataBuilder .setCustomMetadata (entry .getKey (), entry .getValue ().toString ());
267- }
268-
269- StorageMetadata md = metadataBuilder .build ();
270- UploadTask uploadTask = fileRef .putFile (file , md );
298+ StorageMetadata md = buildMetadataFromMap (metadata );
299+ UploadTask uploadTask = reference .putFile (file , md );
271300
272301 // register observers to listen for when the download is done or if it fails
273302 uploadTask
@@ -327,16 +356,77 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
327356 }
328357 }
329358
359+ //Firebase.Storage methods
330360 @ ReactMethod
331- public void getRealPathFromURI (final String uri , final Callback callback ) {
332- try {
333- String path = getRealPathFromURI (uri );
334- callback .invoke (null , path );
335- } catch (Exception ex ) {
336- ex .printStackTrace ();
337- final int errorCode = 1 ;
338- callback .invoke (makeErrorPayload (errorCode , ex ));
361+ public void refFromURL (final String url , final Callback callback ) {
362+
363+ }
364+
365+ @ ReactMethod
366+ public void setMaxDownloadRetryTime (final double milliseconds ) {
367+ FirebaseStorage .getInstance ().setMaxDownloadRetryTimeMillis ((long )milliseconds );
368+ }
369+
370+ @ ReactMethod
371+ public void setMaxOperationRetryTime (final double milliseconds ) {
372+ FirebaseStorage .getInstance ().setMaxOperationRetryTimeMillis ((long )milliseconds );
373+ }
374+
375+ @ ReactMethod
376+ public void setMaxUploadRetryTime (final double milliseconds ) {
377+ FirebaseStorage .getInstance ().setMaxUploadRetryTimeMillis ((long )milliseconds );
378+ }
379+
380+ private StorageReference getReference (String path ) {
381+ if (path .startsWith ("url::" )) {
382+ String url = path .substring (5 );
383+ return FirebaseStorage .getInstance ().getReferenceFromUrl (url );
384+ } else {
385+ return FirebaseStorage .getInstance ().getReference (path );
386+ }
387+ }
388+
389+ private StorageMetadata buildMetadataFromMap (ReadableMap metadata ) {
390+ StorageMetadata .Builder metadataBuilder = new StorageMetadata .Builder ();
391+ Map <String , Object > m = Utils .recursivelyDeconstructReadableMap (metadata );
392+
393+ for (Map .Entry <String , Object > entry : m .entrySet ()) {
394+ metadataBuilder .setCustomMetadata (entry .getKey (), entry .getValue ().toString ());
339395 }
396+
397+ return metadataBuilder .build ();
398+ }
399+
400+ private WritableMap getMetadataAsMap (StorageMetadata storageMetadata ) {
401+ WritableMap metadata = Arguments .createMap ();
402+ metadata .putString ("bucket" , storageMetadata .getBucket ());
403+ metadata .putString ("generation" , storageMetadata .getGeneration ());
404+ metadata .putString ("metageneration" , storageMetadata .getMetadataGeneration ());
405+ metadata .putString ("fullPath" , storageMetadata .getPath ());
406+ metadata .putString ("name" , storageMetadata .getName ());
407+ metadata .putDouble ("size" , storageMetadata .getSizeBytes ());
408+ metadata .putDouble ("timeCreated" , storageMetadata .getCreationTimeMillis ());
409+ metadata .putDouble ("updated" , storageMetadata .getUpdatedTimeMillis ());
410+ metadata .putString ("md5hash" , storageMetadata .getMd5Hash ());
411+ metadata .putString ("cacheControl" , storageMetadata .getCacheControl ());
412+ metadata .putString ("contentDisposition" , storageMetadata .getContentDisposition ());
413+ metadata .putString ("contentEncoding" , storageMetadata .getContentEncoding ());
414+ metadata .putString ("contentLanguage" , storageMetadata .getContentLanguage ());
415+ metadata .putString ("contentType" , storageMetadata .getContentType ());
416+
417+ WritableArray downloadURLs = Arguments .createArray ();
418+ for (Uri uri : storageMetadata .getDownloadUrls ()) {
419+ downloadURLs .pushString (uri .getPath ());
420+ }
421+ metadata .putArray ("downloadURLs" , downloadURLs );
422+
423+ WritableMap customMetadata = Arguments .createMap ();
424+ for (String key : storageMetadata .getCustomMetadataKeys ()) {
425+ customMetadata .putString (key , storageMetadata .getCustomMetadata (key ));
426+ }
427+ metadata .putMap ("customMetadata" , customMetadata );
428+
429+ return metadata ;
340430 }
341431
342432 private String getRealPathFromURI (final String uri ) {
0 commit comments