FIRStorageGetMetadataTask.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.maxOperationRetryTime;
  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. FIRStorageMetadata *metadata;
  51. if (error) {
  52. if (!self.error) {
  53. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  54. }
  55. } else {
  56. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  57. if (responseDictionary != nil) {
  58. metadata = [[FIRStorageMetadata alloc] initWithDictionary:responseDictionary];
  59. [metadata setType:FIRStorageMetadataTypeFile];
  60. } else {
  61. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  62. }
  63. }
  64. if (callback) {
  65. callback(metadata, self.error);
  66. }
  67. self->_fetcherCompletion = nil;
  68. };
  69. #pragma clang diagnostic pop
  70. __weak FIRStorageGetMetadataTask *weakSelf = self;
  71. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  72. weakSelf.fetcherCompletion(data, error);
  73. }];
  74. }
  75. @end