FIRStorageUtils.m 4.3 KB

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