diff --git a/Parse/Internal/File/Controller/PFFileController.m b/Parse/Internal/File/Controller/PFFileController.m index d9b2a5174..1b59f42db 100644 --- a/Parse/Internal/File/Controller/PFFileController.m +++ b/Parse/Internal/File/Controller/PFFileController.m @@ -23,6 +23,7 @@ #import "PFHash.h" #import "PFMacros.h" #import "PFRESTFileCommand.h" +#import "PFErrorUtilities.h" static NSString *const PFFileControllerCacheDirectoryName_ = @"PFFileCache"; @@ -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{ @@ -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 diff --git a/Parse/PFFile.m b/Parse/PFFile.m index 342318064..fc9a83c11 100644 --- a/Parse/PFFile.m +++ b/Parse/PFFile.m @@ -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) { @@ -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]; } @@ -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; }