-
-
Notifications
You must be signed in to change notification settings - Fork 878
Added PFFileStagingController to manage staging. #49
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Copyright (c) 2015-present, Parse, LLC. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import <Bolts/BFTask.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@protocol PFFileManagerProvider; | ||
|
||
@interface PFFileStagingController : NSObject | ||
|
||
@property (nonatomic, weak, readonly) id<PFFileManagerProvider> dataSource; | ||
|
||
@property (nonatomic, copy, readonly) NSString *stagedFilesDirectoryPath; | ||
|
||
///-------------------------------------- | ||
/// @name Init | ||
///-------------------------------------- | ||
|
||
- (instancetype)init NS_UNAVAILABLE; | ||
- (instancetype)initWithDataSource:(id<PFFileManagerProvider>)dataSource NS_DESIGNATED_INITIALIZER; | ||
|
||
+ (instancetype)controllerWithDataSource:(id<PFFileManagerProvider>)dataSource; | ||
|
||
///-------------------------------------- | ||
/// @name Staging | ||
///-------------------------------------- | ||
|
||
/*! | ||
Moves a file from the specified path to the staging directory based off of the name and unique ID passed in. | ||
|
||
@param filePath The source path to stage | ||
@param name The name of the file to stage | ||
@param uniqueId A unique ID for this file to be used when differentiating between files with the same name. | ||
|
||
@return A task, which yields the path of the staged file on disk. | ||
*/ | ||
- (BFTask *)stageFileAsyncAtPath:(NSString *)filePath name:(NSString *)name uniqueId:(uint64_t)uniqueId; | ||
|
||
/*! | ||
Creates a file from the specified data and places it into the staging directory based off of the name and unique | ||
ID passed in. | ||
|
||
@param fileData The data to stage | ||
@param name The name of the file to stage | ||
@param uniqueId The unique ID for this file to be used when differentiating between files with the same name. | ||
|
||
@return A task, which yields the path of the staged file on disk. | ||
*/ | ||
- (BFTask *)stageFileAsyncWithData:(NSData *)fileData name:(NSString *)name uniqueId:(uint64_t)uniqueId; | ||
|
||
/*! | ||
Get the staged directory path for a file with the specified name and unique ID. | ||
|
||
@param name The name of the staged file | ||
@param uniqueId The unique ID of the staged file | ||
|
||
@return The path in the staged directory folder which contains the contents of the requested file. | ||
*/ | ||
- (NSString *)stagedFilePathForFileWithName:(NSString *)name uniqueId:(uint64_t)uniqueId; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* Copyright (c) 2015-present, Parse, LLC. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import "PFFileStagingController.h" | ||
|
||
#import "BFTask+Private.h" | ||
#import "PFAssert.h" | ||
#import "PFAsyncTaskQueue.h" | ||
#import "PFDataProvider.h" | ||
#import "PFFileManager.h" | ||
#import "PFLogging.h" | ||
|
||
static NSString *const PFFileStagingControllerDirectoryName_ = @"PFFileStaging"; | ||
|
||
@implementation PFFileStagingController { | ||
PFAsyncTaskQueue *_taskQueue; | ||
} | ||
|
||
///-------------------------------------- | ||
#pragma mark - Init | ||
///-------------------------------------- | ||
|
||
- (instancetype)init { | ||
PFNotDesignatedInitializer(); | ||
} | ||
|
||
- (instancetype)initWithDataSource:(id<PFFileManagerProvider>)dataSource { | ||
self = [super init]; | ||
if (!self) return nil; | ||
|
||
_dataSource = dataSource; | ||
_taskQueue = [PFAsyncTaskQueue taskQueue]; | ||
|
||
[self _clearStagedFilesAsync]; | ||
|
||
return self; | ||
} | ||
|
||
+ (instancetype)controllerWithDataSource:(id<PFFileManagerProvider>)dataSource { | ||
return [[self alloc] initWithDataSource:dataSource]; | ||
} | ||
|
||
///-------------------------------------- | ||
#pragma mark - Properties | ||
///-------------------------------------- | ||
|
||
- (NSString *)stagedFilesDirectoryPath { | ||
NSString *folderPath = [self.dataSource.fileManager parseLocalSandboxDataDirectoryPath]; | ||
return [folderPath stringByAppendingPathComponent:PFFileStagingControllerDirectoryName_]; | ||
} | ||
|
||
///-------------------------------------- | ||
#pragma mark - Staging | ||
///-------------------------------------- | ||
|
||
- (BFTask *)stageFileAsyncAtPath:(NSString *)filePath name:(NSString *)name uniqueId:(uint64_t)uniqueId { | ||
return [_taskQueue enqueue:^id(BFTask *task) { | ||
return [[PFFileManager createDirectoryIfNeededAsyncAtPath:[self stagedFilesDirectoryPath]] continueWithBlock:^id(BFTask *task) { | ||
NSString *destinationPath = [self stagedFilePathForFileWithName:name uniqueId:uniqueId]; | ||
return [[PFFileManager copyItemAsyncAtPath:filePath toPath:destinationPath] continueWithSuccessResult:destinationPath]; | ||
}]; | ||
}]; | ||
} | ||
|
||
- (BFTask *)stageFileAsyncWithData:(NSData *)fileData name:(NSString *)name uniqueId:(uint64_t)uniqueId { | ||
return [_taskQueue enqueue:^id(BFTask *task) { | ||
return [[PFFileManager createDirectoryIfNeededAsyncAtPath:[self stagedFilesDirectoryPath]] continueWithBlock:^id(BFTask *task) { | ||
NSString *destinationPath = [self stagedFilePathForFileWithName:name uniqueId:uniqueId]; | ||
return [[PFFileManager writeDataAsync:fileData toFile:destinationPath] continueWithSuccessResult:destinationPath]; | ||
}]; | ||
}]; | ||
} | ||
|
||
- (NSString *)stagedFilePathForFileWithName:(NSString *)name uniqueId:(uint64_t)uniqueId { | ||
NSString *fileName = [NSString stringWithFormat:@"%llX_%@", uniqueId, name]; | ||
return [[self stagedFilesDirectoryPath] stringByAppendingPathComponent:fileName]; | ||
} | ||
|
||
///-------------------------------------- | ||
#pragma mark - Clearing | ||
///-------------------------------------- | ||
|
||
- (BFTask *)_clearStagedFilesAsync { | ||
return [_taskQueue enqueue:^id(BFTask *task) { | ||
NSString *stagedFilesDirectoryPath = [self stagedFilesDirectoryPath]; | ||
return [PFFileManager removeItemAtPathAsync:stagedFilesDirectoryPath]; | ||
}]; | ||
} | ||
|
||
@end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supernit: You don't really need this to be inside NS_ASSUME_NONNULL, but it's ok.