FIRStorageGetMetadataTask.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebaseStorage/Sources/FIRStorageGetMetadataTask.h"
  15. #import "FirebaseStorage/Sources/Public/FirebaseStorage/FIRStorageConstants.h"
  16. #import "FirebaseStorage/Sources/FIRStorageMetadata_Private.h"
  17. #import "FirebaseStorage/Sources/FIRStorageTask_Private.h"
  18. #import "FirebaseStorage/Sources/FIRStorageUtils.h"
  19. @implementation FIRStorageGetMetadataTask {
  20. @private
  21. FIRStorageVoidMetadataError _completion;
  22. }
  23. @synthesize fetcher = _fetcher;
  24. @synthesize fetcherCompletion = _fetcherCompletion;
  25. - (instancetype)initWithReference:(FIRStorageReference *)reference
  26. fetcherService:(GTMSessionFetcherService *)service
  27. dispatchQueue:(dispatch_queue_t)queue
  28. completion:(FIRStorageVoidMetadataError)completion {
  29. self = [super initWithReference:reference fetcherService:service dispatchQueue:queue];
  30. if (self) {
  31. _completion = [completion copy];
  32. }
  33. return self;
  34. }
  35. - (void)dealloc {
  36. [_fetcher stopFetching];
  37. }
  38. - (void)enqueue {
  39. __weak FIRStorageGetMetadataTask *weakSelf = self;
  40. [self dispatchAsync:^() {
  41. FIRStorageGetMetadataTask *strongSelf = weakSelf;
  42. if (!strongSelf) {
  43. return;
  44. }
  45. NSMutableURLRequest *request = [strongSelf.baseRequest mutableCopy];
  46. request.HTTPMethod = @"GET";
  47. request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime;
  48. FIRStorageVoidMetadataError callback = strongSelf->_completion;
  49. strongSelf->_completion = nil;
  50. GTMSessionFetcher *fetcher = [strongSelf.fetcherService fetcherWithRequest:request];
  51. strongSelf->_fetcher = fetcher;
  52. fetcher.comment = @"GetMetadataTask";
  53. #pragma clang diagnostic push
  54. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  55. strongSelf->_fetcherCompletion = ^(NSData *data, NSError *error) {
  56. FIRStorageMetadata *metadata;
  57. if (error) {
  58. if (!self.error) {
  59. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  60. }
  61. } else {
  62. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  63. if (responseDictionary != nil) {
  64. metadata = [[FIRStorageMetadata alloc] initWithDictionary:responseDictionary];
  65. [metadata setType:FIRStorageMetadataTypeFile];
  66. } else {
  67. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  68. }
  69. }
  70. if (callback) {
  71. callback(metadata, self.error);
  72. }
  73. self->_fetcherCompletion = nil;
  74. };
  75. #pragma clang diagnostic pop
  76. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  77. weakSelf.fetcherCompletion(data, error);
  78. }];
  79. }];
  80. }
  81. @end