Skip to content

Custom File Upload Controller #1114

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

Merged
merged 11 commits into from
Apr 7, 2017
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
48 changes: 48 additions & 0 deletions Parse.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

52 changes: 37 additions & 15 deletions Parse/Internal/File/Controller/PFFileController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#import "PFMacros.h"
#import "PFRESTFileCommand.h"
#import "PFErrorUtilities.h"
#import "Parse.h"
#import "PFFileUploadController.h"

static NSString *const PFFileControllerCacheDirectoryName_ = @"PFFileCache";

Expand Down Expand Up @@ -218,21 +220,41 @@ - (NSString *)_temporaryFileDownloadPathForFileState:(PFFileState *)fileState {
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
options:PFCommandRunningOptionRetryIfFailed
cancellationToken:cancellationToken
progressBlock:progressBlock] continueWithSuccessBlock:^id(BFTask<PFCommandResult *> *task) {
@strongify(self);
PFCommandResult *result = task.result;
PFFileState *fileState = [[PFFileState alloc] initWithName:result.result[@"name"]
urlString:result.result[@"url"]
mimeType:nil];
return [[self _cacheFileAsyncWithState:fileState atPath:sourceFilePath] continueWithSuccessResult:fileState];
}];

id<PFFileUploadController> customFileUploadController = Parse.currentConfiguration.fileUploadController;
if (customFileUploadController) {
@weakify(self);
return [[customFileUploadController uploadSourceFilePath:sourceFilePath
fileName:fileState.name
mimeType:fileState.mimeType
sessionToken:sessionToken
cancellationToken:cancellationToken
progressBlock:progressBlock]
continueWithSuccessBlock:^id(BFTask<PFFileUploadResult *> *task) {
@strongify(self);
PFFileUploadResult *result = task.result;
PFFileState *fileState = [[PFFileState alloc] initWithName:result.name
urlString:result.url
mimeType:nil];
return [[self _cacheFileAsyncWithState:fileState atPath:sourceFilePath] continueWithSuccessResult:fileState];
}];
} else {
PFRESTFileCommand *command = [PFRESTFileCommand uploadCommandForFileWithName:fileState.name sessionToken:sessionToken];
@weakify(self);
return [[self.dataSource.commandRunner runFileUploadCommandAsync:command
withContentType:fileState.mimeType
contentSourceFilePath:sourceFilePath
options:PFCommandRunningOptionRetryIfFailed
cancellationToken:cancellationToken
progressBlock:progressBlock] continueWithSuccessBlock:^id(BFTask<PFCommandResult *> *task) {
@strongify(self);
PFCommandResult *result = task.result;
PFFileState *fileState = [[PFFileState alloc] initWithName:result.result[@"name"]
urlString:result.result[@"url"]
mimeType:nil];
return [[self _cacheFileAsyncWithState:fileState atPath:sourceFilePath] continueWithSuccessResult:fileState];
}];
}
}

///--------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions Parse/Internal/File/Controller/PFFileUploadResult.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// PFFileUploadResult.m
// Parse
//
// Created by Ken Cooper on 2/21/17.
// Copyright © 2017 Parse Inc. All rights reserved.
//

#import "PFFileUploadResult.h"

@implementation PFFileUploadResult

@end
1 change: 1 addition & 0 deletions Parse/Internal/ParseClientConfiguration_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern NSString *const _ParseDefaultServerURLString;

@property (nullable, nonatomic, copy, readwrite) NSString *applicationId;
@property (nullable, nonatomic, copy, readwrite) NSString *clientKey;
@property (nullable, nonatomic, strong, readwrite) id<PFFileUploadController> fileUploadController;

@property (nonatomic, copy, readwrite) NSString *server;

Expand Down
37 changes: 37 additions & 0 deletions Parse/PFFileUploadController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// PFUploadController.h
// Parse
//
// Created by Ken Cooper on 2/20/17.
// Copyright © 2017 Parse Inc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <Bolts/BFTask.h>

/**
A policy interface for overriding the default upload behavior of uploading a PFFile
to application's parse server. Allows for direct uploads to other file storage
providers.
*/
@protocol PFFileUploadController <NSObject>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could very well have the PFRESTFileUploadController: NSObject<PFFileUploadController> what do you think?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. I originally started to go that route, but wanted to minimize touching the code base. I'll make that change. I assume you want the PFRESTFileUploadController to have a public .h?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an internal one, not really a need to be exposed. The protocol needs to be however.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will do. Few things to do first, but will get to it in a bit.


