@@ -52,15 +52,15 @@ @implementation FirestackStorage
52
52
[err setValue: @" Call setStorageUrl() first" forKey: @" description" ];
53
53
return callback (@[err]);
54
54
}
55
-
55
+
56
56
if ([path hasPrefix: @" assets-library://" ]) {
57
57
NSURL *localFile = [[NSURL alloc ] initWithString: path];
58
58
PHFetchResult* assets = [PHAsset fetchAssetsWithALAssetURLs: @[localFile] options: nil ];
59
59
PHAsset *asset = [assets firstObject ];
60
60
[asset requestContentEditingInputWithOptions: nil
61
61
completionHandler: ^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
62
62
NSURL *imageFile = contentEditingInput.fullSizeImageURL ;
63
-
63
+
64
64
[self performUpload: urlStr
65
65
name: name
66
66
file: imageFile
@@ -70,14 +70,14 @@ @implementation FirestackStorage
70
70
} else {
71
71
NSURL *localFile = [NSURL fileURLWithPath: path];
72
72
FIRStorageMetadata *firmetadata = [[FIRStorageMetadata alloc ] initWithDictionary: metadata];
73
-
73
+
74
74
[self performUpload: urlStr
75
75
name: name
76
76
file: localFile
77
77
metadata: firmetadata
78
78
callback: callback];
79
79
}
80
-
80
+
81
81
}
82
82
83
83
- (void ) performUpload : (NSString *) urlStr
@@ -88,10 +88,10 @@ - (void) performUpload:(NSString *) urlStr
88
88
{
89
89
FIRStorageReference *storageRef = [[FIRStorage storage ] referenceForURL: urlStr];
90
90
FIRStorageReference *uploadRef = [storageRef child: name];
91
-
91
+
92
92
FIRStorageUploadTask *uploadTask = [uploadRef putFile: imageFile
93
93
metadata: firmetadata];
94
-
94
+
95
95
// Listen for state changes, errors, and completion of the upload.
96
96
[uploadTask observeStatus: FIRStorageTaskStatusResume handler: ^(FIRStorageTaskSnapshot *snapshot) {
97
97
// Upload resumed, also fires when the upload starts
@@ -100,7 +100,7 @@ - (void) performUpload:(NSString *) urlStr
100
100
@" ref" : snapshot.reference .bucket
101
101
}];
102
102
}];
103
-
103
+
104
104
[uploadTask observeStatus: FIRStorageTaskStatusPause handler: ^(FIRStorageTaskSnapshot *snapshot) {
105
105
// Upload paused
106
106
[self sendJSEvent: STORAGE_UPLOAD_PAUSED props: @{
@@ -116,17 +116,17 @@ - (void) performUpload:(NSString *) urlStr
116
116
} else {
117
117
percentComplete = 100.0 * (snapshot.progress .completedUnitCount ) / (snapshot.progress .totalUnitCount );
118
118
}
119
-
119
+
120
120
[self sendJSEvent: STORAGE_UPLOAD_PROGRESS props: @{
121
121
@" eventName" : STORAGE_UPLOAD_PROGRESS,
122
122
@" progress" : @(percentComplete)
123
123
}];
124
-
124
+
125
125
}];
126
-
126
+
127
127
[uploadTask observeStatus: FIRStorageTaskStatusSuccess handler: ^(FIRStorageTaskSnapshot *snapshot) {
128
128
[uploadTask removeAllObservers ];
129
-
129
+
130
130
// Upload completed successfully
131
131
FIRStorageReference *ref = snapshot.reference ;
132
132
NSDictionary *props = @{
@@ -135,14 +135,14 @@ - (void) performUpload:(NSString *) urlStr
135
135
@" name" : ref.name ,
136
136
@" metadata" : [snapshot.metadata dictionaryRepresentation ]
137
137
};
138
-
138
+
139
139
callback (@[[NSNull null ], props]);
140
140
}];
141
-
141
+
142
142
[uploadTask observeStatus: FIRStorageTaskStatusFailure handler: ^(FIRStorageTaskSnapshot *snapshot) {
143
143
if (snapshot.error != nil ) {
144
144
NSDictionary *errProps = [[NSMutableDictionary alloc ] init ];
145
-
145
+
146
146
switch (snapshot.error .code ) {
147
147
case FIRStorageErrorCodeObjectNotFound:
148
148
// File doesn't exist
@@ -161,17 +161,112 @@ - (void) performUpload:(NSString *) urlStr
161
161
[errProps setValue: @" Unknown error" forKey: @" description" ];
162
162
break ;
163
163
}
164
-
164
+
165
+ callback (@[errProps]);
166
+ }}];
167
+ }
168
+
169
+ RCT_EXPORT_METHOD (downloadFile: (NSString *) urlStr
170
+ path:(NSString *) path
171
+ localFile:(NSString *) file
172
+ callback:(RCTResponseSenderBlock) callback)
173
+ {
174
+ if (urlStr == nil ) {
175
+ NSError *err = [[NSError alloc ] init ];
176
+ [err setValue: @" Storage configuration error" forKey: @" name" ];
177
+ [err setValue: @" Call setStorageUrl() first" forKey: @" description" ];
178
+ return callback (@[err]);
179
+ }
180
+
181
+ FIRStorageReference *storageRef = [[FIRStorage storage ] referenceForURL: urlStr];
182
+ FIRStorageReference *fileRef = [storageRef child: path];
183
+
184
+ NSURL *localFile = [NSURL fileURLWithPath: file];
185
+
186
+ FIRStorageDownloadTask *downloadTask = [fileRef writeToFile: localFile];
187
+ // Listen for state changes, errors, and completion of the download.
188
+ [downloadTask observeStatus: FIRStorageTaskStatusResume handler: ^(FIRStorageTaskSnapshot *snapshot) {
189
+ // Upload resumed, also fires when the upload starts
190
+ [self sendJSEvent: STORAGE_DOWNLOAD_RESUMED props: @{
191
+ @" eventName" : STORAGE_DOWNLOAD_RESUMED,
192
+ @" ref" : snapshot.reference .bucket
193
+ }];
194
+ }];
195
+
196
+ [downloadTask observeStatus: FIRStorageTaskStatusPause handler: ^(FIRStorageTaskSnapshot *snapshot) {
197
+ // Upload paused
198
+ [self sendJSEvent: STORAGE_DOWNLOAD_PAUSED props: @{
199
+ @" eventName" : STORAGE_DOWNLOAD_PAUSED,
200
+ @" ref" : snapshot.reference .bucket
201
+ }];
202
+ }];
203
+ [downloadTask observeStatus: FIRStorageTaskStatusProgress handler: ^(FIRStorageTaskSnapshot *snapshot) {
204
+ // Upload reported progress
205
+ float percentComplete;
206
+ if (snapshot.progress .totalUnitCount == 0 ) {
207
+ percentComplete = 0.0 ;
208
+ } else {
209
+ percentComplete = 100.0 * (snapshot.progress .completedUnitCount ) / (snapshot.progress .totalUnitCount );
210
+ }
211
+
212
+ [self sendJSEvent: STORAGE_DOWNLOAD_PROGRESS props: @{
213
+ @" eventName" : STORAGE_DOWNLOAD_PROGRESS,
214
+ @" progress" : @(percentComplete)
215
+ }];
216
+
217
+ }];
218
+
219
+ [downloadTask observeStatus: FIRStorageTaskStatusSuccess handler: ^(FIRStorageTaskSnapshot *snapshot) {
220
+ [downloadTask removeAllObservers ];
221
+
222
+ // Upload completed successfully
223
+ FIRStorageReference *ref = snapshot.reference ;
224
+ NSDictionary *props = @{
225
+ @" fullPath" : ref.fullPath ,
226
+ @" bucket" : ref.bucket ,
227
+ @" name" : ref.name
228
+ };
229
+
230
+ callback (@[[NSNull null ], props]);
231
+ }];
232
+
233
+ [downloadTask observeStatus: FIRStorageTaskStatusFailure handler: ^(FIRStorageTaskSnapshot *snapshot) {
234
+ if (snapshot.error != nil ) {
235
+ NSDictionary *errProps = [[NSMutableDictionary alloc ] init ];
236
+
237
+ switch (snapshot.error .code ) {
238
+ case FIRStorageErrorCodeObjectNotFound:
239
+ // File doesn't exist
240
+ [errProps setValue: @" File does not exist" forKey: @" description" ];
241
+ break ;
242
+ case FIRStorageErrorCodeUnauthorized:
243
+ // User doesn't have permission to access file
244
+ [errProps setValue: @" You do not have permissions" forKey: @" description" ];
245
+ break ;
246
+ case FIRStorageErrorCodeCancelled:
247
+ // User canceled the upload
248
+ [errProps setValue: @" Download canceled" forKey: @" description" ];
249
+ break ;
250
+ case FIRStorageErrorCodeUnknown:
251
+ // Unknown error occurred, inspect the server response
252
+ [errProps setValue: @" Unknown error" forKey: @" description" ];
253
+ break ;
254
+ }
255
+
165
256
callback (@[errProps]);
166
257
}}];
167
258
}
168
259
260
+
169
261
// Not sure how to get away from this... yet
170
262
- (NSArray <NSString *> *)supportedEvents {
171
263
return @[
172
264
STORAGE_UPLOAD_PAUSED,
173
265
STORAGE_UPLOAD_RESUMED,
174
- STORAGE_UPLOAD_PROGRESS
266
+ STORAGE_UPLOAD_PROGRESS,
267
+ STORAGE_DOWNLOAD_PAUSED,
268
+ STORAGE_DOWNLOAD_RESUMED,
269
+ STORAGE_DOWNLOAD_PROGRESS
175
270
];
176
271
}
177
272
0 commit comments