Skip to content

Commit d8b2adb

Browse files
committed
Fix crash on trying to getData/save a PFFile without local data.
1 parent ab13cc2 commit d8b2adb

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Parse/Internal/File/Controller/PFFileController.m

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#import "PFHash.h"
2424
#import "PFMacros.h"
2525
#import "PFRESTFileCommand.h"
26+
#import "PFErrorUtilities.h"
2627

2728
static NSString *const PFFileControllerCacheDirectoryName_ = @"PFFileCache";
2829

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

94100
@weakify(self);
95101
return [BFTask taskFromExecutor:[BFExecutor defaultPriorityBackgroundExecutor] withBlock:^id{
@@ -208,13 +214,17 @@ - (BFTask *)uploadFileAsyncWithState:(PFFileState *)fileState
208214
sessionToken:(NSString *)sessionToken
209215
cancellationToken:(BFCancellationToken *)cancellationToken
210216
progressBlock:(PFProgressBlock)progressBlock {
211-
PFRESTFileCommand *command = [PFRESTFileCommand uploadCommandForFileWithName:fileState.name
212-
sessionToken:sessionToken];
213-
214-
@weakify(self);
215217
if (cancellationToken.cancellationRequested) {
216218
return [BFTask cancelledTask];
217219
}
220+
if (!sourceFilePath) {
221+
NSError *error = [PFErrorUtilities errorWithCode:kPFErrorUnsavedFile
222+
message:@"Can't upload a file that doesn't exist locally."];
223+
return [BFTask taskWithError:error];
224+
}
225+
226+
PFRESTFileCommand *command = [PFRESTFileCommand uploadCommandForFileWithName:fileState.name sessionToken:sessionToken];
227+
@weakify(self);
218228
return [[[self.dataSource.commandRunner runFileUploadCommandAsync:command
219229
withContentType:fileState.mimeType
220230
contentSourceFilePath:sourceFilePath

Parse/PFFile.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,9 @@ - (BFTask *)_uploadFileAsyncWithSessionToken:(NSString *)sessionToken
340340
}
341341

342342
PFFileController *controller = [[self class] fileController];
343-
NSString *sourceFilePath = self.stagedFilePath;
344343
@weakify(self);
345344
return [[[controller uploadFileAsyncWithState:[self _fileState]
346-
sourceFilePath:sourceFilePath
345+
sourceFilePath:self.stagedFilePath
347346
sessionToken:sessionToken
348347
cancellationToken:cancellationToken
349348
progressBlock:progressBlock] continueWithSuccessBlock:^id(BFTask *task) {
@@ -441,11 +440,17 @@ - (NSString *)_cachedFilePath {
441440

442441
- (NSData *)_cachedData {
443442
NSString *filePath = (self.dirty ? self.stagedFilePath : [self _cachedFilePath]);
443+
if (!filePath) {
444+
return nil;
445+
}
444446
return [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:NULL];
445447
}
446448

447449
- (NSInputStream *)_cachedDataStream {
448450
NSString *filePath = (self.dirty ? self.stagedFilePath : [[[self class] fileController] cachedFilePathForFileState:self.state]);
451+
if (!filePath) {
452+
return nil;
453+
}
449454
return [NSInputStream inputStreamWithFileAtPath:filePath];
450455
}
451456

@@ -504,7 +509,8 @@ - (BOOL)isDirty {
504509
- (BOOL)isDataAvailable {
505510
__block BOOL available = NO;
506511
[self _performDataAccessBlock:^{
507-
available = self.dirty || [[NSFileManager defaultManager] fileExistsAtPath:[self _cachedFilePath]];
512+
available = ((self.dirty && self.stagedFilePath) ||
513+
(self.url && [[NSFileManager defaultManager] fileExistsAtPath:[self _cachedFilePath]]));
508514
}];
509515
return available;
510516
}

0 commit comments

Comments
 (0)