FIRStorageGetMetadataTask.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "FIRStorageGetMetadataTask.h"
  15. #import "FIRStorageConstants.h"
  16. #import "FIRStorageMetadata_Private.h"
  17. #import "FIRStorageTask_Private.h"
  18. #import "FIRStorageUtils.h"
  19. #import "FirebaseStorage.h"
  20. @implementation FIRStorageGetMetadataTask {
  21. @private
  22. FIRStorageVoidMetadataError _completion;
  23. }
  24. @synthesize fetcher = _fetcher;
  25. @synthesize fetcherCompletion = _fetcherCompletion;
  26. - (instancetype)initWithReference:(FIRStorageReference *)reference
  27. fetcherService:(GTMSessionFetcherService *)service
  28. dispatchQueue:(dispatch_queue_t)queue
  29. completion:(FIRStorageVoidMetadataError)completion {
  30. self = [super initWithReference:reference fetcherService:service dispatchQueue:queue];
  31. if (self) {
  32. _completion = [completion copy];
  33. }
  34. return self;
  35. }
  36. - (void)dealloc {
  37. [_fetcher stopFetching];
  38. }
  39. - (void)enqueue {
  40. __weak FIRStorageGetMetadataTask *weakSelf = self;
  41. [self dispatchAsync:^() {
  42. FIRStorageGetMetadataTask *strongSelf = weakSelf;
  43. if (!strongSelf) {
  44. return;
  45. }
  46. NSMutableURLRequest *request = [strongSelf.baseRequest mutableCopy];
  47. request.HTTPMethod = @"GET";
  48. request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime;
  49. FIRStorageVoidMetadataError callback = strongSelf->_completion;
  50. strongSelf->_completion = nil;
  51. GTMSessionFetcher *fetcher = [strongSelf.fetcherService fetcherWithRequest:request];
  52. strongSelf->_fetcher = fetcher;
  53. fetcher.comment = @"GetMetadataTask";
  54. #pragma clang diagnostic push
  55. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  56. strongSelf->_fetcherCompletion = ^(NSData *data, NSError *error) {
  57. FIRStorageMetadata *metadata;
  58. if (error) {
  59. if (!self.error) {
  60. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  61. }
  62. } else {
  63. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  64. if (responseDictionary != nil) {
  65. metadata = [[FIRStorageMetadata alloc] initWithDictionary:responseDictionary];
  66. [metadata setType:FIRStorageMetadataTypeFile];
  67. } else {
  68. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  69. }
  70. }
  71. if (callback) {
  72. callback(metadata, self.error);
  73. }
  74. self->_fetcherCompletion = nil;
  75. };
  76. #pragma clang diagnostic pop
  77. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  78. weakSelf.fetcherCompletion(data, error);
  79. }];
  80. }];
  81. }
  82. @end