Skip to content

Commit 08eab35

Browse files
committed
Simplify storage modules to use the bucket specified when initialising Firebase
1 parent 6f44df2 commit 08eab35

File tree

3 files changed

+20
-64
lines changed

3 files changed

+20
-64
lines changed

android/src/main/java/io/fullstack/firestack/storage/FirestackStorage.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ public boolean isExternalStorageWritable() {
8585
}
8686

8787
@ReactMethod
88-
public void downloadFile(final String urlStr,
89-
final String fbPath,
88+
public void downloadFile(final String remotePath,
9089
final String localFile,
9190
final Callback callback) {
92-
Log.d(TAG, "downloadFile: " + urlStr + ", " + localFile);
9391
if (!isExternalStorageWritable()) {
9492
Log.w(TAG, "downloadFile failed: external storage not writable");
9593
WritableMap error = Arguments.createMap();
@@ -99,13 +97,9 @@ public void downloadFile(final String urlStr,
9997
callback.invoke(error);
10098
return;
10199
}
102-
FirebaseStorage storage = FirebaseStorage.getInstance();
103-
String storageBucket = storage.getApp().getOptions().getStorageBucket();
104-
String storageUrl = "gs://" + storageBucket;
105-
Log.d(TAG, "Storage url " + storageUrl + fbPath);
100+
Log.d(TAG, "downloadFile from remote path: " + remotePath);
106101

107-
StorageReference storageRef = storage.getReferenceFromUrl(storageUrl);
108-
StorageReference fileRef = storageRef.child(fbPath);
102+
StorageReference fileRef = FirebaseStorage.getInstance().getReference(remotePath);
109103

110104
fileRef.getStream(new StreamDownloadTask.StreamProcessor() {
111105
@Override
@@ -182,15 +176,10 @@ public void onFailure(@NonNull Exception exception) {
182176
}
183177

184178
@ReactMethod
185-
public void downloadUrl(final String javascriptStorageBucket,
186-
final String path,
179+
public void downloadUrl(final String remotePath,
187180
final Callback callback) {
188-
FirebaseStorage storage = FirebaseStorage.getInstance();
189-
String storageBucket = storage.getApp().getOptions().getStorageBucket();
190-
String storageUrl = "gs://" + storageBucket;
191-
Log.d(TAG, "Storage url " + storageUrl + path);
192-
final StorageReference storageRef = storage.getReferenceFromUrl(storageUrl);
193-
final StorageReference fileRef = storageRef.child(path);
181+
Log.d(TAG, "Download url for remote path: " + remotePath);
182+
final StorageReference fileRef = FirebaseStorage.getInstance().getReference(remotePath);
194183

195184
Task<Uri> downloadTask = fileRef.getDownloadUrl();
196185
downloadTask
@@ -200,7 +189,7 @@ public void onSuccess(Uri uri) {
200189
final WritableMap res = Arguments.createMap();
201190

202191
res.putString("status", "success");
203-
res.putString("bucket", storageRef.getBucket());
192+
res.putString("bucket", FirebaseStorage.getInstance().getApp().getOptions().getStorageBucket());
204193
res.putString("fullPath", uri.toString());
205194
res.putString("path", uri.getPath());
206195
res.putString("url", uri.toString());
@@ -256,12 +245,10 @@ private WritableMap getMetadataAsMap(StorageMetadata storageMetadata) {
256245

257246
// STORAGE
258247
@ReactMethod
259-
public void uploadFile(final String urlStr, final String name, final String filepath, final ReadableMap metadata, final Callback callback) {
260-
FirebaseStorage storage = FirebaseStorage.getInstance();
261-
StorageReference storageRef = storage.getReferenceFromUrl(urlStr);
262-
StorageReference fileRef = storageRef.child(name);
248+
public void uploadFile(final String remotePath, final String filepath, final ReadableMap metadata, final Callback callback) {
249+
StorageReference fileRef = FirebaseStorage.getInstance().getReference(remotePath);
263250

264-
Log.i(TAG, "From file: " + filepath + " to " + urlStr + " with name " + name);
251+
Log.i(TAG, "Upload file: " + filepath + " to " + remotePath);
265252

266253
try {
267254
Uri file;

ios/Firestack/FirestackStorage.m

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,15 @@ - (dispatch_queue_t)methodQueue
2121
return dispatch_queue_create("io.fullstack.firestack.storage", DISPATCH_QUEUE_SERIAL);
2222
}
2323

24-
RCT_EXPORT_METHOD(downloadUrl: (NSString *) storageUrl
25-
path:(NSString *) path
24+
RCT_EXPORT_METHOD(downloadUrl: (NSString *) remotePath
2625
callback:(RCTResponseSenderBlock) callback)
2726
{
28-
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:storageUrl];
29-
FIRStorageReference *fileRef = [storageRef child:path];
27+
FIRStorageReference *fileRef = [[FIRStorage storage] referenceWithPath:remotePath];
3028
[fileRef downloadURLWithCompletion:^(NSURL * _Nullable URL, NSError * _Nullable error) {
3129
if (error != nil) {
3230
NSDictionary *evt = @{
3331
@"status": @"error",
34-
@"path": path,
32+
@"path": remotePath,
3533
@"msg": [error debugDescription]
3634
};
3735
callback(@[evt]);
@@ -46,21 +44,12 @@ - (dispatch_queue_t)methodQueue
4644
}];
4745
}
4846

49-
RCT_EXPORT_METHOD(uploadFile: (NSString *) urlStr
50-
name: (NSString *) name
47+
RCT_EXPORT_METHOD(uploadFile: (NSString *) remotePath
5148
path:(NSString *)path
5249
metadata:(NSDictionary *)metadata
5350
callback:(RCTResponseSenderBlock) callback)
5451
{
55-
if (urlStr == nil) {
56-
NSError *err = [[NSError alloc] init];
57-
[err setValue:@"Storage configuration error" forKey:@"name"];
58-
[err setValue:@"Call setStorageUrl() first" forKey:@"description"];
59-
return callback(@[err]);
60-
}
61-
62-
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:urlStr];
63-
FIRStorageReference *uploadRef = [storageRef child:name];
52+
FIRStorageReference *uploadRef = [[FIRStorage storage] referenceWithPath:remotePath];
6453
FIRStorageMetadata *firmetadata = [[FIRStorageMetadata alloc] initWithDictionary:metadata];
6554

6655
if ([path hasPrefix:@"assets-library://"]) {
@@ -164,21 +153,11 @@ - (void) addUploadObservers:(FIRStorageUploadTask *) uploadTask
164153
}}];
165154
}
166155

167-
RCT_EXPORT_METHOD(downloadFile: (NSString *) urlStr
168-
path:(NSString *) path
156+
RCT_EXPORT_METHOD(downloadFile: (NSString *) remotePath
169157
localFile:(NSString *) file
170158
callback:(RCTResponseSenderBlock) callback)
171159
{
172-
if (urlStr == nil) {
173-
NSError *err = [[NSError alloc] init];
174-
[err setValue:@"Storage configuration error" forKey:@"name"];
175-
[err setValue:@"Call setStorageUrl() first" forKey:@"description"];
176-
return callback(@[err]);
177-
}
178-
179-
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:urlStr];
180-
FIRStorageReference *fileRef = [storageRef child:path];
181-
160+
FIRStorageReference *fileRef = [[FIRStorage storage] referenceWithPath:remotePath];
182161
NSURL *localFile = [NSURL fileURLWithPath:file];
183162

184163
FIRStorageDownloadTask *downloadTask = [fileRef writeToFile:localFile];

lib/modules/storage.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class StorageRef extends ReferenceBase {
1717
downloadUrl(): Promise<Object> {
1818
const path = this.pathToString();
1919
this.log.debug('downloadUrl(', path, ')');
20-
return promisify('downloadUrl', FirestackStorage)(this.storage.storageUrl, path)
20+
return promisify('downloadUrl', FirestackStorage)(path)
2121
.catch(err => {
2222
this.log.error('Error downloading URL for ', path, '. Error: ', err);
2323
throw err;
@@ -39,7 +39,7 @@ class StorageRef extends ReferenceBase {
3939
this.storage._addListener('download_resumed', listener),
4040
];
4141

42-
return promisify('downloadFile', FirestackStorage)(this.storage.storageUrl, path, downloadPath)
42+
return promisify('downloadFile', FirestackStorage)(path, downloadPath)
4343
.then((res) => {
4444
this.log.debug('res --->', res);
4545
listeners.forEach(listener => listener.remove());
@@ -58,11 +58,6 @@ type StorageOptionsType = {
5858
export default class Storage extends Base {
5959
constructor(firestack: Object, options:StorageOptionsType={}) {
6060
super(firestack, options);
61-
62-
if (this.options.storageBucket) {
63-
this.setStorageUrl(this.options.storageBucket);
64-
}
65-
6661
this.refs = {};
6762
}
6863

@@ -92,7 +87,7 @@ export default class Storage extends Base {
9287
this._addListener('upload_progress', listener),
9388
];
9489

95-
return promisify('uploadFile', FirestackStorage)(this.storageUrl, name, _filePath, metadata)
90+
return promisify('uploadFile', FirestackStorage)(name, _filePath, metadata)
9691
.then((res) => {
9792
listeners.forEach(listener => listener.remove());
9893
return res;
@@ -112,11 +107,6 @@ export default class Storage extends Base {
112107
return listener;
113108
}
114109

115-
setStorageUrl(url: string): void {
116-
// return promisify('setStorageUrl', FirestackStorage)(url);
117-
this.storageUrl = `gs://${url}`;
118-
}
119-
120110
_pathKey(...path: Array<string>): string {
121111
return path.join('-');
122112
}

0 commit comments

Comments
 (0)