FIRStorageUpdateMetadataTask.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/FIRStorageUpdateMetadataTask.h"
  15. #import "FirebaseStorage/Sources/FIRStorageMetadata_Private.h"
  16. #import "FirebaseStorage/Sources/FIRStorageTask_Private.h"
  17. @implementation FIRStorageUpdateMetadataTask {
  18. @private
  19. FIRStorageVoidMetadataError _completion;
  20. // Metadata used in the update request
  21. FIRStorageMetadata *_updateMetadata;
  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. metadata:(FIRStorageMetadata *)metadata
  29. completion:(FIRStorageVoidMetadataError)completion {
  30. self = [super initWithReference:reference fetcherService:service dispatchQueue:queue];
  31. if (self) {
  32. _updateMetadata = [metadata copy];
  33. _completion = [completion copy];
  34. }
  35. return self;
  36. }
  37. - (void)dealloc {
  38. [_fetcher stopFetching];
  39. }
  40. - (void)enqueue {
  41. __weak FIRStorageUpdateMetadataTask *weakSelf = self;
  42. [self dispatchAsync:^() {
  43. FIRStorageUpdateMetadataTask *strongSelf = weakSelf;
  44. if (!strongSelf) {
  45. return;
  46. }
  47. NSMutableURLRequest *request = [strongSelf.baseRequest mutableCopy];
  48. NSDictionary *updateDictionary = [strongSelf->_updateMetadata updatedMetadata];
  49. NSData *updateData = [NSData frs_dataFromJSONDictionary:updateDictionary];
  50. request.HTTPMethod = @"PATCH";
  51. request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime;
  52. request.HTTPBody = updateData;
  53. NSString *typeString = @"application/json; charset=UTF-8";
  54. [request setValue:typeString forHTTPHeaderField:@"Content-Type"];
  55. NSString *lengthString = [NSString stringWithFormat:@"%zu", (unsigned long)[updateData length]];
  56. [request setValue:lengthString forHTTPHeaderField:@"Content-Length"];
  57. FIRStorageVoidMetadataError callback = strongSelf->_completion;
  58. strongSelf->_completion = nil;
  59. GTMSessionFetcher *fetcher = [strongSelf.fetcherService fetcherWithRequest:request];
  60. strongSelf->_fetcher = fetcher;
  61. #pragma clang diagnostic push
  62. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  63. strongSelf->_fetcherCompletion = ^(NSData *data, NSError *error) {
  64. FIRStorageMetadata *metadata;
  65. if (error) {
  66. if (!self.error) {
  67. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  68. }
  69. } else {
  70. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  71. if (responseDictionary) {
  72. metadata = [[FIRStorageMetadata alloc] initWithDictionary:responseDictionary];
  73. [metadata setType:FIRStorageMetadataTypeFile];
  74. } else {
  75. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  76. }
  77. }
  78. if (callback) {
  79. callback(metadata, self.error);
  80. }
  81. self->_fetcherCompletion = nil;
  82. };
  83. #pragma clang diagnostic pop
  84. fetcher.comment = @"UpdateMetadataTask";
  85. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  86. weakSelf.fetcherCompletion(data, error);
  87. }];
  88. }];
  89. }
  90. @end