-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathstorage_reference_ios.mm
More file actions
465 lines (426 loc) · 18.5 KB
/
storage_reference_ios.mm
File metadata and controls
465 lines (426 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright 2016 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "storage/src/ios/storage_reference_ios.h"
#import "FirebaseStorage-Swift.h"
#include "app/src/util_ios.h"
#include "storage/src/common/common_internal.h"
#include "storage/src/include/firebase/storage.h"
#include "storage/src/include/firebase/storage/common.h"
#include "storage/src/include/firebase/storage/controller.h"
#include "storage/src/include/firebase/storage/metadata.h"
#include "storage/src/ios/controller_ios.h"
#include "storage/src/ios/listener_ios.h"
#include "storage/src/ios/metadata_ios.h"
#include "storage/src/ios/storage_ios.h"
#include "storage/src/ios/util_ios.h"
namespace firebase {
namespace storage {
namespace internal {
// Should reads and writes share thier futures?
enum StorageReferenceFn {
kStorageReferenceFnDelete = 0,
kStorageReferenceFnGetBytes,
kStorageReferenceFnGetFile,
kStorageReferenceFnGetDownloadUrl,
kStorageReferenceFnGetMetadata,
kStorageReferenceFnUpdateMetadata,
kStorageReferenceFnPutBytes,
kStorageReferenceFnPutFile,
kStorageReferenceFnCount,
};
StorageReferenceInternal::StorageReferenceInternal(StorageInternal* storage,
std::unique_ptr<FIRStorageReferencePointer> impl)
: storage_(storage), impl_(std::move(impl)) {
storage_->future_manager().AllocFutureApi(this, kStorageReferenceFnCount);
}
StorageReferenceInternal::~StorageReferenceInternal() {
storage_->future_manager().ReleaseFutureApi(this);
}
StorageReferenceInternal::StorageReferenceInternal(const StorageReferenceInternal& other)
: storage_(other.storage_) {
impl_.reset(new FIRStorageReferencePointer(*other.impl_));
storage_->future_manager().AllocFutureApi(this, kStorageReferenceFnCount);
}
StorageReferenceInternal& StorageReferenceInternal::operator=(
const StorageReferenceInternal& other) {
impl_.reset(new FIRStorageReferencePointer(*other.impl_));
return *this;
}
#if defined(FIREBASE_USE_MOVE_OPERATORS) || defined(DOXYGEN)
StorageReferenceInternal::StorageReferenceInternal(StorageReferenceInternal&& other)
: storage_(other.storage_), impl_(std::move(other.impl_)) {
other.storage_ = nullptr;
other.impl_ = std::make_unique<FIRStorageReferencePointer>(nil);
storage_->future_manager().MoveFutureApi(&other, this);
}
StorageReferenceInternal& StorageReferenceInternal::operator=(
StorageReferenceInternal&& other) {
storage_ = other.storage_;
impl_ = std::move(other.impl_);
other.storage_ = nullptr;
other.impl_ = std::make_unique<FIRStorageReferencePointer>(nil);
storage_->future_manager().MoveFutureApi(&other, this);
return *this;
}
#endif // defined(FIREBASE_USE_MOVE_OPERATORS) || defined(DOXYGEN)
Storage* StorageReferenceInternal::storage() {
return Storage::GetInstance(storage_->app());
}
StorageReferenceInternal* StorageReferenceInternal::Child(const char* path) const {
return new StorageReferenceInternal(
storage_, std::make_unique<FIRStorageReferencePointer>([impl() child:@(path)]));
}
Future<void> StorageReferenceInternal::Delete() {
ReferenceCountedFutureImpl* future_impl = future();
SafeFutureHandle<void> handle = future_impl->SafeAlloc<void>(kStorageReferenceFnDelete);
FIRStorageVoidError completion = ^(NSError *error) {
Error error_code = NSErrorToErrorCode(error);
const char* error_string = GetErrorMessage(error_code);
future_impl->Complete(handle, error_code, error_string);
};
[impl() deleteWithCompletion:completion];
return DeleteLastResult();
}
Future<void> StorageReferenceInternal::DeleteLastResult() {
return static_cast<const Future<void>&>(future()->LastResult(kStorageReferenceFnDelete));
}
std::string StorageReferenceInternal::bucket() { return std::string(impl().bucket.UTF8String); }
std::string StorageReferenceInternal::full_path() {
return std::string("/") + std::string(impl().fullPath.UTF8String);
}
Future<size_t> StorageReferenceInternal::GetFile(const char* path, Listener* listener,
Controller* controller_out) {
ReferenceCountedFutureImpl* future_impl = future();
SafeFutureHandle<size_t> handle = future_impl->SafeAlloc<size_t>(kStorageReferenceFnGetFile);
ControllerInternal* local_controller = new ControllerInternal();
FIRStorageVoidURLError completion = ^(NSURL *url, NSError *error) {
Error error_code = NSErrorToErrorCode(error);
const char* error_string = GetErrorMessage(error_code);
if (error == nil) {
size_t bytes_transferred = static_cast<size_t>(local_controller->bytes_transferred());
future_impl->CompleteWithResult(handle, error_code, error_string, bytes_transferred);
} else {
future_impl->Complete(handle, error_code, error_string);
}
{
MutexLock mutex(controller_init_mutex_);
delete local_controller;
}
};
if (controller_out) {
controller_out->internal_->set_pending_valid(true);
}
// Cache a copy of the impl and storage, in case this is destroyed before the thread runs.
FIRStorageReference* my_impl = impl();
StorageInternal* storage = storage_;
NSString* path_string = @(path);
if (path_string.length == 0) {
future_impl->Complete(handle, kErrorUnknown, "Path cannot be empty.");
return GetFileLastResult();
}
NSURL* local_file_url = nil;
if ([path_string hasPrefix:@"file://"]) {
// If it starts with the prefix, load it assuming a URL string.
local_file_url = [NSURL URLWithString:path_string];
} else {
// Otherwise, assume it is a file path.
local_file_url = [NSURL fileURLWithPath:path_string];
}
// If we still failed to convert the path, error out.
if (local_file_url == nil) {
future_impl->Complete(handle, kErrorUnknown,
"Unable to convert provided path to valid URL");
return GetFileLastResult();
}
util::DispatchAsyncSafeMainQueue(^() {
FIRStorageDownloadTask *download_task;
{
MutexLock mutex(controller_init_mutex_);
download_task = [my_impl writeToFile:local_file_url completion:completion];
local_controller->AssignTask(storage, download_task);
}
if (listener) {
listener->impl_->AttachTask(storage, download_task);
}
if (controller_out) {
controller_out->internal_->set_pending_valid(false);
controller_out->internal_->AssignTask(storage, download_task);
}
});
return GetFileLastResult();
}
Future<size_t> StorageReferenceInternal::GetFileLastResult() {
return static_cast<const Future<size_t>&>(future()->LastResult(kStorageReferenceFnGetFile));
}
Future<size_t> StorageReferenceInternal::GetBytes(void* buffer, size_t buffer_size,
Listener* listener, Controller* controller_out) {
ReferenceCountedFutureImpl* future_impl = future();
SafeFutureHandle<size_t> handle = future_impl->SafeAlloc<size_t>(kStorageReferenceFnGetBytes);
__block BOOL completed = NO;
FIRStorageVoidDataError completion = ^(NSData* _Nullable data, NSError* _Nullable error) {
if (completed) {
return;
}
completed = YES;
Error error_code = NSErrorToErrorCode(error);
const char* error_string = GetErrorMessage(error_code);
if (data != nil) {
assert(data.length <= buffer_size);
memcpy(buffer, data.bytes, data.length);
future_impl->CompleteWithResult(handle, error_code, error_string,
static_cast<size_t>(data.length));
} else {
future_impl->Complete(handle, error_code, error_string);
}
if (listener) listener->impl_->DetachTask();
};
if (controller_out) {
controller_out->internal_->set_pending_valid(true);
}
// Cache a copy of the impl and storage, in case this is destroyed before the thread runs.
FIRStorageReference* my_impl = impl();
StorageInternal* storage = storage_;
util::DispatchAsyncSafeMainQueue(^() {
FIRStorageDownloadTask *download_task = [my_impl dataWithMaxSize:buffer_size
completion:completion];
if (listener) listener->impl_->AttachTask(storage, download_task);
if (controller_out) {
controller_out->internal_->AssignTask(storage, download_task);
controller_out->internal_->set_pending_valid(false);
}
});
return GetBytesLastResult();
}
Future<size_t> StorageReferenceInternal::GetBytesLastResult() {
return static_cast<const Future<size_t>&>(future()->LastResult(kStorageReferenceFnGetBytes));
}
Future<std::string> StorageReferenceInternal::GetDownloadUrl() {
ReferenceCountedFutureImpl* future_impl = future();
SafeFutureHandle<std::string> handle =
future_impl->SafeAlloc<std::string>(kStorageReferenceFnGetDownloadUrl);
__block BOOL completed = NO;
FIRStorageVoidURLError completion = ^(NSURL *url, NSError *error) {
if (completed) return;
completed = YES;
Error error_code = NSErrorToErrorCode(error);
const char* error_string = GetErrorMessage(error_code);
if (url != nil) {
future_impl->CompleteWithResult(handle, error_code, error_string,
std::string(url.absoluteString.UTF8String));
} else {
future_impl->Complete(handle, error_code, error_string);
}
};
[impl() downloadURLWithCompletion:completion];
return GetDownloadUrlLastResult();
}
Future<std::string> StorageReferenceInternal::GetDownloadUrlLastResult() {
return static_cast<const Future<std::string>&>(
future()->LastResult(kStorageReferenceFnGetDownloadUrl));
}
Future<Metadata> StorageReferenceInternal::GetMetadata() {
ReferenceCountedFutureImpl* future_impl = future();
SafeFutureHandle<Metadata> handle =
future_impl->SafeAlloc<Metadata>(kStorageReferenceFnGetMetadata);
StorageInternal* storage = storage_;
__block BOOL completed = NO;
FIRStorageVoidMetadataError completion = ^(FIRStorageMetadata *metadata, NSError *error) {
if (completed) return;
completed = YES;
Error error_code = NSErrorToErrorCode(error);
const char* error_string = GetErrorMessage(error_code);
if (metadata != nil) {
future_impl->CompleteWithResult(
handle, error_code, error_string,
Metadata(new MetadataInternal(storage, std::make_unique<FIRStorageMetadataPointer>(metadata))));
} else {
future_impl->CompleteWithResult(handle, error_code, error_string, Metadata(nullptr));
}
};
[impl() metadataWithCompletion:completion];
return GetMetadataLastResult();
}
Future<Metadata> StorageReferenceInternal::GetMetadataLastResult() {
return static_cast<const Future<Metadata>&>(future()->LastResult(kStorageReferenceFnGetMetadata));
}
Future<Metadata> StorageReferenceInternal::UpdateMetadata(const Metadata* metadata) {
ReferenceCountedFutureImpl* future_impl = future();
SafeFutureHandle<Metadata> handle =
future_impl->SafeAlloc<Metadata>(kStorageReferenceFnUpdateMetadata);
StorageInternal* storage = storage_;
__block BOOL completed = NO;
FIRStorageVoidMetadataError completion = ^(FIRStorageMetadata *metadata_impl, NSError *error) {
if (completed) return;
completed = YES;
Error error_code = NSErrorToErrorCode(error);
const char* error_string = GetErrorMessage(error_code);
if (metadata_impl != nil) {
future_impl->CompleteWithResult(
handle, error_code, error_string,
Metadata(
new MetadataInternal(storage, std::make_unique<FIRStorageMetadataPointer>(metadata_impl))));
} else {
future_impl->CompleteWithResult(handle, error_code, error_string,
Metadata(nullptr));
}
};
metadata->internal_->CommitCustomMetadata();
[impl() updateMetadata:metadata->internal_->impl() completion:completion];
return UpdateMetadataLastResult();
}
Future<Metadata> StorageReferenceInternal::UpdateMetadataLastResult() {
return static_cast<const Future<Metadata>&>(
future()->LastResult(kStorageReferenceFnUpdateMetadata));
}
std::string StorageReferenceInternal::name() { return std::string(impl().name.UTF8String); }
StorageReferenceInternal* StorageReferenceInternal::GetParent() {
return new StorageReferenceInternal(storage_,
std::make_unique<FIRStorageReferencePointer>(impl().parent));
}
Future<Metadata> StorageReferenceInternal::PutBytes(
const void* buffer, size_t buffer_size, Listener* listener, Controller* controller_out) {
return PutBytes(buffer, buffer_size, nullptr, listener, controller_out);
}
Future<Metadata> StorageReferenceInternal::PutBytes(
const void* buffer, size_t buffer_size, const Metadata* metadata, Listener* listener,
Controller* controller_out) {
ReferenceCountedFutureImpl* future_impl = future();
SafeFutureHandle<Metadata> handle = future_impl->SafeAlloc<Metadata>(kStorageReferenceFnPutBytes);
StorageInternal* storage = storage_;
__block BOOL completed = NO;
FIRStorageVoidMetadataError completion =
^(FIRStorageMetadata *resultant_metadata, NSError *error) {
if (completed) return;
completed = YES;
Error error_code = NSErrorToErrorCode(error);
const char* error_string = GetErrorMessage(error_code);
if (error == nil) {
future_impl->CompleteWithResult(
handle, error_code, error_string,
Metadata(new MetadataInternal(
storage, std::make_unique<FIRStorageMetadataPointer>(resultant_metadata))));
} else {
future_impl->CompleteWithResult(handle, error_code, error_string, Metadata(nullptr));
}
if (listener) listener->impl_->DetachTask();
};
FIRStorageMetadata *metadata_impl = nil;
Metadata local_metadata;
if (metadata) local_metadata = *metadata;
metadata = &local_metadata;
internal::MetadataSetDefaults(&local_metadata);
if (metadata->is_valid()) {
metadata->internal_->CommitCustomMetadata();
metadata_impl = metadata->internal_->impl();
}
if (controller_out) {
controller_out->internal_->set_pending_valid(true);
}
// Cache a copy of the impl, in case this is destroyed before the thread runs.
FIRStorageReference* my_impl = impl();
util::DispatchAsyncSafeMainQueue(^() {
// NOTE: We can allocate NSData without copying the input buffer as we know that
// FIRStorageUploadTask does not mutate the NSData object.
FIRStorageUploadTask* upload_task =
[my_impl putData:[NSData dataWithBytesNoCopy:const_cast<void*>(buffer)
length:buffer_size
freeWhenDone:NO]
metadata:metadata_impl
completion:completion];
if (listener) {
listener->impl_->AttachTask(storage_, upload_task);
}
if (controller_out) {
controller_out->internal_->set_pending_valid(false);
controller_out->internal_->AssignTask(storage_, upload_task);
}
});
return PutBytesLastResult();
}
Future<Metadata> StorageReferenceInternal::PutBytesLastResult() {
return static_cast<const Future<Metadata>&>(future()->LastResult(kStorageReferenceFnPutBytes));
}
Future<Metadata> StorageReferenceInternal::PutFile(
const char* path, Listener* listener, Controller* controller_out) {
return PutFile(path, nullptr, listener, controller_out);
}
Future<Metadata> StorageReferenceInternal::PutFile(
const char* path, const Metadata* metadata, Listener* listener, Controller* controller_out) {
ReferenceCountedFutureImpl* future_impl = future();
SafeFutureHandle<Metadata> handle = future_impl->SafeAlloc<Metadata>(kStorageReferenceFnPutFile);
StorageInternal* storage = storage_;
__block BOOL completed = NO;
FIRStorageVoidMetadataError completion =
^(FIRStorageMetadata *resultant_metadata, NSError *error) {
if (completed) return;
completed = YES;
Error error_code = NSErrorToErrorCode(error);
const char* error_string = GetErrorMessage(error_code);
if (error == nil) {
future_impl->CompleteWithResult(
handle, error_code, error_string,
Metadata(new MetadataInternal(
storage, std::make_unique<FIRStorageMetadataPointer>(resultant_metadata))));
} else {
future_impl->CompleteWithResult(handle, error_code, error_string, Metadata(nullptr));
}
if (listener) listener->impl_->DetachTask();
};
FIRStorageMetadata *metadata_impl = nil;
Metadata local_metadata;
if (metadata) local_metadata = *metadata;
metadata = &local_metadata;
internal::MetadataSetDefaults(&local_metadata);
if (metadata->is_valid()) {
metadata->internal_->CommitCustomMetadata();
metadata_impl = metadata->internal_->impl();
}
NSURL* local_file_url = [NSURL URLWithString:@(path)];
if (controller_out) {
controller_out->internal_->set_pending_valid(true);
}
// Cache a copy of the impl, in case this is destroyed before the thread runs.
FIRStorageReference* my_impl = impl();
util::DispatchAsyncSafeMainQueue(^() {
// Due to b/73017865 the iOS SDK can end up in an infinite loop if the source file doesn't
// exist so check for file existence here and abort if it's missing.
NSError* file_url_exists_error;
if (![local_file_url checkResourceIsReachableAndReturnError:&file_url_exists_error]) {
Error error_code = kErrorObjectNotFound;
const char* error_string = GetErrorMessage(error_code);
future_impl->CompleteWithResult(handle, error_code, error_string, Metadata(nullptr));
return;
}
FIRStorageUploadTask* upload_task = [my_impl putFile:local_file_url
metadata:metadata_impl
completion:completion];
if (listener) {
listener->impl_->AttachTask(storage, upload_task);
}
if (controller_out) {
controller_out->internal_->set_pending_valid(false);
controller_out->internal_->AssignTask(storage, upload_task);
}
});
return PutFileLastResult();
}
Future<Metadata> StorageReferenceInternal::PutFileLastResult() {
return static_cast<const Future<Metadata>&>(future()->LastResult(kStorageReferenceFnPutFile));
}
ReferenceCountedFutureImpl* StorageReferenceInternal::future() {
return storage_->future_manager().GetFutureApi(this);
}
} // namespace internal
} // namespace storage
} // namespace firebase