FIRStorageGetMetadataTask.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. completion:(FIRStorageVoidMetadataError)completion {
  29. self = [super initWithReference:reference fetcherService:service];
  30. if (self) {
  31. _completion = [completion copy];
  32. }
  33. return self;
  34. }
  35. - (void) dealloc {
  36. [_fetcher stopFetching];
  37. }
  38. - (void)enqueue {
  39. NSMutableURLRequest *request = [self.baseRequest mutableCopy];
  40. request.HTTPMethod = @"GET";
  41. request.timeoutInterval = self.reference.storage.maxDownloadRetryTime;
  42. FIRStorageVoidMetadataError callback = _completion;
  43. _completion = nil;
  44. GTMSessionFetcher *fetcher = [self.fetcherService fetcherWithRequest:request];
  45. _fetcher = fetcher;
  46. fetcher.comment = @"GetMetadataTask";
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  49. _fetcherCompletion = ^(NSData *data, NSError *error) {
  50. if (error) {
  51. if (!self.error) {
  52. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  53. }
  54. if (callback) {
  55. callback(nil, self.error);
  56. }
  57. _fetcherCompletion = nil;
  58. return;
  59. }
  60. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  61. if (responseDictionary != nil) {
  62. FIRStorageMetadata *metadata =
  63. [[FIRStorageMetadata alloc] initWithDictionary:responseDictionary];
  64. [metadata setType:FIRStorageMetadataTypeFile];
  65. if (callback) {
  66. callback(metadata, nil);
  67. }
  68. } else {
  69. NSString *returnedData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  70. NSString *invalidDataString =
  71. [NSString stringWithFormat:kFIRStorageInvalidDataFormat, returnedData];
  72. NSDictionary *dict;
  73. if (invalidDataString.length > 0) {
  74. dict = @{NSLocalizedFailureReasonErrorKey : invalidDataString};
  75. }
  76. self.error = [FIRStorageErrors errorWithCode:FIRStorageErrorCodeUnknown infoDictionary:dict];
  77. if (callback) {
  78. callback(nil, self.error);
  79. }
  80. }
  81. _fetcherCompletion = nil;
  82. };
  83. #pragma clang diagnostic pop
  84. __weak FIRStorageGetMetadataTask *weakSelf = self;
  85. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  86. weakSelf.fetcherCompletion(data, error);
  87. }];
  88. }
  89. @end