Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // DropboxAccountManage.m
- #import <Foundation/Foundation.h>
- #import "DropboxAccountManager.h"
- @implementation DropboxAccountManager
- // returns nil if no token is there, else a db
- + (DBUserClient*)getDropboxClient{
- return [DBClientsManager authorizedClient];
- }
- + (void) link:(UIViewController *)viewcontroller {
- [DBClientsManager authorizeFromController:[UIApplication sharedApplication]
- controller:viewcontroller
- openURL:^(NSURL *url) {
- [[UIApplication sharedApplication] openURL:url];
- }
- browserAuth:NO];
- }
- + (void) unlink {
- [DBClientsManager unlinkAndResetClients];
- }
- + (NSString*) getToken {
- DBAccessToken *dbAccessToken = [[DBOAuthManager sharedOAuthManager]getFirstAccessToken];
- return dbAccessToken.accessToken;
- }
- + (void) getCurrentUserInfo {
- DBUserClient *client = [DropboxAccountManager getDropboxClient];
- NSString *accessToken = [DropboxAccountManager getToken];
- if(accessToken)
- NSLog(@"DBX Token-> %@", accessToken);
- // email and name
- [[client.usersRoutes getCurrentAccount]
- setResponseBlock:^(DBUSERSFullAccount *result, DBFILESCreateFolderError *routeError, DBRequestError *error) {
- if (result) {
- NSLog(@"Email-> %@, Name-> %@\n", result.email, result.name.displayName);
- } else {
- NSLog(@"%@\n%@\n", routeError, error);
- }
- }];
- // space used
- [[client.usersRoutes getSpaceUsage]
- setResponseBlock:^(DBUSERSSpaceUsage *result, DBFILESCreateFolderError *routeError, DBRequestError *error) {
- NSNumber *spaceTotal = nil;
- NSNumber *spaceUsed = result.used;
- if(result.allocation.isIndividual){
- spaceTotal = result.allocation.individual.allocated;
- }
- if(result.allocation.isTeam){
- spaceTotal = result.allocation.team.allocated;
- }
- float percentageUsed = ([spaceUsed floatValue] * 100 / [spaceTotal floatValue] );
- float gbTotlal = ([spaceTotal floatValue] / 1000000000 );
- if (result) {
- NSLog(@"Usage-> %.02f%% of %.02f gb used.", percentageUsed, gbTotlal);
- } else {
- NSLog(@"%@\n%@\n", routeError, error);
- }
- }];
- }
- + (void) listFolder:(NSString *) path completion:(void (^)(NSArray*))listFolderFinished{
- DBUserClient *client = [DropboxAccountManager getDropboxClient];
- [[client.filesRoutes listFolder:path]
- setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderError *routeError, DBRequestError *error) {
- if (response) {
- NSArray<DBFILESMetadata *> *entries = response.entries;
- NSString *cursor = response.cursor;
- BOOL hasMore = [response.hasMore boolValue];
- if (hasMore) {
- NSLog(@"Folder is large enough where we need to call `listFolderContinue:`");
- [self listFolderContinueWithClient:client cursor:cursor array:entries completion:listFolderFinished];
- } else {
- NSLog(@"List folder complete.");
- listFolderFinished(entries);
- }
- } else {
- NSLog(@"%@\n%@\n", routeError, error);
- }
- }];
- }
- + (void)listFolderContinueWithClient:(DBUserClient *)client cursor:(NSString *)cursor array:(NSArray*)oldArray completion:(void (^)(NSArray*))listFolderFinished{
- [[client.filesRoutes listFolderContinue:cursor] setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderContinueError *routeError, DBRequestError *error) {
- if (response) {
- NSArray<DBFILESMetadata *> *entries = [response.entries arrayByAddingObjectsFromArray:oldArray];
- NSString *cursor = response.cursor;
- BOOL hasMore = [response.hasMore boolValue];
- if (hasMore) {
- [self listFolderContinueWithClient:client cursor:cursor array:entries completion:listFolderFinished];
- } else {
- listFolderFinished(entries);
- NSLog(@"List folder complete.");
- }
- } else {
- NSLog(@"%@\n%@\n", routeError, error);
- }
- }];
- }
- + (void)uploadFile:(NSString *)localPath remotePath:(NSString *)remotepath{
- DBUserClient *client = [DropboxAccountManager getDropboxClient];
- DBFILESWriteMode *mode = [[DBFILESWriteMode alloc] initWithOverwrite];
- NSData *fileData = [@"file data exampleasdsadsad" dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
- [[client.filesRoutes uploadData:remotepath mode:mode autorename:false clientModified:nil mute: false inputData:fileData]
- setResponseBlock:^(DBFILESFileMetadata *result, DBFILESUploadError *routeError, DBRequestError *error) {
- if (result) {
- NSLog(@"%@\n", result);
- } else {
- NSLog(@"%@\n%@\n", routeError, error);
- }
- }];
- }
- + (void)batchUploadFiles:(NSString *)localPath remotePath:(NSString *)remotepath {
- DBUserClient *client = [DropboxAccountManager getDropboxClient];
- DBFILESWriteMode *mode = [[DBFILESWriteMode alloc] initWithOverwrite];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *workingDirectoryName = @"/data";
- NSURL *workingDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0]
- URLByAppendingPathComponent:workingDirectoryName];
- [fileManager createDirectoryAtPath:[workingDirectory path]
- withIntermediateDirectories:YES
- attributes:nil
- error:nil];
- NSMutableDictionary<NSURL *, DBFILESCommitInfo *> *uploadFilesUrlsToCommitInfo = [NSMutableDictionary new];
- NSLog(@"\n\nCreating files in: %@\n\n", [workingDirectory path]);
- // create a bunch of fake files
- for (int i = 0; i < 10; i++) {
- NSString *fileName = [NSString stringWithFormat:@":test_file_%d", i];
- NSString *fileContent = [NSString stringWithFormat:@"%@'s content. Test content here sdsdsdsd.", fileName];
- NSURL *fileUrl = [workingDirectory URLByAppendingPathComponent:fileName];
- // set to test large file
- BOOL testLargeFile = NO;
- // don't create a file for the name test_file_5 so we use a custom large file
- // there instead
- if (i != 5 || !testLargeFile) {
- NSError *fileCreationError;
- [fileContent writeToFile:[fileUrl path] atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&fileCreationError];
- if (fileCreationError) {
- NSLog(@"Error creating file: %@", fileCreationError);
- }
- } else {
- if (![fileManager fileExistsAtPath:[fileUrl path]]) {
- NSLog(@"\n\nPlease create a large file named %@ to test chunked uploading\n\n", [fileUrl lastPathComponent]);
- }
- }
- DBFILESCommitInfo *commitInfo = [[DBFILESCommitInfo alloc] initWithPath:[NSString stringWithFormat:@"%@/%@", @"/data", fileName] mode:mode autorename:false clientModified:nil mute:false];
- [uploadFilesUrlsToCommitInfo setObject:commitInfo forKey:fileUrl];
- }
- [client.filesRoutes batchUploadFiles:uploadFilesUrlsToCommitInfo queue:nil progressBlock:^(int64_t uploaded, int64_t uploadedTotal, int64_t expectedToUploadTotal) {
- NSLog(@"Uploaded: %lld UploadedTotal: %lld ExpectedToUploadTotal: %lld", uploaded, uploadedTotal, expectedToUploadTotal);
- } responseBlock:^(NSDictionary<NSURL *,DBFILESUploadSessionFinishBatchResultEntry *> *fileUrlsToBatchResultEntries,
- DBASYNCPollError *finishBatchRouteError, DBRequestError *finishBatchRequestError,
- NSDictionary<NSURL *,DBRequestError *> *fileUrlsToRequestErrors) {
- if (fileUrlsToBatchResultEntries) {
- for (NSURL *clientSideFileUrl in fileUrlsToBatchResultEntries) {
- DBFILESUploadSessionFinishBatchResultEntry *resultEntry = fileUrlsToBatchResultEntries[clientSideFileUrl];
- if ([resultEntry isSuccess]) {
- NSString *dropboxFilePath = resultEntry.success.pathDisplay;
- NSLog(@"File successfully uploaded from %@ on local machine to %@ in Dropbox.",
- [clientSideFileUrl absoluteString], dropboxFilePath);
- } else if ([resultEntry isFailure]) {
- // This particular file was not uploaded successfully, although the other
- // files may have been uploaded successfully. Perhaps implement some retry
- // logic here based on `uploadError`
- DBRequestError *uploadNetworkError = fileUrlsToRequestErrors[clientSideFileUrl];
- DBFILESUploadSessionFinishError *uploadSessionFinishError = resultEntry.failure;
- NSLog(@"%@\n", uploadNetworkError);
- NSLog(@"%@\n", uploadSessionFinishError);
- }
- }
- } else if (finishBatchRouteError) {
- NSLog(@"Either bug in SDK code, or transient error on Dropbox server: %@", finishBatchRouteError);
- } else if (finishBatchRequestError) {
- NSLog(@"Request error from calling `/upload_session/finish_batch/check`");
- NSLog(@"%@", finishBatchRequestError);
- } else if ([fileUrlsToRequestErrors count] > 0) {
- NSLog(@"Other additional errors (e.g. file doesn't exist client-side, etc.).");
- NSLog(@"%@", fileUrlsToRequestErrors);
- }
- }];
- }
- + (void)downloadFile:(NSString *)remotePath {
- dispatch_queue_t queue_a = dispatch_queue_create("dowloadQueue", DISPATCH_QUEUE_CONCURRENT);
- for (int i = 0; i < 10; i++){
- dispatch_async(queue_a, ^{
- NSLog(@"A - %i", i);
- });
- }
- }
- + (void)printEntries:(NSArray<DBFILESMetadata *> *)entries {
- int size = [entries count];
- NSLog(@"there are %d entries in the array", size);
- /** for (DBFILESMetadata *entry in entries) {
- if ([entry isKindOfClass:[DBFILESFileMetadata class]]) {
- DBFILESFileMetadata *fileMetadata = (DBFILESFileMetadata *)entry;
- NSLog(@"File data: %@\n", fileMetadata);
- } else if ([entry isKindOfClass:[DBFILESFolderMetadata class]]) {
- DBFILESFolderMetadata *folderMetadata = (DBFILESFolderMetadata *)entry;
- NSLog(@"Folder data: %@\n", folderMetadata);
- } else if ([entry isKindOfClass:[DBFILESDeletedMetadata class]]) {
- DBFILESDeletedMetadata *deletedMetadata = (DBFILESDeletedMetadata *)entry;
- NSLog(@"Deleted data: %@\n", deletedMetadata);
- }
- }**/
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement