FIRStorageUtils.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. @class FIRStoragePath;
  18. @class FIRIMPLStorageReference;
  19. NS_ASSUME_NONNULL_BEGIN
  20. /**
  21. * FIRStorageUtils provides a number of helper methods for commonly used operations
  22. * in Firebase Storage, such as JSON parsing, escaping, and file extensions.
  23. */
  24. @interface FIRStorageUtils : NSObject
  25. /**
  26. * Returns a percent encoded string appropriate for GCS.
  27. * See https://cloud.google.com/storage/docs/naming for more details.
  28. * @param string A path to escape characters according to the GCS
  29. * @return A percent encoded string appropriate for GCS operations or nil if string is nil
  30. * or can't be escaped.
  31. */
  32. + (nullable NSString *)GCSEscapedString:(NSString *)string;
  33. /**
  34. * Returns the MIME type for a file extension.
  35. * Example of how to get MIME type here: http://ddeville.me/2011/12/mime-to-UTI-cocoa/
  36. * @param extension A file extension such as "txt", "png", etc.
  37. * @return The MIME type for the input extension such as "text/plain", "image/png", etc.
  38. * or nil if no type is found.
  39. */
  40. + (nullable NSString *)MIMETypeForExtension:(NSString *)extension;
  41. /**
  42. * Returns a properly escaped query string from a given dictionary of query items to values.
  43. * @param dictionary A dictionary containing query items and associated values.
  44. * @return A properly escaped query string or the empty string for a nil or empty dictionary.
  45. */
  46. + (NSString *)queryStringForDictionary:(nullable NSDictionary *)dictionary;
  47. /**
  48. * Returns a base NSURLRequest used by all tasks.
  49. * @param reference The FIRIMPLStorageReference to create a request for.
  50. * @return Returns a properly formatted NSURLRequest of the form:
  51. * scheme://host/version/b/<bucket>/o[/path/to/object]
  52. */
  53. + (NSURLRequest *)defaultRequestForReference:(FIRIMPLStorageReference *)reference;
  54. /**
  55. * Returns a base NSURLRequest with custom query parameters.
  56. * @param reference The FIRIMPLStorageReference to create a request for.
  57. * @param queryParams A key/value dictionary with query parameters.
  58. * @return Returns a formatted NSURLRequest
  59. */
  60. + (NSURLRequest *)defaultRequestForReference:(FIRIMPLStorageReference *)reference
  61. queryParams:(NSDictionary<NSString *, NSString *> *)queryParams;
  62. /**
  63. * Creates the appropriate GCS percent escaped path for a given FIRStoragePath.
  64. * @param path The FIRStoragePath to encode.
  65. * @return Returns the GCS encoded URL for a given FIRStoragePath.
  66. */
  67. + (NSString *)encodedURLForPath:(FIRStoragePath *)path;
  68. /**
  69. * Creates a NSError in the Firebase Storage domain with given code and description.
  70. * Useful for argument validation.
  71. * @param description The error description to surface to the user.
  72. * @param code The error code.
  73. * @return An NSError in the Firebase Storage error domain.
  74. */
  75. + (NSError *)storageErrorWithDescription:(NSString *)description code:(NSInteger)code;
  76. /**
  77. * Performs a crude translation of the user provided timeouts to the retry intervals that
  78. * GTMSessionFetcher accepts. GTMSessionFetcher times out operations if the time between individual
  79. * retry attempts exceed a certain threshold, while our API contract looks at the total observed
  80. * time of the operation (i.e. the sum of all retries).
  81. * @param retryTime A timeout that caps the sum of all retry attempts
  82. * @return A timeout that caps the timeout of the last retry attempt
  83. */
  84. + (NSTimeInterval)computeRetryIntervalFromRetryTime:(NSTimeInterval)retryTime;
  85. @end
  86. @interface NSDictionary (FIRStorageNSDictionaryJSONHelpers)
  87. /**
  88. * Returns a dictionary representation of the data in @a data.
  89. * @param data NSData containing JSON data.
  90. * @return An NSDictionary representation of the JSON, or nil if serialization failed.
  91. */
  92. + (nullable instancetype)frs_dictionaryFromJSONData:(nullable NSData *)data;
  93. @end
  94. @interface NSData (FIRStorageNSDataJSONHelpers)
  95. /**
  96. * Returns an NSData instance containing JSON serialized from @a dictionary.
  97. * @param dictionary An NSDictionary containing only types serializable to JSON.
  98. * @return An NSData object representing the binary JSON, or nil if serialization failed.
  99. */
  100. + (nullable instancetype)frs_dataFromJSONDictionary:(nullable NSDictionary *)dictionary;
  101. @end
  102. NS_ASSUME_NONNULL_END