/**
Uploads a file asynchronously from file path for a given file state.

@param sourceFilePath Path to the file to upload.
@param fileName The PFFile's fileName.
@param mimeType The PFFile's mime type.
@param sessionToken The current users's session token.
@param cancellationToken Cancellation token.
@param progressBlock Progress block to call (optional).

@return `BFTask` with a success result set to `PFFileUploadResult` containing the url and name of the uploaded file.
*/
-(BFTask<PFFileUploadResult *> * _Nonnull)uploadSourceFilePath:(NSString * _Nonnull)sourceFilePath
fileName:(NSString * _Nullable)fileName
mimeType:(NSString * _Nullable)mimeType
sessionToken:(NSString * _Nonnull)sessionToken
cancellationToken:(BFCancellationToken * _Nonnull)cancellationToken
progressBlock:(PFProgressBlock _Nonnull)progressBlock;
@end
17 changes: 17 additions & 0 deletions Parse/PFFileUploadResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// PFFileUploadResult.h
// Parse
//
// Created by Ken Cooper on 2/21/17.
// Copyright © 2017 Parse Inc. All rights reserved.
//

#import <Foundation/Foundation.h>

/**
Response provided by a custom `PFFileUploadController`.
*/
@interface PFFileUploadResult : NSObject
@property (strong, nonatomic) NSString *url;
@property (strong, nonatomic) NSString *name;
@end
2 changes: 2 additions & 0 deletions Parse/Parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#import <Parse/PFUser+Synchronous.h>
#import <Parse/PFUser+Deprecated.h>
#import <Parse/PFUserAuthenticationDelegate.h>
#import <Parse/PFFileUploadResult.h>
#import <Parse/PFFileUploadController.h>

#if TARGET_OS_IOS

Expand Down
12 changes: 12 additions & 0 deletions Parse/ParseClientConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#import <Parse/PFConstants.h>

@protocol PFFileUploadController;

NS_ASSUME_NONNULL_BEGIN

/**
Expand Down Expand Up @@ -49,6 +51,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, copy) NSString *server;

/**
Sets a custom file upload controller that uploads PFFiles using its own policy.
*/
@property (nonatomic, strong, readwrite, nullable) id<PFFileUploadController> fileUploadController;

///--------------------------------------
#pragma mark - Enabling Local Datastore
///--------------------------------------
Expand Down Expand Up @@ -120,6 +127,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, copy, readonly) NSString *server;

/**
The custom upload controller that synchronously uploads PFFiles using its own policy.
*/
@property (nonatomic, strong, readonly, nullable) id<PFFileUploadController> fileUploadController;

///--------------------------------------
#pragma mark - Enabling Local Datastore
///--------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions Parse/ParseClientConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ - (BOOL)isEqual:(id)object {
return ([PFObjectUtilities isObject:self.applicationId equalToObject:other.applicationId] &&
[PFObjectUtilities isObject:self.clientKey equalToObject:other.clientKey] &&
[self.server isEqualToString:other.server] &&
self.fileUploadController == other.fileUploadController &&
self.localDatastoreEnabled == other.localDatastoreEnabled &&
[PFObjectUtilities isObject:self.applicationGroupIdentifier equalToObject:other.applicationGroupIdentifier] &&
[PFObjectUtilities isObject:self.containingApplicationBundleIdentifier equalToObject:other.containingApplicationBundleIdentifier] &&
Expand All @@ -128,6 +129,7 @@ - (instancetype)copyWithZone:(NSZone *)zone {
configuration->_applicationId = [self->_applicationId copy];
configuration->_clientKey = [self->_clientKey copy];
configuration->_server = [self.server copy];
configuration->_fileUploadController = self->_fileUploadController;
configuration->_localDatastoreEnabled = self->_localDatastoreEnabled;
configuration->_applicationGroupIdentifier = [self->_applicationGroupIdentifier copy];
configuration->_containingApplicationBundleIdentifier = [self->_containingApplicationBundleIdentifier copy];
Expand Down