FIRStorageUpdateMetadataTask.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. - (instancetype)initWithReference:(FIRStorageReference *)reference
  24. fetcherService:(GTMSessionFetcherService *)service
  25. metadata:(FIRStorageMetadata *)metadata
  26. completion:(FIRStorageVoidMetadataError)completion {
  27. self = [super initWithReference:reference fetcherService:service];
  28. if (self) {
  29. _updateMetadata = [metadata copy];
  30. _completion = [completion copy];
  31. }
  32. return self;
  33. }
  34. - (void)enqueue {
  35. NSMutableURLRequest *request = [self.baseRequest mutableCopy];
  36. NSDictionary *updateDictionary = [_updateMetadata dictionaryRepresentation];
  37. NSData *updateData = [NSData frs_dataFromJSONDictionary:updateDictionary];
  38. request.HTTPMethod = @"PATCH";
  39. request.timeoutInterval = self.reference.storage.maxUploadRetryTime;
  40. request.HTTPBody = updateData;
  41. NSString *typeString = @"application/json; charset=UTF-8";
  42. [request setValue:typeString forHTTPHeaderField:@"Content-Type"];
  43. NSString *lengthString = [NSString stringWithFormat:@"%zu", (unsigned long)[updateData length]];
  44. [request setValue:lengthString forHTTPHeaderField:@"Content-Length"];
  45. FIRStorageVoidMetadataError callback = _completion;
  46. _completion = nil;
  47. GTMSessionFetcher *fetcher = [self.fetcherService fetcherWithRequest:request];
  48. fetcher.comment = @"UpdateMetadataTask";
  49. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  50. if (error) {
  51. if (!self.error) {
  52. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  53. }
  54. if (callback) {
  55. callback(nil, self.error);
  56. }
  57. return;
  58. }
  59. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  60. if (responseDictionary) {
  61. FIRStorageMetadata *metadata =
  62. [[FIRStorageMetadata alloc] initWithDictionary:responseDictionary];
  63. [metadata setType:FIRStorageMetadataTypeFile];
  64. if (callback){
  65. callback(metadata, nil);
  66. }
  67. } else {
  68. NSString *returnedData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  69. NSString *invalidDataString =
  70. [NSString stringWithFormat:kFIRStorageInvalidDataFormat, returnedData];
  71. NSDictionary *dict;
  72. if (invalidDataString.length > 0) {
  73. dict = @{NSLocalizedFailureReasonErrorKey : invalidDataString};
  74. }
  75. self.error = [FIRStorageErrors errorWithCode:FIRStorageErrorCodeUnknown infoDictionary:dict];
  76. if (callback) {
  77. callback(nil, self.error);
  78. }
  79. }
  80. }];
  81. }
  82. @end