2017-05-15 12:27:07 -07:00
|
|
|
// 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.
|
|
|
|
|
|
2020-07-31 07:22:41 -07:00
|
|
|
#import "FirebaseStorage/Sources/Public/FirebaseStorage/FIRStorageDownloadTask.h"
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2020-01-03 07:22:33 -08:00
|
|
|
#import "FirebaseStorage/Sources/FIRStorageConstants_Private.h"
|
|
|
|
|
#import "FirebaseStorage/Sources/FIRStorageDownloadTask_Private.h"
|
|
|
|
|
#import "FirebaseStorage/Sources/FIRStorageObservableTask_Private.h"
|
|
|
|
|
#import "FirebaseStorage/Sources/FIRStorageTask_Private.h"
|
2020-10-20 16:59:15 -07:00
|
|
|
#import "FirebaseStorage/Sources/FIRStorage_Private.h"
|
2017-05-15 12:27:07 -07:00
|
|
|
|
|
|
|
|
@implementation FIRStorageDownloadTask
|
|
|
|
|
|
|
|
|
|
@synthesize progress = _progress;
|
|
|
|
|
@synthesize fetcher = _fetcher;
|
2017-06-15 09:43:31 -07:00
|
|
|
@synthesize fetcherCompletion = _fetcherCompletion;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
|
|
|
|
- (instancetype)initWithReference:(FIRStorageReference *)reference
|
|
|
|
|
fetcherService:(GTMSessionFetcherService *)service
|
2018-10-23 12:15:11 -07:00
|
|
|
dispatchQueue:(dispatch_queue_t)queue
|
2017-05-15 12:27:07 -07:00
|
|
|
file:(nullable NSURL *)fileURL {
|
2018-10-23 12:15:11 -07:00
|
|
|
self = [super initWithReference:reference fetcherService:service dispatchQueue:queue];
|
2017-05-15 12:27:07 -07:00
|
|
|
if (self) {
|
|
|
|
|
_fileURL = [fileURL copy];
|
|
|
|
|
_progress = [NSProgress progressWithTotalUnitCount:0];
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 14:43:53 -08:00
|
|
|
- (void)dealloc {
|
2017-06-15 09:43:31 -07:00
|
|
|
[_fetcher stopFetching];
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
- (void)enqueue {
|
|
|
|
|
[self enqueueWithData:nil];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)enqueueWithData:(nullable NSData *)resumeData {
|
2017-06-15 09:43:31 -07:00
|
|
|
__weak FIRStorageDownloadTask *weakSelf = self;
|
|
|
|
|
|
2018-10-23 12:15:11 -07:00
|
|
|
[self dispatchAsync:^() {
|
|
|
|
|
FIRStorageDownloadTask *strongSelf = weakSelf;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2018-10-23 12:15:11 -07:00
|
|
|
if (!strongSelf) {
|
2018-08-24 12:22:26 -07:00
|
|
|
return;
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
2018-08-24 12:22:26 -07:00
|
|
|
|
2018-10-23 12:15:11 -07:00
|
|
|
strongSelf.state = FIRStorageTaskStateQueueing;
|
|
|
|
|
NSMutableURLRequest *request = [strongSelf.baseRequest mutableCopy];
|
|
|
|
|
request.HTTPMethod = @"GET";
|
|
|
|
|
request.timeoutInterval = strongSelf.reference.storage.maxDownloadRetryTime;
|
2019-01-02 09:27:31 -08:00
|
|
|
NSURLComponents *components = [NSURLComponents componentsWithURL:request.URL
|
|
|
|
|
resolvingAgainstBaseURL:NO];
|
2018-10-23 12:15:11 -07:00
|
|
|
[components setQuery:@"alt=media"];
|
|
|
|
|
request.URL = components.URL;
|
2018-08-24 12:22:26 -07:00
|
|
|
|
2018-10-23 12:15:11 -07:00
|
|
|
GTMSessionFetcher *fetcher;
|
|
|
|
|
if (resumeData) {
|
|
|
|
|
fetcher = [GTMSessionFetcher fetcherWithDownloadResumeData:resumeData];
|
|
|
|
|
fetcher.comment = @"Resuming DownloadTask";
|
|
|
|
|
} else {
|
|
|
|
|
fetcher = [strongSelf.fetcherService fetcherWithRequest:request];
|
|
|
|
|
fetcher.comment = @"Starting DownloadTask";
|
2018-08-24 12:22:26 -07:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 12:15:11 -07:00
|
|
|
[fetcher setResumeDataBlock:^(NSData *data) {
|
|
|
|
|
FIRStorageDownloadTask *strong = weakSelf;
|
|
|
|
|
if (strong && data) {
|
|
|
|
|
strong->_downloadData = data;
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
|
2020-10-20 16:59:15 -07:00
|
|
|
fetcher.maxRetryInterval = strongSelf.reference.storage.maxDownloadRetryInterval;
|
2018-10-23 12:15:11 -07:00
|
|
|
|
|
|
|
|
if (strongSelf->_fileURL) {
|
|
|
|
|
// Handle file downloads
|
|
|
|
|
[fetcher setDestinationFileURL:strongSelf->_fileURL];
|
|
|
|
|
[fetcher setDownloadProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten,
|
|
|
|
|
int64_t totalBytesExpectedToWrite) {
|
|
|
|
|
weakSelf.state = FIRStorageTaskStateProgress;
|
|
|
|
|
weakSelf.progress.completedUnitCount = totalBytesWritten;
|
|
|
|
|
weakSelf.progress.totalUnitCount = totalBytesExpectedToWrite;
|
|
|
|
|
FIRStorageTaskSnapshot *snapshot = weakSelf.snapshot;
|
|
|
|
|
[weakSelf fireHandlersForStatus:FIRStorageTaskStatusProgress snapshot:snapshot];
|
|
|
|
|
weakSelf.state = FIRStorageTaskStateRunning;
|
|
|
|
|
}];
|
|
|
|
|
} else {
|
|
|
|
|
// Handle data downloads
|
|
|
|
|
[fetcher setReceivedProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten) {
|
|
|
|
|
weakSelf.state = FIRStorageTaskStateProgress;
|
|
|
|
|
weakSelf.progress.completedUnitCount = totalBytesWritten;
|
|
|
|
|
int64_t totalLength = [[weakSelf.fetcher response] expectedContentLength];
|
|
|
|
|
weakSelf.progress.totalUnitCount = totalLength;
|
|
|
|
|
FIRStorageTaskSnapshot *snapshot = weakSelf.snapshot;
|
|
|
|
|
[weakSelf fireHandlersForStatus:FIRStorageTaskStatusProgress snapshot:snapshot];
|
|
|
|
|
weakSelf.state = FIRStorageTaskStateRunning;
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strongSelf->_fetcher = fetcher;
|
|
|
|
|
strongSelf->_fetcherCompletion = ^(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];
|
|
|
|
|
self->_fetcherCompletion = nil;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Download completed successfully, fire completion callbacks
|
|
|
|
|
self.state = FIRStorageTaskStateSuccess;
|
|
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
|
self->_downloadData = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[self fireHandlersForStatus:FIRStorageTaskStatusSuccess snapshot:self.snapshot];
|
|
|
|
|
[self removeAllObservers];
|
|
|
|
|
self->_fetcherCompletion = nil;
|
|
|
|
|
};
|
2017-06-15 09:43:31 -07:00
|
|
|
|
2018-10-23 12:15:11 -07:00
|
|
|
strongSelf.state = FIRStorageTaskStateRunning;
|
|
|
|
|
[strongSelf.fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
|
2020-11-30 12:16:50 -08:00
|
|
|
FIRStorageDownloadTask *strongSelf = weakSelf;
|
|
|
|
|
if (strongSelf.fetcherCompletion) {
|
|
|
|
|
strongSelf.fetcherCompletion(data, error);
|
|
|
|
|
}
|
2018-10-23 12:15:11 -07:00
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Download Management
|
|
|
|
|
|
|
|
|
|
- (void)cancel {
|
|
|
|
|
NSError *error = [FIRStorageErrors errorWithCode:FIRStorageErrorCodeCancelled];
|
|
|
|
|
[self cancelWithError:error];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)cancelWithError:(NSError *)error {
|
2018-10-23 12:15:11 -07:00
|
|
|
__weak FIRStorageDownloadTask *weakSelf = self;
|
|
|
|
|
[self dispatchAsync:^() {
|
|
|
|
|
weakSelf.state = FIRStorageTaskStateCancelled;
|
|
|
|
|
[weakSelf.fetcher stopFetching];
|
|
|
|
|
weakSelf.error = error;
|
|
|
|
|
[weakSelf fireHandlersForStatus:FIRStorageTaskStatusFailure snapshot:weakSelf.snapshot];
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)pause {
|
2018-10-23 12:15:11 -07:00
|
|
|
__weak FIRStorageDownloadTask *weakSelf = self;
|
|
|
|
|
[self dispatchAsync:^() {
|
|
|
|
|
weakSelf.state = FIRStorageTaskStatePausing;
|
|
|
|
|
[weakSelf.fetcher stopFetching];
|
|
|
|
|
// Give the resume callback a chance to run (if scheduled)
|
|
|
|
|
[weakSelf.fetcher waitForCompletionWithTimeout:0.001];
|
|
|
|
|
weakSelf.state = FIRStorageTaskStatePaused;
|
|
|
|
|
FIRStorageTaskSnapshot *snapshot = weakSelf.snapshot;
|
|
|
|
|
[weakSelf fireHandlersForStatus:FIRStorageTaskStatusPause snapshot:snapshot];
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)resume {
|
2018-10-23 12:15:11 -07:00
|
|
|
__weak FIRStorageDownloadTask *weakSelf = self;
|
|
|
|
|
[self dispatchAsync:^() {
|
|
|
|
|
weakSelf.state = FIRStorageTaskStateResuming;
|
|
|
|
|
FIRStorageTaskSnapshot *snapshot = weakSelf.snapshot;
|
|
|
|
|
[weakSelf fireHandlersForStatus:FIRStorageTaskStatusResume snapshot:snapshot];
|
|
|
|
|
weakSelf.state = FIRStorageTaskStateRunning;
|
|
|
|
|
[weakSelf enqueueWithData:weakSelf.downloadData];
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|