aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Storage/FIRStorageDownloadTask.m
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/FIRStorageDownloadTask.m
parent32461366c9e204a527ca05e6e9b9404a2454ac51 (diff)
Initial
Diffstat (limited to 'Firebase/Storage/FIRStorageDownloadTask.m')
-rw-r--r--Firebase/Storage/FIRStorageDownloadTask.m162
1 files changed, 162 insertions, 0 deletions
diff --git a/Firebase/Storage/FIRStorageDownloadTask.m b/Firebase/Storage/FIRStorageDownloadTask.m
new file mode 100644
index 0000000..0d71e52
--- /dev/null
+++ b/Firebase/Storage/FIRStorageDownloadTask.m
@@ -0,0 +1,162 @@
+// 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 "FIRStorageDownloadTask.h"
+
+#import "FIRStorageConstants_Private.h"
+#import "FIRStorageDownloadTask_Private.h"
+#import "FIRStorageObservableTask_Private.h"
+#import "FIRStorageTask_Private.h"
+
+@implementation FIRStorageDownloadTask
+
+@synthesize progress = _progress;
+@synthesize fetcher = _fetcher;
+
+- (instancetype)initWithReference:(FIRStorageReference *)reference
+ fetcherService:(GTMSessionFetcherService *)service
+ file:(nullable NSURL *)fileURL {
+ self = [super initWithReference:reference fetcherService:service];
+ if (self) {
+ _fileURL = [fileURL copy];
+ _progress = [NSProgress progressWithTotalUnitCount:0];
+ }
+ return self;
+}
+
+- (void)enqueue {
+ [self enqueueWithData:nil];
+}
+
+- (void)enqueueWithData:(nullable NSData *)resumeData {
+ NSAssert([NSThread isMainThread], @"Download attempting to execute on non main queue! Please "
+ @"only execute this method on the main queue.");
+ self.state = FIRStorageTaskStateQueueing;
+ NSMutableURLRequest *request = [self.baseRequest mutableCopy];
+ request.HTTPMethod = @"GET";
+ request.timeoutInterval = self.reference.storage.maxDownloadRetryTime;
+ NSURLComponents *components =
+ [NSURLComponents componentsWithURL:request.URL resolvingAgainstBaseURL:NO];
+ [components setQuery:@"alt=media"];
+ request.URL = components.URL;
+
+ GTMSessionFetcher *fetcher;
+ if (resumeData) {
+ fetcher = [GTMSessionFetcher fetcherWithDownloadResumeData:resumeData];
+ fetcher.comment = @"Resuming DownloadTask";
+ } else {
+ fetcher = [self.fetcherService fetcherWithRequest:request];
+ fetcher.comment = @"Starting DownloadTask";
+ }
+
+ [fetcher setResumeDataBlock:^(NSData *data) {
+ if (data) {
+ _downloadData = data;
+ }
+ }];
+
+ fetcher.maxRetryInterval = self.reference.storage.maxDownloadRetryTime;
+
+ if (_fileURL) {
+ // Handle file downloads
+ [fetcher setDestinationFileURL:_fileURL];
+ [fetcher setDownloadProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten,
+ int64_t totalBytesExpectedToWrite) {
+ self.state = FIRStorageTaskStateProgress;
+ self.progress.completedUnitCount = totalBytesWritten;
+ self.progress.totalUnitCount = totalBytesExpectedToWrite;
+ FIRStorageTaskSnapshot *snapshot = self.snapshot;
+ [self fireHandlersForStatus:FIRStorageTaskStatusProgress snapshot:snapshot];
+ self.state = FIRStorageTaskStateRunning;
+ }];
+ } else {
+ // Handle data downloads
+ [fetcher setReceivedProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten) {
+ self.state = FIRStorageTaskStateProgress;
+ self.progress.completedUnitCount = totalBytesWritten;
+ int64_t totalLength = [[self.fetcher response] expectedContentLength];
+ self.progress.totalUnitCount = totalLength;
+ FIRStorageTaskSnapshot *snapshot = self.snapshot;
+ [self fireHandlersForStatus:FIRStorageTaskStatusProgress snapshot:snapshot];
+ self.state = FIRStorageTaskStateRunning;
+ }];
+ }
+
+ _fetcher = fetcher;
+
+ self.state = FIRStorageTaskStateRunning;
+ [self.fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
+ // Fire last progress updates
+ [self fireHandlersForStatus:FIRStorageTaskStatusProgress snapshot:self.snapshot];
+
+ // Handle potential issues with download
+ if (error) {
+ self.state = FIRStorageTaskStateFailed;
+ self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
+ [self fireHandlersForStatus:FIRStorageTaskStatusFailure snapshot:self.snapshot];
+ [self removeAllObservers];
+ return;
+ }
+
+ // Download completed successfully, fire completion callbacks
+ self.state = FIRStorageTaskStateSuccess;
+
+ if (data) {
+ _downloadData = data;
+ }
+
+ [self fireHandlersForStatus:FIRStorageTaskStatusSuccess snapshot:self.snapshot];
+ [self removeAllObservers];
+ }];
+}
+
+#pragma mark - Download Management
+
+- (void)cancel {
+ NSError *error = [FIRStorageErrors errorWithCode:FIRStorageErrorCodeCancelled];
+ [self cancelWithError:error];
+}
+
+- (void)cancelWithError:(NSError *)error {
+ NSAssert([NSThread isMainThread], @"Cancel attempting to execute on non main queue! Please only "
+ @"execute this method on the main queue.");
+ self.state = FIRStorageTaskStateCancelled;
+ [self.fetcher stopFetching];
+ self.error = error;
+ [self fireHandlersForStatus:FIRStorageTaskStatusFailure snapshot:self.snapshot];
+}
+
+- (void)pause {
+ NSAssert([NSThread isMainThread], @"Pause attempting to execute on non main queue! Please only "
+ @"execute this method on the main queue.");
+ self.state = FIRStorageTaskStatePausing;
+ [self.fetcher stopFetching];
+ // Give the resume callback a chance to run (if scheduled)
+ [self.fetcher waitForCompletionWithTimeout:0.001];
+ self.state = FIRStorageTaskStatePaused;
+ FIRStorageTaskSnapshot *snapshot = self.snapshot;
+ [self fireHandlersForStatus:FIRStorageTaskStatusPause snapshot:snapshot];
+}
+
+- (void)resume {
+ NSAssert([NSThread isMainThread], @"Resume attempting to execute on non main queue! Please only "
+ @"execute this method on the main queue.");
+ self.state = FIRStorageTaskStateResuming;
+ FIRStorageTaskSnapshot *snapshot = self.snapshot;
+ [self fireHandlersForStatus:FIRStorageTaskStatusResume snapshot:snapshot];
+ self.state = FIRStorageTaskStateRunning;
+ [self enqueueWithData:_downloadData];
+}
+
+@end