Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on trying to getData/save a PFFile without local data. #561

Merged
merged 1 commit into from
Nov 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions Parse/Internal/File/Controller/PFFileController.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "PFHash.h"
#import "PFMacros.h"
#import "PFRESTFileCommand.h"
#import "PFErrorUtilities.h"

static NSString *const PFFileControllerCacheDirectoryName_ = @"PFFileCache";

Expand Down Expand Up @@ -90,6 +91,11 @@ - (BFTask *)downloadFileAsyncWithState:(PFFileState *)fileState
if (cancellationToken.cancellationRequested) {
return [BFTask cancelledTask];
}
if (!fileState.secureURLString) {
NSError *error = [PFErrorUtilities errorWithCode:kPFErrorUnsavedFile
message:@"Can't download a file that doesn't exist on the server or locally."];
return [BFTask taskWithError:error];
}

@weakify(self);
return [BFTask taskFromExecutor:[BFExecutor defaultPriorityBackgroundExecutor] withBlock:^id{
Expand Down Expand Up @@ -208,13 +214,17 @@ - (BFTask *)uploadFileAsyncWithState:(PFFileState *)fileState
sessionToken:(NSString *)sessionToken
cancellationToken:(BFCancellationToken *)cancellationToken
progressBlock:(PFProgressBlock)progressBlock {
PFRESTFileCommand *command = [PFRESTFileCommand uploadCommandForFileWithName:fileState.name
sessionToken:sessionToken];

@weakify(self);
if (cancellationToken.cancellationRequested) {
return [BFTask cancelledTask];
}
if (!sourceFilePath) {
NSError *error = [PFErrorUtilities errorWithCode:kPFErrorUnsavedFile
message:@"Can't upload a file that doesn't exist locally."];
return [BFTask taskWithError:error];
}

PFRESTFileCommand *command = [PFRESTFileCommand uploadCommandForFileWithName:fileState.name sessionToken:sessionToken];
@weakify(self);
return [[[self.dataSource.commandRunner runFileUploadCommandAsync:command
withContentType:fileState.mimeType
contentSourceFilePath:sourceFilePath
Expand Down
12 changes: 9 additions & 3 deletions Parse/PFFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,9 @@ - (BFTask *)_uploadFileAsyncWithSessionToken:(NSString *)sessionToken
}

PFFileController *controller = [[self class] fileController];
NSString *sourceFilePath = self.stagedFilePath;
@weakify(self);
return [[[controller uploadFileAsyncWithState:[self _fileState]
sourceFilePath:sourceFilePath
sourceFilePath:self.stagedFilePath
sessionToken:sessionToken
cancellationToken:cancellationToken
progressBlock:progressBlock] continueWithSuccessBlock:^id(BFTask *task) {
Expand Down Expand Up @@ -441,11 +440,17 @@ - (NSString *)_cachedFilePath {

- (NSData *)_cachedData {
NSString *filePath = (self.dirty ? self.stagedFilePath : [self _cachedFilePath]);
if (!filePath) {
return nil;
}
return [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:NULL];
}

- (NSInputStream *)_cachedDataStream {
NSString *filePath = (self.dirty ? self.stagedFilePath : [[[self class] fileController] cachedFilePathForFileState:self.state]);
if (!filePath) {
return nil;
}
return [NSInputStream inputStreamWithFileAtPath:filePath];
}

Expand Down Expand Up @@ -504,7 +509,8 @@ - (BOOL)isDirty {
- (BOOL)isDataAvailable {
__block BOOL available = NO;
[self _performDataAccessBlock:^{
available = self.dirty || [[NSFileManager defaultManager] fileExistsAtPath:[self _cachedFilePath]];
available = ((self.dirty && self.stagedFilePath) ||
(self.url && [[NSFileManager defaultManager] fileExistsAtPath:[self _cachedFilePath]]));
}];
return available;
}
Expand Down