aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Storage/Private
diff options
context:
space:
mode:
authorGravatar Paul Beusterien <paulbeusterien@google.com>2017-05-15 12:27:07 -0700
committerGravatar Paul Beusterien <paulbeusterien@google.com>2017-05-15 12:27:07 -0700
commit98ba64449a632518bd2b86fe8d927f4a960d3ddc (patch)
tree131d9c4272fa6179fcda6c5a33fcb3b1bd57ad2e /Firebase/Storage/Private
parent32461366c9e204a527ca05e6e9b9404a2454ac51 (diff)
Initial
Diffstat (limited to 'Firebase/Storage/Private')
-rw-r--r--Firebase/Storage/Private/FIRStorageConstants_Private.h145
-rw-r--r--Firebase/Storage/Private/FIRStorageDeleteTask.h34
-rw-r--r--Firebase/Storage/Private/FIRStorageDownloadTask_Private.h52
-rw-r--r--Firebase/Storage/Private/FIRStorageErrors.h54
-rw-r--r--Firebase/Storage/Private/FIRStorageGetMetadataTask.h34
-rw-r--r--Firebase/Storage/Private/FIRStorageMetadata_Private.h52
-rw-r--r--Firebase/Storage/Private/FIRStorageObservableTask_Private.h45
-rw-r--r--Firebase/Storage/Private/FIRStoragePath.h106
-rw-r--r--Firebase/Storage/Private/FIRStorageReference_Private.h37
-rw-r--r--Firebase/Storage/Private/FIRStorageTaskSnapshot_Private.h56
-rw-r--r--Firebase/Storage/Private/FIRStorageTask_Private.h77
-rw-r--r--Firebase/Storage/Private/FIRStorageTokenAuthorizer.h44
-rw-r--r--Firebase/Storage/Private/FIRStorageUpdateMetadataTask.h35
-rw-r--r--Firebase/Storage/Private/FIRStorageUploadTask_Private.h69
-rw-r--r--Firebase/Storage/Private/FIRStorageUtils.h93
-rw-r--r--Firebase/Storage/Private/FIRStorage_Private.h38
16 files changed, 971 insertions, 0 deletions
diff --git a/Firebase/Storage/Private/FIRStorageConstants_Private.h b/Firebase/Storage/Private/FIRStorageConstants_Private.h
new file mode 100644
index 0000000..50addb1
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageConstants_Private.h
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+@class FIRStorageMetadata;
+
+NS_ASSUME_NONNULL_BEGIN
+
+FOUNDATION_EXPORT NSString *const kGCSScheme;
+FOUNDATION_EXPORT NSString *const kGCSHost;
+FOUNDATION_EXPORT NSString *const kGCSUploadPath;
+FOUNDATION_EXPORT NSString *const kGCSStorageVersionPath;
+FOUNDATION_EXPORT NSString *const kGCSBucketPathFormat;
+FOUNDATION_EXPORT NSString *const kGCSObjectPathFormat;
+
+FOUNDATION_EXPORT NSString *const kFIRStorageScheme;
+FOUNDATION_EXPORT NSString *const kFIRStorageHost;
+FOUNDATION_EXPORT NSString *const kFIRStorageVersionPath;
+FOUNDATION_EXPORT NSString *const kFIRStorageBucketPathFormat;
+FOUNDATION_EXPORT NSString *const kFIRStorageObjectPathFormat;
+FOUNDATION_EXPORT NSString *const kFIRStorageFullPathFormat;
+
+FOUNDATION_EXPORT NSString *const kFIRStorageAuthTokenFormat;
+FOUNDATION_EXPORT NSString *const kFIRStorageDefaultBucketFormat;
+
+FOUNDATION_EXPORT NSString *const kFIRStorageResponseErrorDomain;
+FOUNDATION_EXPORT NSString *const kFIRStorageResponseErrorCode;
+FOUNDATION_EXPORT NSString *const kFIRStorageResponseBody;
+
+FOUNDATION_EXPORT NSString *const kFIRStorageTaskStatusResumeNotification;
+FOUNDATION_EXPORT NSString *const kFIRStorageTaskStatusPauseNotification;
+FOUNDATION_EXPORT NSString *const kFIRStorageTaskStatusProgressNotification;
+FOUNDATION_EXPORT NSString *const kFIRStorageTaskStatusCompleteNotification;
+FOUNDATION_EXPORT NSString *const kFIRStorageTaskStatusFailureNotification;
+
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataBucket;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataCacheControl;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataContentDisposition;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataContentEncoding;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataContentLanguage;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataContentType;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataCustomMetadata;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataSize;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataDownloadURLs;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataGeneration;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataMetageneration;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataTimeCreated;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataUpdated;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataName;
+FOUNDATION_EXPORT NSString *const kFIRStorageMetadataDownloadTokens;
+
+FOUNDATION_EXPORT NSString *const kFIRStorageInvalidDataFormat;
+FOUNDATION_EXPORT NSString *const kFIRStorageInvalidObserverStatus;
+
+FOUNDATION_EXPORT NSString *const kFIRStorageBundleIdentifier;
+
+/**
+ * Enum representing the internal state of an upload or download task.
+ */
+typedef NS_ENUM(NSInteger, FIRStorageTaskState) {
+ /**
+ * Unknown task state
+ */
+ FIRStorageTaskStateUnknown,
+
+ /**
+ * Task is being queued is ready to run
+ */
+ FIRStorageTaskStateQueueing,
+
+ /**
+ * Task is resuming from a paused state
+ */
+ FIRStorageTaskStateResuming,
+
+ /**
+ * Task is currently running
+ */
+ FIRStorageTaskStateRunning,
+
+ /**
+ * Task reporting a progress event
+ */
+ FIRStorageTaskStateProgress,
+
+ /**
+ * Task is pausing
+ */
+ FIRStorageTaskStatePausing,
+
+ /**
+ * Task is completing successfully
+ */
+ FIRStorageTaskStateCompleting,
+
+ /**
+ * Task is failing unrecoverably
+ */
+ FIRStorageTaskStateFailing,
+
+ /**
+ * Task paused successfully
+ */
+ FIRStorageTaskStatePaused,
+
+ /**
+ * Task cancelled successfully
+ */
+ FIRStorageTaskStateCancelled,
+
+ /**
+ * Task completed successfully
+ */
+ FIRStorageTaskStateSuccess,
+
+ /**
+ * Task failed unrecoverably
+ */
+ FIRStorageTaskStateFailed
+};
+
+/**
+ * Represents the various types of metadata: Files or Folders.
+ */
+typedef NS_ENUM(NSUInteger, FIRStorageMetadataType) {
+ FIRStorageMetadataTypeUnknown,
+ FIRStorageMetadataTypeFile,
+ FIRStorageMetadataTypeFolder,
+};
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageDeleteTask.h b/Firebase/Storage/Private/FIRStorageDeleteTask.h
new file mode 100644
index 0000000..c97fd27
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageDeleteTask.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import "FIRStorageTask.h"
+
+@class GTMSessionFetcherService;
+
+NS_ASSUME_NONNULL_BEGIN
+
+/**
+ * Task which provides the ability to delete an object in Firebase Storage.
+ */
+@interface FIRStorageDeleteTask : FIRStorageTask<FIRStorageTaskManagement>
+
+- (instancetype)initWithReference:(FIRStorageReference *)reference
+ fetcherService:(GTMSessionFetcherService *)service
+ completion:(FIRStorageVoidError)completion;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageDownloadTask_Private.h b/Firebase/Storage/Private/FIRStorageDownloadTask_Private.h
new file mode 100644
index 0000000..293d1d5
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageDownloadTask_Private.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+@class GTMSessionFetcherService;
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface FIRStorageDownloadTask ()
+
+/**
+ * Bytes which have been downloaded so far.
+ */
+@property(readonly, nonatomic) NSData *downloadData;
+
+/**
+ * The file on disk to write to.
+ */
+@property(copy, nonatomic) NSURL *fileURL;
+
+/**
+ * Initializes a download task with a base FIRStorageReference and GTMSessionFetcherService.
+ * @param reference The base FIRStorageReference which fetchers use for configuration.
+ * @param service The GTMSessionFetcherService which will create fetchers.
+ * @param fileURL The system URL to download to. If nil, download in memory as bytes.
+ * @return Returns an instance of FIRStorageDownloadTask
+ */
+- (instancetype)initWithReference:(FIRStorageReference *)reference
+ fetcherService:(GTMSessionFetcherService *)service
+ file:(nullable NSURL *)fileURL;
+
+/**
+ * Cancels the download task and passes an appropriate error to the developer.
+ * @param error NSError to propegate to the developer.
+ */
+- (void)cancelWithError:(NSError *)error;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageErrors.h b/Firebase/Storage/Private/FIRStorageErrors.h
new file mode 100644
index 0000000..7c236d9
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageErrors.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import "FIRStorageConstants.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@class FIRStorageReference;
+
+/**
+ * Adds wrappers for common Firebase Storage errors (including creating errors from GCS errors).
+ * For more information on unwrapping GCS errors, see the GCS errors docs:
+ * https://cloud.google.com/storage/docs/json_api/v1/status-codes
+ * This is never publicly exposed to end developers (as they will simply see an NSError).
+ */
+@interface FIRStorageErrors : NSObject
+
+/**
+ * Creates a Firebase Storage error from a specific FIRStorageErrorCode.
+ */
++ (NSError *)errorWithCode:(FIRStorageErrorCode)code;
+
+/**
+ * Creates a Firebase Storage error from a specific FIRStorageErrorCode while adding
+ * custom info from an optionally provided info dictionary.
+ */
++ (NSError *)errorWithCode:(FIRStorageErrorCode)code
+ infoDictionary:(nullable NSDictionary *)dictionary;
+
+/**
+ * Creates a Firebase Storage error from a specific GCS error and FIRStorageReference.
+ * @param error Server error to wrap and return as a Firebase Storage error.
+ * @param reference FIRStorageReference which provides context about the request being made.
+ * @return Returns an Firebase Storage error, or nil if no error is provided.
+ */
++ (nullable NSError *)errorWithServerError:(nullable NSError *)error
+ reference:(nullable FIRStorageReference *)reference;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageGetMetadataTask.h b/Firebase/Storage/Private/FIRStorageGetMetadataTask.h
new file mode 100644
index 0000000..5f1dc8f
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageGetMetadataTask.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import "FIRStorageTask.h"
+
+@class GTMSessionFetcherService;
+
+NS_ASSUME_NONNULL_BEGIN
+
+/**
+ * Task which provides the ability to get metadata on an object in Firebase Storage.
+ */
+@interface FIRStorageGetMetadataTask : FIRStorageTask<FIRStorageTaskManagement>
+
+- (instancetype)initWithReference:(FIRStorageReference *)reference
+ fetcherService:(GTMSessionFetcherService *)service
+ completion:(FIRStorageVoidMetadataError)completion;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageMetadata_Private.h b/Firebase/Storage/Private/FIRStorageMetadata_Private.h
new file mode 100644
index 0000000..629c935
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageMetadata_Private.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import "FIRStorageConstants_Private.h"
+
+@class FIRStorageReference;
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface FIRStorageMetadata ()
+
+@property(readwrite, nonatomic) NSString *name;
+
+@property(readwrite, nonatomic) NSString *path;
+
+@property(readwrite, nonatomic) FIRStorageReference *reference;
+
+/**
+ * The type of the object, either a "File" or a "Folder".
+ */
+@property(readwrite) FIRStorageMetadataType type;
+
+/**
+ * Returns an RFC3339 formatted date from a string.
+ * @param dateString An NSString of the form: yyyy-MM-ddTHH:mm:ss.SSSZ.
+ * @return An NSDate populated from the string or nil if conversion isn't possible.
+ */
+- (nullable NSDate *)dateFromRFC3339String:(NSString *)dateString;
+
+/**
+ * Returns an RFC3339 formatted string from an NSDate object.
+ * @param date The NSDate object to be converted to a string.
+ * @return An NSString of the form: yyyy-MM-ddTHH:mm:ss.SSSZ or nil if conversion isn't possible.
+ */
+- (nullable NSString *)RFC3339StringFromDate:(NSDate *)date;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageObservableTask_Private.h b/Firebase/Storage/Private/FIRStorageObservableTask_Private.h
new file mode 100644
index 0000000..e37b63f
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageObservableTask_Private.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+NS_ASSUME_NONNULL_BEGIN
+
+@class FIRStorageTaskSnapshot;
+
+@class GTMSessionFetcherService;
+
+@interface FIRStorageObservableTask ()
+
+/**
+ * Creates a new FIRStorageTask initialized with a FIRStorageReference and GTMSessionFetcherService.
+ * @param reference A FIRStorageReference the task will be performed on.
+ * @param service A GTMSessionFetcherService which provides the fetchers and configuration for
+ * requests.
+ * @return A new FIRStorageTask representing the current task.
+ */
+- (instancetype)initWithReference:(FIRStorageReference *)reference
+ fetcherService:(GTMSessionFetcherService *)service;
+
+/**
+ * Raise events for a given task status by passing along a snapshot of existing task state.
+ * @param status A FIRStorageTaskStatus to raise events for.
+ * @param snapshot A FIRStorageTaskSnapshot snapshot of task state to pass through the handler.
+ */
+- (void)fireHandlersForStatus:(FIRStorageTaskStatus)status
+ snapshot:(FIRStorageTaskSnapshot *)snapshot;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStoragePath.h b/Firebase/Storage/Private/FIRStoragePath.h
new file mode 100644
index 0000000..53ff7ef
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStoragePath.h
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+/**
+ * Represents a path in GCS, which can be represented as: gs://bucket/path/to/object
+ * or http[s]://firebasestorage.googleapis.com/v0/b/bucket/o/path/to/object?token=<12345>
+ * This class also includes helper methods to parse those URI/Ls, as well as to
+ * add and remove path segments.
+ */
+@interface FIRStoragePath : NSObject
+
+/**
+ * The GCS bucket in the path.
+ */
+@property(copy, nonatomic) NSString *bucket;
+
+/**
+ * The GCS object in the path.
+ */
+@property(copy, nonatomic, nullable) NSString *object;
+
+/**
+ * Parses a generic string (representing some URI or URL) and returns the appropriate path.
+ * @param string String which is parsed into a path.
+ * @return Returns an instance of FIRStoragePath or nil if one can't be created.
+ * @throws Throws an exception if the string is not a valid gs:// URI or http[s]:// URL.
+ */
++ (nullable FIRStoragePath *)pathFromString:(NSString *)string;
+
+/**
+ * Parses a gs://bucket/path/to/object URI into a GCS path.
+ * @param aURIString gs:// URI which is parsed into a path.
+ * @return Returns an instance of FIRStoragePath or nil if one can't be created.
+ * @throws Throws an exception if the string is not a valid gs:// URI.
+ */
++ (nullable FIRStoragePath *)pathFromGSURI:(NSString *)aURIString;
+
+- (instancetype)init NS_UNAVAILABLE;
+
+/**
+ * Constructs an FIRStoragePath object that represents the given bucket and object.
+ * @param bucket The name of the bucket.
+ * @param object The name of the object.
+ * @return An instance of FIRStoragePath representing the @a bucket and @a object.
+ */
+- (instancetype)initWithBucket:(NSString *)bucket
+ object:(nullable NSString *)object NS_DESIGNATED_INITIALIZER;
+
+/**
+ * Parses a http[s]://firebasestorage.googleapis.com/v0/b/bucket/o/path/to/object...?token=<12345>
+ * URL into a GCS path.
+ * @param aURLString http[s]:// URL which is parsed into a path.
+ * string which is parsed into a path.
+ * @return Returns an instance of FIRStoragePath or nil if one can't be created.
+ * @throws Throws an exception if the string is not a valid http[s]:// URL.
+ */
++ (nullable FIRStoragePath *)pathFromHTTPURL:(NSString *)aURLString;
+
+/**
+ * Creates a new path based off of the current path and a string appended to it.
+ * Note that all slashes are compressed to a single slash, and leading and trailing slashes
+ * are removed.
+ * @param path String to append to the current path.
+ * @return Returns a new instance of FIRStoragePath with the new path appended.
+ */
+- (FIRStoragePath *)child:(NSString *)path;
+
+/**
+ * Creates a new path based off of the current path with the last path segment removed.
+ * @return Returns a new instance of FIRStoragePath pointing to the parent path,
+ * or nil if the current path points to the root.
+ */
+- (nullable FIRStoragePath *)parent;
+
+/**
+ * Creates a new path based off of the root of the bucket.
+ * @return Returns a new instance of FIRStoragePath pointing to the root of the bucket.
+ */
+- (FIRStoragePath *)root;
+
+/**
+ * Returns a GS URI representing the current path.
+ * @return Returns a gs://bucket/path/to/object URI representing the current path.
+ */
+- (NSString *)stringValue;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageReference_Private.h b/Firebase/Storage/Private/FIRStorageReference_Private.h
new file mode 100644
index 0000000..825964d
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageReference_Private.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import "FIRStoragePath.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface FIRStorageReference ()
+
+@property(nonatomic, readwrite) FIRStorage *storage;
+
+/**
+ * The current path which points to an object in the Google Cloud Storage bucket.
+ */
+@property(strong, nonatomic) FIRStoragePath *path;
+
+- (instancetype)initWithStorage:(FIRStorage *)storage
+ path:(FIRStoragePath *)path NS_DESIGNATED_INITIALIZER;
+
+- (NSString *)stringValue;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageTaskSnapshot_Private.h b/Firebase/Storage/Private/FIRStorageTaskSnapshot_Private.h
new file mode 100644
index 0000000..1762a61
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageTaskSnapshot_Private.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import "FIRStorageConstants_Private.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@class FIRStorageMetadata;
+@class FIRStorageReference;
+@class FIRStorageTask;
+
+@interface FIRStorageTaskSnapshot ()
+
+@property(readwrite, copy, nonatomic) FIRStorageTask *task;
+@property(readwrite, copy, nonatomic) FIRStorageMetadata *metadata;
+@property(readwrite, copy, nonatomic) FIRStorageReference *reference;
+@property(readwrite, strong, nonatomic) NSProgress *progress;
+@property(readwrite, copy, nonatomic) NSError *error;
+
+/**
+ * Creates a new task snapshot from the given properties.
+ * @param task The task being represented in this snapshot.
+ * @param state The current state of the parent task.
+ * @param metadata The FIRStorageMetadata of a task. Before upload/update, contains the metadata
+ * to be updated; after, contains the returned metadata. May be nil if no metadata is provided
+ * or returned.
+ * @param reference The FIRStorageReference that spawned the task this snapshot is based on.
+ * @param progress An NSProgress object containing progress of the task this snapshot is based on,
+ * or nil if the task doesn't report progress.
+ * @param error An NSError object containing an error that occurred during the task,
+ * if one occurred.
+ * @return Returns the constructed snapshot.
+ */
+- (instancetype)initWithTask:(__kindof FIRStorageTask *)task
+ state:(FIRStorageTaskState)state
+ metadata:(nullable FIRStorageMetadata *)metadata
+ reference:(FIRStorageReference *)reference
+ progress:(nullable NSProgress *)progress
+ error:(nullable NSError *)error;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageTask_Private.h b/Firebase/Storage/Private/FIRStorageTask_Private.h
new file mode 100644
index 0000000..598006b
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageTask_Private.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import "FIRStorageConstants_Private.h"
+#import "FIRStorageErrors.h"
+#import "FIRStorageReference.h"
+#import "FIRStorageReference_Private.h"
+#import "FIRStorageTaskSnapshot.h"
+#import "FIRStorageTaskSnapshot_Private.h"
+#import "FIRStorageUtils.h"
+
+#import <GTMSessionFetcher/GTMSessionFetcher.h>
+#import <GTMSessionFetcher/GTMSessionFetcherService.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface FIRStorageTask ()
+
+/**
+ * State for the current task in progress.
+ */
+@property(atomic) FIRStorageTaskState state;
+
+/**
+ * FIRStorageMetadata for the task in progress, or nil if none present.
+ */
+@property(strong, nonatomic, nullable) FIRStorageMetadata *metadata;
+
+/**
+ * Error which occurred during task execution, or nil if no error occurred.
+ */
+@property(strong, nonatomic, nullable) NSError *error;
+
+/**
+ * NSProgress object which tracks the progess of an observable task.
+ */
+@property(strong, nonatomic) NSProgress *progress;
+
+/**
+ * Reference pointing to the location the task is being performed against.
+ */
+@property(strong, nonatomic) FIRStorageReference *reference;
+
+@property(strong, readwrite, nonatomic, nonnull) FIRStorageTaskSnapshot *snapshot;
+
+@property(readonly, copy, nonatomic) NSURLRequest *baseRequest;
+
+@property(strong, atomic) GTMSessionFetcher *fetcher;
+
+@property(readonly, nonatomic) GTMSessionFetcherService *fetcherService;
+
+/**
+ * Creates a new FIRStorageTask initialized with a FIRStorageReference and GTMSessionFetcherService.
+ * @param reference A FIRStorageReference the task will be performed on.
+ * @param service A GTMSessionFetcherService which provides the fetchers and configuration for
+ * requests.
+ * @return A new FIRStorageTask representing the current task.
+ */
+- (instancetype)initWithReference:(FIRStorageReference *)reference
+ fetcherService:(GTMSessionFetcherService *)service NS_DESIGNATED_INITIALIZER;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageTokenAuthorizer.h b/Firebase/Storage/Private/FIRStorageTokenAuthorizer.h
new file mode 100644
index 0000000..78a8218
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageTokenAuthorizer.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <GTMSessionFetcher/GTMSessionFetcherService.h>
+
+@class FIRApp;
+
+NS_ASSUME_NONNULL_BEGIN
+
+/**
+ * Wrapper class for FIRApp that implements the GTMFetcherAuthorizationProtocol,
+ * so as to easily provide GTMSessionFetcher fetches a Firebase Authentication JWT
+ * for the current logged in user. Handles token expiration and other failure cases.
+ * If no authentication provider exists or no token is found, no token is added
+ * and the request is passed.
+ */
+@interface FIRStorageTokenAuthorizer : NSObject<GTMFetcherAuthorizationProtocol>
+
+/**
+ * Initializes the token authorizer with an instance of FIRApp.
+ * @param app An instance of FIRApp which provides auth tokens.
+ * @return Returns an instance of FIRStorageTokenAuthorizer which adds the appropriate
+ * "Authorization" header to all outbound requests. Note that a token may not be added
+ * if a getTokenImplementation doesn't exist on FIRApp. This allows for unauthenticated
+ * access, if Firebase Storage rules allow for it.
+ */
+- (instancetype)initWithApp:(FIRApp *)app fetcherService:(GTMSessionFetcherService *)service;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageUpdateMetadataTask.h b/Firebase/Storage/Private/FIRStorageUpdateMetadataTask.h
new file mode 100644
index 0000000..2fcefdd
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageUpdateMetadataTask.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import "FIRStorageTask.h"
+
+@class GTMSessionFetcherService;
+
+NS_ASSUME_NONNULL_BEGIN
+
+/**
+ * Task which provides the ability update the metadata on an object in Firebase Storage.
+ */
+@interface FIRStorageUpdateMetadataTask : FIRStorageTask<FIRStorageTaskManagement>
+
+- (instancetype)initWithReference:(FIRStorageReference *)reference
+ fetcherService:(GTMSessionFetcherService *)service
+ metadata:(FIRStorageMetadata *)metadata
+ completion:(FIRStorageVoidMetadataError)completion;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageUploadTask_Private.h b/Firebase/Storage/Private/FIRStorageUploadTask_Private.h
new file mode 100644
index 0000000..468d9d3
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageUploadTask_Private.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+@class GTMSessionUploadFetcher;
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface FIRStorageUploadTask ()
+
+/**
+ * The data to be uploaded (if uploading bytes).
+ */
+@property(readonly, copy, nonatomic, nullable) NSData *uploadData;
+
+/**
+ * The name of a file on disk to be uploaded (if uploading from a file).
+ */
+@property(readonly, copy, nonatomic, nullable) NSURL *fileURL;
+
+/**
+ * The FIRStorageMetadata about the object being uploaded.
+ */
+@property(readonly, copy, nonatomic) FIRStorageMetadata *uploadMetadata;
+
+/**
+ * GTMSessionUploadFetcher used by all uploads.
+ */
+@property(strong, atomic) GTMSessionUploadFetcher *uploadFetcher;
+
+/**
+ * Initializes an upload task with a base FIRStorageReference and GTMSessionFetcherService.
+ * @param reference The base FIRStorageReference which fetchers use for configuration.
+ * @param service The GTMSessionFetcherService which will create fetchers.
+ * @param uploadData The NSData object to be uploaded.
+ * @return Returns an instance of FIRStorageUploadTask.
+ */
+- (instancetype)initWithReference:(FIRStorageReference *)reference
+ fetcherService:(GTMSessionFetcherService *)service
+ data:(NSData *)uploadData
+ metadata:(FIRStorageMetadata *)metadata;
+
+/**
+ * Initializes an upload task with a base FIRStorageReference and GTMSessionFetcherService.
+ * @param reference The base FIRStorageReference which fetchers use for configuration.
+ * @param service The GTMSessionFetcherService which will create fetchers.
+ * @param fileURL The system file URL to upload from.
+ * @return Returns an instance of FIRStorageUploadTask.
+ */
+- (instancetype)initWithReference:(FIRStorageReference *)reference
+ fetcherService:(GTMSessionFetcherService *)service
+ file:(NSURL *)fileURL
+ metadata:(FIRStorageMetadata *)metadata;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorageUtils.h b/Firebase/Storage/Private/FIRStorageUtils.h
new file mode 100644
index 0000000..e687c82
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorageUtils.h
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+@class FIRStoragePath;
+
+NS_ASSUME_NONNULL_BEGIN
+
+/**
+ * FIRStorageUtils provides a number of helper methods for commonly used operations
+ * in Firebase Storage, such as JSON parsing, escaping, and file extensions.
+ */
+@interface FIRStorageUtils : NSObject
+
+/**
+ * Returns a percent encoded string appropriate for GCS.
+ * See https://cloud.google.com/storage/docs/naming for more details.
+ * @param string A path to escape characters according to the GCS
+ * @return A percent encoded string appropriate for GCS operations or nil if string is nil
+ * or can't be escaped.
+ */
++ (nullable NSString *)GCSEscapedString:(NSString *)string;
+
+/**
+ * Returns the MIME type for a file extension.
+ * Example of how to get MIME type here: http://ddeville.me/2011/12/mime-to-UTI-cocoa/
+ * @param extension A file extension such as "txt", "png", etc.
+ * @return The MIME type for the input extension such as "text/plain", "image/png", etc.
+ * or nil if no type is found.
+ */
++ (nullable NSString *)MIMETypeForExtension:(NSString *)extension;
+
+/**
+ * Returns a properly escaped query string from a given dictionary of query items to values.
+ * @param dictionary A dictionary containing query items and associated values.
+ * @return A properly escaped query string or the empty string for a nil or empty dictionary.
+ */
++ (NSString *)queryStringForDictionary:(nullable NSDictionary *)dictionary;
+
+/**
+ * Returns a base NSURLRequest used by all tasks.
+ * @param path The FIRStoragePath to create a request for.
+ * @return Returns a properly formatted NSURLRequest of the form:
+ * scheme://host/version/b/<bucket>/o[/path/to/object]
+ */
++ (NSURLRequest *)defaultRequestForPath:(FIRStoragePath *)path;
+
+/**
+ * Creates the appropriate GCS percent escaped path for a given FIRStoragePath.
+ * @param path The FIRStoragePath to encode.
+ * @return Returns the GCS encoded URL for a given FIRStoragePath.
+ */
++ (NSString *)encodedURLForPath:(FIRStoragePath *)path;
+
+@end
+
+@interface NSDictionary (FIRStorageNSDictionaryJSONHelpers)
+
+/**
+ * Returns a dictionary representation of the data in @a data.
+ * @param data NSData containing JSON data.
+ * @return An NSDictionary representation of the JSON, or nil if serialization failed.
+ */
++ (nullable instancetype)frs_dictionaryFromJSONData:(nullable NSData *)data;
+
+@end
+
+@interface NSData (FIRStorageNSDataJSONHelpers)
+
+/**
+ * Returns an NSData instance containing JSON serialized from @a dictionary.
+ * @param dictionary An NSDictionary containing only types serializable to JSON.
+ * @return An NSData object representing the binary JSON, or nil if serialization failed.
+ */
++ (nullable instancetype)frs_dataFromJSONDictionary:(nullable NSDictionary *)dictionary;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/Firebase/Storage/Private/FIRStorage_Private.h b/Firebase/Storage/Private/FIRStorage_Private.h
new file mode 100644
index 0000000..aefe808
--- /dev/null
+++ b/Firebase/Storage/Private/FIRStorage_Private.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+@class FIRApp;
+@class GTMSessionFetcherService;
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface FIRStorage ()
+
+@property(strong, nonatomic, readwrite) FIRApp *app;
+
+@property(strong, nonatomic) GTMSessionFetcherService *fetcherServiceForApp;
+
+@property(strong, nonatomic) NSString *storageBucket;
+
+/**
+ * Enables/disables GTMSessionFetcher HTTP logging
+ * @param isLoggingEnabled Boolean passed through to enable/disable GTMSessionFetcher logging
+ */
++ (void)setGTMSessionFetcherLoggingEnabled:(BOOL)isLoggingEnabled;
+
+@end
+
+NS_ASSUME_NONNULL_END