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 "FirebaseStorageInternal/Sources/FIRStorageGetMetadataTask.h"
  15. #import "FirebaseStorageInternal/Sources/Public/FirebaseStorageInternal/FIRStorageConstants.h"
  16. #import "FirebaseStorageInternal/Sources/FIRStorageMetadata_Private.h"
  17. #import "FirebaseStorageInternal/Sources/FIRStorageTask_Private.h"
  18. #import "FirebaseStorageInternal/Sources/FIRStorageUtils.h"
  19. @implementation FIRStorageGetMetadataTask {
  20. @private
  21. FIRStorageVoidMetadataError _completion;
  22. }
  23. @synthesize fetcher = _fetcher;
  24. @synthesize fetcherCompletion = _fetcherCompletion;
  25. - (instancetype)initWithReference:(FIRIMPLStorageReference *)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. strongSelf->_fetcherCompletion = ^(NSData *data, NSError *error) {
  54. FIRIMPLStorageMetadata *metadata;
  55. if (error) {
  56. if (!self.error) {
  57. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  58. }
  59. } else {
  60. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  61. if (responseDictionary != nil) {
  62. metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:responseDictionary];
  63. [metadata setType:FIRStorageMetadataTypeFile];
  64. } else {
  65. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  66. }
  67. }
  68. if (callback) {
  69. callback(metadata, self.error);
  70. }
  71. self->_fetcherCompletion = nil;
  72. };
  73. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  74. FIRStorageGetMetadataTask *strongSelf = weakSelf;
  75. if (strongSelf.fetcherCompletion) {
  76. strongSelf.fetcherCompletion(data, error);
  77. }
  78. }];
  79. }];
  80. }
  81. @end