FIRStorageGetMetadataTask.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "FIRStorageGetMetadataTask.h"
  15. #import "FIRStorageConstants.h"
  16. #import "FIRStorageMetadata_Private.h"
  17. #import "FIRStorageTask_Private.h"
  18. #import "FIRStorageUtils.h"
  19. #import "FirebaseStorage.h"
  20. @implementation FIRStorageGetMetadataTask {
  21. @private
  22. FIRStorageVoidMetadataError _completion;
  23. }
  24. - (instancetype)initWithReference:(FIRStorageReference *)reference
  25. fetcherService:(GTMSessionFetcherService *)service
  26. completion:(FIRStorageVoidMetadataError)completion {
  27. self = [super initWithReference:reference fetcherService:service];
  28. if (self) {
  29. _completion = [completion copy];
  30. }
  31. return self;
  32. }
  33. - (void)enqueue {
  34. NSMutableURLRequest *request = [self.baseRequest mutableCopy];
  35. request.HTTPMethod = @"GET";
  36. request.timeoutInterval = self.reference.storage.maxDownloadRetryTime;
  37. FIRStorageVoidMetadataError callback = _completion;
  38. _completion = nil;
  39. GTMSessionFetcher *fetcher = [self.fetcherService fetcherWithRequest:request];
  40. fetcher.comment = @"GetMetadataTask";
  41. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  42. if (error) {
  43. if (!self.error) {
  44. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  45. }
  46. if (callback) {
  47. callback(nil, self.error);
  48. }
  49. return;
  50. }
  51. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  52. if (responseDictionary != nil) {
  53. FIRStorageMetadata *metadata =
  54. [[FIRStorageMetadata alloc] initWithDictionary:responseDictionary];
  55. [metadata setType:FIRStorageMetadataTypeFile];
  56. if (callback) {
  57. callback(metadata, nil);
  58. }
  59. } else {
  60. NSString *returnedData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  61. NSString *invalidDataString =
  62. [NSString stringWithFormat:kFIRStorageInvalidDataFormat, returnedData];
  63. NSDictionary *dict;
  64. if (invalidDataString.length > 0) {
  65. dict = @{NSLocalizedFailureReasonErrorKey : invalidDataString};
  66. }
  67. self.error = [FIRStorageErrors errorWithCode:FIRStorageErrorCodeUnknown infoDictionary:dict];
  68. if (callback) {
  69. callback(nil, self.error);
  70. }
  71. }
  72. }];
  73. }
  74. @end