FIRStorageUpdateMetadataTask.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.maxOperationRetryTime;
  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. FIRStorageMetadata *metadata;
  58. if (error) {
  59. if (!self.error) {
  60. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  61. }
  62. } else {
  63. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  64. if (responseDictionary) {
  65. metadata = [[FIRStorageMetadata alloc] initWithDictionary:responseDictionary];
  66. [metadata setType:FIRStorageMetadataTypeFile];
  67. } else {
  68. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  69. }
  70. }
  71. if (callback) {
  72. callback(metadata, self.error);
  73. }
  74. self->_fetcherCompletion = nil;
  75. };
  76. #pragma clang diagnostic pop
  77. fetcher.comment = @"UpdateMetadataTask";
  78. __weak FIRStorageUpdateMetadataTask *weakSelf = self;
  79. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  80. weakSelf.fetcherCompletion(data, error);
  81. }];
  82. }
  83. @end