FIRStorageUpdateMetadataTask.m 3.8 KB

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