4
4
import android .os .StatFs ;
5
5
import android .content .Context ;
6
6
import android .util .Log ;
7
+
7
8
import java .util .Map ;
8
9
import java .util .HashMap ;
9
10
import java .io .File ;
@@ -79,27 +80,27 @@ public String getName() {
79
80
public void downloadUrl (final String javascriptStorageBucket ,
80
81
final String path ,
81
82
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 >() {
103
104
@ Override
104
105
public void onSuccess (final StorageMetadata storageMetadata ) {
105
106
Log .d (TAG , "getMetadata success " + storageMetadata );
@@ -118,27 +119,27 @@ public void onSuccess(final StorageMetadata storageMetadata) {
118
119
res .putMap ("metadata" , metadata );
119
120
callback .invoke (null , res );
120
121
}
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
+ });
128
129
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 ());
134
135
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 ());
138
139
139
- callback .invoke (err );
140
- }
141
- });
140
+ callback .invoke (err );
141
+ }
142
+ });
142
143
}
143
144
144
145
// STORAGE
@@ -149,17 +150,21 @@ public void uploadFile(final String urlStr, final String name, final String file
149
150
StorageReference storageRef = storage .getReferenceFromUrl (urlStr );
150
151
StorageReference fileRef = storageRef .child (name );
151
152
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
+
153
155
try {
154
- // InputStream stream = new FileInputStream(new File(filepath));
155
156
Uri file = Uri .fromFile (new File (filepath ));
156
157
157
158
StorageMetadata .Builder metadataBuilder = new StorageMetadata .Builder ();
158
159
Map <String , Object > m = FirestackUtils .recursivelyDeconstructReadableMap (metadata );
159
160
161
+ for (Map .Entry <String , Object > entry : m .entrySet ()) {
162
+ metadataBuilder .setCustomMetadata (entry .getKey (), entry .getValue ().toString ());
163
+ }
164
+
160
165
StorageMetadata md = metadataBuilder .build ();
161
166
UploadTask uploadTask = fileRef .putFile (file , md );
162
- // UploadTask uploadTask = fileRef.putStream(stream, md);
167
+ // uploadTask uploadTask = fileRef.putStream(stream, md);
163
168
164
169
// Register observers to listen for when the download is done or if it fails
165
170
uploadTask .addOnFailureListener (new OnFailureListener () {
@@ -179,33 +184,26 @@ public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
179
184
Log .d (TAG , "Successfully uploaded file " + taskSnapshot );
180
185
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
181
186
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
-
189
187
callback .invoke (null , resp );
190
188
}
191
189
})
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 >() {
209
207
@ Override
210
208
public void onPaused (UploadTask .TaskSnapshot taskSnapshot ) {
211
209
System .out .println ("Upload is paused" );
@@ -217,8 +215,7 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
217
215
FirestackUtils .sendEvent (mReactContext , "upload_paused" , data );
218
216
}
219
217
});
220
- }
221
- catch (Exception ex ) {
218
+ } catch (Exception ex ) {
222
219
callback .invoke (makeErrorPayload (2 , ex ));
223
220
}
224
221
}
@@ -227,8 +224,8 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
227
224
public void getRealPathFromURI (final String uri , final Callback callback ) {
228
225
try {
229
226
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 );
232
229
int column_index = cursor .getColumnIndexOrThrow (MediaStore .Images .Media .DATA );
233
230
cursor .moveToFirst ();
234
231
String path = cursor .getString (column_index );
@@ -282,9 +279,9 @@ public Map<String, Object> getConstants() {
282
279
283
280
File externalStorageDirectory = Environment .getExternalStorageDirectory ();
284
281
if (externalStorageDirectory != null ) {
285
- constants .put (ExternalStorageDirectoryPath , externalStorageDirectory .getAbsolutePath ());
282
+ constants .put (ExternalStorageDirectoryPath , externalStorageDirectory .getAbsolutePath ());
286
283
} else {
287
- constants .put (ExternalStorageDirectoryPath , null );
284
+ constants .put (ExternalStorageDirectoryPath , null );
288
285
}
289
286
290
287
File externalDirectory = this .getReactApplicationContext ().getExternalFilesDir (null );
0 commit comments