Skip to content

Commit 383897f

Browse files
committed
Added an ability to download a file and get a file path to PFFiles.
1 parent fe978af commit 383897f

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

Parse/PFConstants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ typedef void (^PFSetResultBlock)(NSSet *PF_NULLABLE_S channels, NSError *PF_NULL
381381
typedef void (^PFUserResultBlock)(PFUser *PF_NULLABLE_S user, NSError *PF_NULLABLE_S error);
382382
typedef void (^PFDataResultBlock)(NSData *PF_NULLABLE_S data, NSError *PF_NULLABLE_S error);
383383
typedef void (^PFDataStreamResultBlock)(NSInputStream *PF_NULLABLE_S stream, NSError *PF_NULLABLE_S error);
384+
typedef void (^PFFilePathResultBlock)(NSString *PF_NULLABLE_S filePath, NSError *PF_NULLABLE_S error);
384385
typedef void (^PFStringResultBlock)(NSString *PF_NULLABLE_S string, NSError *PF_NULLABLE_S error);
385386
typedef void (^PFIdResultBlock)(PF_NULLABLE_S id object, NSError *PF_NULLABLE_S error);
386387
typedef void (^PFProgressBlock)(int percentDone);

Parse/PFFile.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,54 @@ PF_ASSUME_NONNULL_BEGIN
374374
*/
375375
- (void)getDataInBackgroundWithTarget:(PF_NULLABLE_S id)target selector:(PF_NULLABLE_S SEL)selector;
376376

377+
/*!
378+
@abstract *Asynchronously* gets the file path for file from cache if available or fetches its contents from the network.
379+
380+
@note The file path may change between versions of SDK.
381+
@note If you overwrite the contents of the file at returned path it will persist those change
382+
until the file cache is cleared.
383+
384+
@returns The task, with the result set to `NSString` representation of a file path.
385+
*/
386+
- (BFTask PF_GENERIC(NSString *) *)getFilePathInBackground;
387+
388+
/*!
389+
@abstract *Asynchronously* gets the file path for file from cache if available or fetches its contents from the network.
390+
391+
@note The file path may change between versions of SDK.
392+
@note If you overwrite the contents of the file at returned path it will persist those change
393+
until the file cache is cleared.
394+
395+
@param progressBlock The block should have the following argument signature: `^(int percentDone)`.
396+
397+
@returns The task, with the result set to `NSString` representation of a file path.
398+
*/
399+
- (BFTask PF_GENERIC(NSString *) *)getFilePathInBackgroundWithProgressBlock:(PF_NULLABLE PFProgressBlock)progressBlock;
400+
401+
/*!
402+
@abstract *Asynchronously* gets the file path for file from cache if available or fetches its contents from the network.
403+
404+
@note The file path may change between versions of SDK.
405+
@note If you overwrite the contents of the file at returned path it will persist those change
406+
until the file cache is cleared.
407+
408+
@param block The block should have the following argument signature: `^(NSString *filePath, NSError *error)`.
409+
*/
410+
- (void)getFilePathInBackgroundWithBlock:(PF_NULLABLE PFFilePathResultBlock)block;
411+
412+
/*!
413+
@abstract *Asynchronously* gets the file path for file from cache if available or fetches its contents from the network.
414+
415+
@note The file path may change between versions of SDK.
416+
@note If you overwrite the contents of the file at returned path it will persist those change
417+
until the file cache is cleared.
418+
419+
@param block The block should have the following argument signature: `^(NSString *filePath, NSError *error)`.
420+
@param progressBlock The block should have the following argument signature: `^(int percentDone)`.
421+
*/
422+
- (void)getFilePathInBackgroundWithBlock:(PF_NULLABLE PFFilePathResultBlock)block
423+
progressBlock:(PF_NULLABLE PFProgressBlock)progressBlock;
424+
377425
///--------------------------------------
378426
/// @name Interrupting a Transfer
379427
///--------------------------------------

Parse/PFFile.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,28 @@ - (void)getDataInBackgroundWithTarget:(id)target selector:(SEL)selector {
213213
}];
214214
}
215215

216+
- (BFTask PF_GENERIC(NSString *) *)getFilePathInBackground {
217+
return [self getFilePathInBackgroundWithProgressBlock:nil];
218+
}
219+
220+
- (BFTask PF_GENERIC(NSString *)*)getFilePathInBackgroundWithProgressBlock:(PFProgressBlock)progressBlock {
221+
return [[self _downloadAsyncWithProgressBlock:progressBlock] continueWithSuccessBlock:^id(BFTask *task) {
222+
if (self.dirty) {
223+
return self.stagedFilePath;
224+
}
225+
return [[[self class] fileController] cachedFilePathForFileState:self.state];
226+
}];
227+
}
228+
229+
- (void)getFilePathInBackgroundWithBlock:(PF_NULLABLE PFFilePathResultBlock)block {
230+
[[self getFilePathInBackground] thenCallBackOnMainThreadAsync:block];
231+
}
232+
233+
- (void)getFilePathInBackgroundWithBlock:(PF_NULLABLE PFFilePathResultBlock)block
234+
progressBlock:(PF_NULLABLE PFProgressBlock)progressBlock {
235+
[[self getFilePathInBackgroundWithProgressBlock:progressBlock] thenCallBackOnMainThreadAsync:block];
236+
}
237+
216238
#pragma mark Interrupting
217239

218240
- (void)cancel {

Tests/Unit/FileUnitTests.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,42 @@ - (void)testDownloading{
415415
return nil;
416416
}];
417417

418+
wait_next;
419+
expectation = [self expectationForSelector:@selector(getFilePathInBackground)];
420+
[[file getFilePathInBackground] continueWithBlock:^id(BFTask *task) {
421+
NSData *data = [NSData dataWithContentsOfFile:task.result];
422+
XCTAssertEqualObjects(data, expectedData);
423+
[expectation fulfill];
424+
return nil;
425+
}];
426+
427+
wait_next;
428+
expectation = [self expectationForSelector:@selector(getFilePathInBackgroundWithProgressBlock:)];
429+
[[file getFilePathInBackgroundWithProgressBlock:[self progressValidationBlock]] continueWithBlock:^id(BFTask *task) {
430+
NSData *data = [NSData dataWithContentsOfFile:task.result];
431+
XCTAssertEqualObjects(data, expectedData);
432+
[expectation fulfill];
433+
return nil;
434+
}];
435+
436+
wait_next;
437+
expectation = [self expectationForSelector:@selector(getFilePathInBackgroundWithBlock:)];
438+
[file getFilePathInBackgroundWithBlock:^(NSString *filePath, NSError *error) {
439+
XCTAssertNil(error);
440+
NSData *data = [NSData dataWithContentsOfFile:filePath];
441+
XCTAssertEqualObjects(data, expectedData);
442+
[expectation fulfill];
443+
}];
444+
445+
wait_next;
446+
expectation = [self expectationForSelector:@selector(getFilePathInBackgroundWithBlock:progressBlock:)];
447+
[file getFilePathInBackgroundWithBlock:^(NSString *filePath, NSError *error) {
448+
XCTAssertNil(error);
449+
NSData *data = [NSData dataWithContentsOfFile:filePath];
450+
XCTAssertEqualObjects(data, expectedData);
451+
[expectation fulfill];
452+
} progressBlock:[self progressValidationBlock]];
453+
418454
wait_next;
419455
}
420456

0 commit comments

Comments
 (0)