44import android .os .StatFs ;
55import android .content .Context ;
66import android .util .Log ;
7+
78import java .util .Map ;
89import java .util .HashMap ;
910import java .io .File ;
@@ -79,27 +80,27 @@ public String getName() {
7980 public void downloadUrl (final String javascriptStorageBucket ,
8081 final String path ,
8182 final Callback callback ) {
82- FirebaseStorage storage = FirebaseStorage .getInstance ();
83- String storageBucket = storage .getApp ().getOptions ().getStorageBucket ();
84- String storageUrl = "gs://" + storageBucket ;
85- Log .d (TAG , "Storage url " + storageUrl + path );
86- final StorageReference storageRef = storage .getReferenceFromUrl (storageUrl );
87- final StorageReference fileRef = storageRef .child (path );
88-
89- Task <Uri > downloadTask = fileRef .getDownloadUrl ();
90- downloadTask .addOnSuccessListener (new OnSuccessListener <Uri >() {
91- @ Override
92- public void onSuccess (Uri uri ) {
93- final WritableMap res = Arguments .createMap ();
94-
95- res .putString ("status" , "success" );
96- res .putString ("bucket" , storageRef .getBucket ());
97- res .putString ("fullPath" , uri .toString ());
98- res .putString ("path" , uri .getPath ());
99- res .putString ("url" , uri .toString ());
100-
101- fileRef .getMetadata ()
102- .addOnSuccessListener (new OnSuccessListener <StorageMetadata >() {
83+ FirebaseStorage storage = FirebaseStorage .getInstance ();
84+ String storageBucket = storage .getApp ().getOptions ().getStorageBucket ();
85+ String storageUrl = "gs://" + storageBucket ;
86+ Log .d (TAG , "Storage url " + storageUrl + path );
87+ final StorageReference storageRef = storage .getReferenceFromUrl (storageUrl );
88+ final StorageReference fileRef = storageRef .child (path );
89+
90+ Task <Uri > downloadTask = fileRef .getDownloadUrl ();
91+ downloadTask .addOnSuccessListener (new OnSuccessListener <Uri >() {
92+ @ Override
93+ public void onSuccess (Uri uri ) {
94+ final WritableMap res = Arguments .createMap ();
95+
96+ res .putString ("status" , "success" );
97+ res .putString ("bucket" , storageRef .getBucket ());
98+ res .putString ("fullPath" , uri .toString ());
99+ res .putString ("path" , uri .getPath ());
100+ res .putString ("url" , uri .toString ());
101+
102+ fileRef .getMetadata ()
103+ .addOnSuccessListener (new OnSuccessListener <StorageMetadata >() {
103104 @ Override
104105 public void onSuccess (final StorageMetadata storageMetadata ) {
105106 Log .d (TAG , "getMetadata success " + storageMetadata );
@@ -118,27 +119,27 @@ public void onSuccess(final StorageMetadata storageMetadata) {
118119 res .putMap ("metadata" , metadata );
119120 callback .invoke (null , res );
120121 }
121- }).addOnFailureListener (new OnFailureListener () {
122- @ Override
123- public void onFailure (@ NonNull Exception exception ) {
124- Log .e (TAG , "Failure in download " + exception );
125- callback .invoke (makeErrorPayload (1 , exception ));
126- }
127- });
122+ }).addOnFailureListener (new OnFailureListener () {
123+ @ Override
124+ public void onFailure (@ NonNull Exception exception ) {
125+ Log .e (TAG , "Failure in download " + exception );
126+ callback .invoke (makeErrorPayload (1 , exception ));
127+ }
128+ });
128129
129- }
130- }).addOnFailureListener (new OnFailureListener () {
131- @ Override
132- public void onFailure (@ NonNull Exception exception ) {
133- Log .e (TAG , "Failed to download file " + exception .getMessage ());
130+ }
131+ }).addOnFailureListener (new OnFailureListener () {
132+ @ Override
133+ public void onFailure (@ NonNull Exception exception ) {
134+ Log .e (TAG , "Failed to download file " + exception .getMessage ());
134135
135- WritableMap err = Arguments .createMap ();
136- err .putString ("status" , "error" );
137- err .putString ("description" , exception .getLocalizedMessage ());
136+ WritableMap err = Arguments .createMap ();
137+ err .putString ("status" , "error" );
138+ err .putString ("description" , exception .getLocalizedMessage ());
138139
139- callback .invoke (err );
140- }
141- });
140+ callback .invoke (err );
141+ }
142+ });
142143 }
143144
144145 // STORAGE
@@ -149,17 +150,21 @@ public void uploadFile(final String urlStr, final String name, final String file
149150 StorageReference storageRef = storage .getReferenceFromUrl (urlStr );
150151 StorageReference fileRef = storageRef .child (name );
151152
152- Log .i (TAG , "From file: " + filepath + " to " + urlStr + " with name " + name );
153+ Log .i (TAG , "From file: " + filepath + " to " + urlStr + " with name " + name );
154+
153155 try {
154- // InputStream stream = new FileInputStream(new File(filepath));
155156 Uri file = Uri .fromFile (new File (filepath ));
156157
157158 StorageMetadata .Builder metadataBuilder = new StorageMetadata .Builder ();
158159 Map <String , Object > m = FirestackUtils .recursivelyDeconstructReadableMap (metadata );
159160
161+ for (Map .Entry <String , Object > entry : m .entrySet ()) {
162+ metadataBuilder .setCustomMetadata (entry .getKey (), entry .getValue ().toString ());
163+ }
164+
160165 StorageMetadata md = metadataBuilder .build ();
161166 UploadTask uploadTask = fileRef .putFile (file , md );
162- // UploadTask uploadTask = fileRef.putStream(stream, md);
167+ // uploadTask uploadTask = fileRef.putStream(stream, md);
163168
164169 // Register observers to listen for when the download is done or if it fails
165170 uploadTask .addOnFailureListener (new OnFailureListener () {
@@ -179,33 +184,26 @@ public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
179184 Log .d (TAG , "Successfully uploaded file " + taskSnapshot );
180185 // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
181186 WritableMap resp = getDownloadData (taskSnapshot );
182- // NSDictionary *props = @{
183- // @"fullPath": ref.fullPath,
184- // @"bucket": ref.bucket,
185- // @"name": ref.name,
186- // @"metadata": [snapshot.metadata dictionaryRepresentation]
187- // };
188-
189187 callback .invoke (null , resp );
190188 }
191189 })
192- .addOnProgressListener (new OnProgressListener <UploadTask .TaskSnapshot >() {
193- @ Override
194- public void onProgress (UploadTask .TaskSnapshot taskSnapshot ) {
195- double totalBytes = taskSnapshot .getTotalByteCount ();
196- double bytesTransferred = taskSnapshot .getBytesTransferred ();
197- double progress = (100.0 * bytesTransferred ) / totalBytes ;
198-
199- System .out .println ("Transferred " + bytesTransferred + "/" + totalBytes + "(" + progress + "% complete)" );
200-
201- if (progress >= 0 ) {
202- WritableMap data = Arguments .createMap ();
203- data .putString ("eventName" , "upload_progress" );
204- data .putDouble ("progress" , progress );
205- FirestackUtils .sendEvent (mReactContext , "upload_progress" , data );
206- }
207- }
208- }).addOnPausedListener (new OnPausedListener <UploadTask .TaskSnapshot >() {
190+ .addOnProgressListener (new OnProgressListener <UploadTask .TaskSnapshot >() {
191+ @ Override
192+ public void onProgress (UploadTask .TaskSnapshot taskSnapshot ) {
193+ double totalBytes = taskSnapshot .getTotalByteCount ();
194+ double bytesTransferred = taskSnapshot .getBytesTransferred ();
195+ double progress = (100.0 * bytesTransferred ) / totalBytes ;
196+
197+ System .out .println ("Transferred " + bytesTransferred + "/" + totalBytes + "(" + progress + "% complete)" );
198+
199+ if (progress >= 0 ) {
200+ WritableMap data = Arguments .createMap ();
201+ data .putString ("eventName" , "upload_progress" );
202+ data .putDouble ("progress" , progress );
203+ FirestackUtils .sendEvent (mReactContext , "upload_progress" , data );
204+ }
205+ }
206+ }).addOnPausedListener (new OnPausedListener <UploadTask .TaskSnapshot >() {
209207 @ Override
210208 public void onPaused (UploadTask .TaskSnapshot taskSnapshot ) {
211209 System .out .println ("Upload is paused" );
@@ -217,8 +215,7 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
217215 FirestackUtils .sendEvent (mReactContext , "upload_paused" , data );
218216 }
219217 });
220- }
221- catch (Exception ex ) {
218+ } catch (Exception ex ) {
222219 callback .invoke (makeErrorPayload (2 , ex ));
223220 }
224221 }
@@ -227,8 +224,8 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
227224 public void getRealPathFromURI (final String uri , final Callback callback ) {
228225 try {
229226 Context context = getReactApplicationContext ();
230- String [] proj = {MediaStore .Images .Media .DATA };
231- Cursor cursor = context .getContentResolver ().query (Uri .parse (uri ), proj , null , null , null );
227+ String [] proj = {MediaStore .Images .Media .DATA };
228+ Cursor cursor = context .getContentResolver ().query (Uri .parse (uri ), proj , null , null , null );
232229 int column_index = cursor .getColumnIndexOrThrow (MediaStore .Images .Media .DATA );
233230 cursor .moveToFirst ();
234231 String path = cursor .getString (column_index );
@@ -282,9 +279,9 @@ public Map<String, Object> getConstants() {
282279
283280 File externalStorageDirectory = Environment .getExternalStorageDirectory ();
284281 if (externalStorageDirectory != null ) {
285- constants .put (ExternalStorageDirectoryPath , externalStorageDirectory .getAbsolutePath ());
282+ constants .put (ExternalStorageDirectoryPath , externalStorageDirectory .getAbsolutePath ());
286283 } else {
287- constants .put (ExternalStorageDirectoryPath , null );
284+ constants .put (ExternalStorageDirectoryPath , null );
288285 }
289286
290287 File externalDirectory = this .getReactApplicationContext ().getExternalFilesDir (null );
0 commit comments