FIRStorageUpdateMetadataTask.m 3.8 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. FIRIMPLStorageMetadata *_updateMetadata;
  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. metadata:(FIRIMPLStorageMetadata *)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. strongSelf->_fetcherCompletion = ^(NSData *data, NSError *error) {
  62. FIRIMPLStorageMetadata *metadata;
  63. if (error) {
  64. if (!self.error) {
  65. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  66. }
  67. } else {
  68. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  69. if (responseDictionary) {
  70. metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:responseDictionary];
  71. [metadata setType:FIRStorageMetadataTypeFile];
  72. } else {
  73. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  74. }
  75. }
  76. if (callback) {
  77. callback(metadata, self.error);
  78. }
  79. self->_fetcherCompletion = nil;
  80. };
  81. fetcher.comment = @"UpdateMetadataTask";
  82. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  83. FIRStorageUpdateMetadataTask *strongSelf = weakSelf;
  84. if (strongSelf.fetcherCompletion) {
  85. strongSelf.fetcherCompletion(data, error);
  86. }
  87. }];
  88. }];
  89. }
  90. @end