FIRStorageUtils.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. NS_ASSUME_NONNULL_BEGIN
  19. /**
  20. * FIRStorageUtils provides a number of helper methods for commonly used operations
  21. * in Firebase Storage, such as JSON parsing, escaping, and file extensions.
  22. */
  23. @interface FIRStorageUtils : NSObject
  24. /**
  25. * Returns a percent encoded string appropriate for GCS.
  26. * See https://cloud.google.com/storage/docs/naming for more details.
  27. * @param string A path to escape characters according to the GCS
  28. * @return A percent encoded string appropriate for GCS operations or nil if string is nil
  29. * or can't be escaped.
  30. */
  31. + (nullable NSString *)GCSEscapedString:(NSString *)string;
  32. /**
  33. * Returns the MIME type for a file extension.
  34. * Example of how to get MIME type here: http://ddeville.me/2011/12/mime-to-UTI-cocoa/
  35. * @param extension A file extension such as "txt", "png", etc.
  36. * @return The MIME type for the input extension such as "text/plain", "image/png", etc.
  37. * or nil if no type is found.
  38. */
  39. + (nullable NSString *)MIMETypeForExtension:(NSString *)extension;
  40. /**
  41. * Returns a properly escaped query string from a given dictionary of query items to values.
  42. * @param dictionary A dictionary containing query items and associated values.
  43. * @return A properly escaped query string or the empty string for a nil or empty dictionary.
  44. */
  45. + (NSString *)queryStringForDictionary:(nullable NSDictionary *)dictionary;
  46. /**
  47. * Returns a base NSURLRequest used by all tasks.
  48. * @param path The FIRStoragePath to create a request for.
  49. * @return Returns a properly formatted NSURLRequest of the form:
  50. * scheme://host/version/b/<bucket>/o[/path/to/object]
  51. */
  52. + (NSURLRequest *)defaultRequestForPath:(FIRStoragePath *)path;
  53. /**
  54. * Returns a base NSURLRequest with custom query parameters.
  55. * @param path The FIRStoragePath to create a request for.
  56. * @param queryParams A key/value dictionary with query parameters.
  57. * @return Returns a formatted NSURLRequest
  58. */
  59. + (NSURLRequest *)defaultRequestForPath:(FIRStoragePath *)path
  60. queryParams:(NSDictionary<NSString *, NSString *> *)queryParams;
  61. /**
  62. * Creates the appropriate GCS percent escaped path for a given FIRStoragePath.
  63. * @param path The FIRStoragePath to encode.
  64. * @return Returns the GCS encoded URL for a given FIRStoragePath.
  65. */
  66. + (NSString *)encodedURLForPath:(FIRStoragePath *)path;
  67. /**
  68. * Creates a NSError in the Firebase Storage domain with given code and description.
  69. * Useful for argument validation.
  70. * @param description The error description to surface to the user.
  71. * @param code The error code.
  72. * @return An NSError in the Firebase Storage error domain.
  73. */
  74. + (NSError *)storageErrorWithDescription:(NSString *)description code:(NSInteger)code;
  75. /**
  76. * Performs a crude translation of the user provided timeouts to the retry intervals that
  77. * GTMSessionFetcher accepts. GTMSessionFetcher times out operations if the time between individual
  78. * retry attempts exceed a certain threshold, while our API contract looks at the total observed
  79. * time of the operation (i.e. the sum of all retries).
  80. * @param retryTime A timeout that caps the sum of all retry attempts
  81. * @return A timeout that caps the timeout of the last retry attempt
  82. */
  83. + (NSTimeInterval)computeRetryIntervalFromRetryTime:(NSTimeInterval)retryTime;
  84. @end
  85. @interface NSDictionary (FIRStorageNSDictionaryJSONHelpers)
  86. /**
  87. * Returns a dictionary representation of the data in @a data.
  88. * @param data NSData containing JSON data.
  89. * @return An NSDictionary representation of the JSON, or nil if serialization failed.
  90. */
  91. + (nullable instancetype)frs_dictionaryFromJSONData:(nullable NSData *)data;
  92. @end
  93. @interface NSData (FIRStorageNSDataJSONHelpers)
  94. /**
  95. * Returns an NSData instance containing JSON serialized from @a dictionary.
  96. * @param dictionary An NSDictionary containing only types serializable to JSON.
  97. * @return An NSData object representing the binary JSON, or nil if serialization failed.
  98. */
  99. + (nullable instancetype)frs_dataFromJSONDictionary:(nullable NSDictionary *)dictionary;
  100. @end
  101. NS_ASSUME_NONNULL_END