FIRStorageUtils.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <MobileCoreServices/MobileCoreServices.h>
  15. #import "FIRStorageUtils.h"
  16. #import "FIRStorageConstants_Private.h"
  17. #import "FIRStoragePath.h"
  18. #import "FirebaseStorage.h"
  19. #import "GTMSessionFetcher.h"
  20. // This is the list at https://cloud.google.com/storage/docs/json_api/ without & and +.
  21. NSString *const kGCSObjectAllowedCharacterSet =
  22. @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~!$'()*,;=:@";
  23. @implementation FIRStorageUtils
  24. + (nullable NSString *)GCSEscapedString:(NSString *)string {
  25. NSCharacterSet *allowedCharacters =
  26. [NSCharacterSet characterSetWithCharactersInString:kGCSObjectAllowedCharacterSet];
  27. return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
  28. }
  29. + (nullable NSString *)MIMETypeForExtension:(NSString *)extension {
  30. if (extension == nil) {
  31. return nil;
  32. }
  33. CFStringRef pathExtension = (__bridge_retained CFStringRef)extension;
  34. CFStringRef type =
  35. UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
  36. NSString *mimeType =
  37. (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
  38. CFRelease(pathExtension);
  39. if (type != NULL) {
  40. CFRelease(type);
  41. }
  42. return mimeType;
  43. }
  44. + (NSString *)queryStringForDictionary:(nullable NSDictionary *)dictionary {
  45. if (!dictionary) {
  46. return @"";
  47. }
  48. __block NSMutableArray *queryItems = [[NSMutableArray alloc] initWithCapacity:[dictionary count]];
  49. [dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull name, NSString *_Nonnull value,
  50. BOOL *_Nonnull stop) {
  51. NSString *item =
  52. [FIRStorageUtils GCSEscapedString:[NSString stringWithFormat:@"%@=%@", name, value]];
  53. [queryItems addObject:item];
  54. }];
  55. return [queryItems componentsJoinedByString:@"&"];
  56. }
  57. + (NSURLRequest *)defaultRequestForPath:(FIRStoragePath *)path {
  58. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  59. NSURLComponents *components = [[NSURLComponents alloc] init];
  60. [components setScheme:kFIRStorageScheme];
  61. [components setHost:kFIRStorageHost];
  62. NSString *encodedPath = [self encodedURLForPath:path];
  63. [components setPercentEncodedPath:encodedPath];
  64. [request setURL:components.URL];
  65. return request;
  66. }
  67. + (NSString *)encodedURLForPath:(FIRStoragePath *)path {
  68. NSString *bucketName = [FIRStorageUtils GCSEscapedString:path.bucket];
  69. NSString *objectName = [FIRStorageUtils GCSEscapedString:path.object];
  70. NSString *bucketFormat = [NSString stringWithFormat:kFIRStorageBucketPathFormat, bucketName];
  71. NSString *urlPath = [@"/" stringByAppendingPathComponent:bucketFormat];
  72. if (objectName) {
  73. NSString *objectFormat = [NSString stringWithFormat:kFIRStorageObjectPathFormat, objectName];
  74. urlPath = [urlPath stringByAppendingFormat:@"/%@", objectFormat];
  75. } else {
  76. urlPath = [urlPath stringByAppendingString:@"/o"];
  77. }
  78. return [@"/" stringByAppendingString:[kFIRStorageVersionPath stringByAppendingString:urlPath]];
  79. }
  80. @end
  81. @implementation NSDictionary (FIRStorageNSDictionaryJSONHelpers)
  82. + (nullable instancetype)frs_dictionaryFromJSONData:(nullable NSData *)data {
  83. if (!data) {
  84. return nil;
  85. }
  86. return [NSJSONSerialization JSONObjectWithData:data
  87. options:NSJSONReadingMutableContainers
  88. error:nil];
  89. }
  90. @end
  91. @implementation NSData (FIRStorageNSDataJSONHelpers)
  92. + (nullable instancetype)frs_dataFromJSONDictionary:(nullable NSDictionary *)dictionary {
  93. if (!dictionary) {
  94. return nil;
  95. }
  96. return [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
  97. }
  98. @end