Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 KB | None | 0 0
  1. //
  2. // DropboxAccountManage.m
  3.  
  4. #import <Foundation/Foundation.h>
  5. #import "DropboxAccountManager.h"
  6.  
  7.  
  8.  
  9. @implementation DropboxAccountManager
  10.  
  11.  
  12. // returns nil if no token is there, else a db
  13. + (DBUserClient*)getDropboxClient{
  14. return [DBClientsManager authorizedClient];
  15. }
  16.  
  17. + (void) link:(UIViewController *)viewcontroller {
  18. [DBClientsManager authorizeFromController:[UIApplication sharedApplication]
  19. controller:viewcontroller
  20. openURL:^(NSURL *url) {
  21. [[UIApplication sharedApplication] openURL:url];
  22. }
  23. browserAuth:NO];
  24. }
  25.  
  26.  
  27. + (void) unlink {
  28. [DBClientsManager unlinkAndResetClients];
  29. }
  30.  
  31.  
  32. + (NSString*) getToken {
  33. DBAccessToken *dbAccessToken = [[DBOAuthManager sharedOAuthManager]getFirstAccessToken];
  34. return dbAccessToken.accessToken;
  35. }
  36.  
  37. + (void) getCurrentUserInfo {
  38.  
  39. DBUserClient *client = [DropboxAccountManager getDropboxClient];
  40. NSString *accessToken = [DropboxAccountManager getToken];
  41. if(accessToken)
  42. NSLog(@"DBX Token-> %@", accessToken);
  43.  
  44. // email and name
  45. [[client.usersRoutes getCurrentAccount]
  46. setResponseBlock:^(DBUSERSFullAccount *result, DBFILESCreateFolderError *routeError, DBRequestError *error) {
  47. if (result) {
  48. NSLog(@"Email-> %@, Name-> %@\n", result.email, result.name.displayName);
  49. } else {
  50. NSLog(@"%@\n%@\n", routeError, error);
  51. }
  52. }];
  53.  
  54. // space used
  55. [[client.usersRoutes getSpaceUsage]
  56. setResponseBlock:^(DBUSERSSpaceUsage *result, DBFILESCreateFolderError *routeError, DBRequestError *error) {
  57.  
  58. NSNumber *spaceTotal = nil;
  59. NSNumber *spaceUsed = result.used;
  60. if(result.allocation.isIndividual){
  61. spaceTotal = result.allocation.individual.allocated;
  62. }
  63. if(result.allocation.isTeam){
  64. spaceTotal = result.allocation.team.allocated;
  65. }
  66.  
  67. float percentageUsed = ([spaceUsed floatValue] * 100 / [spaceTotal floatValue] );
  68. float gbTotlal = ([spaceTotal floatValue] / 1000000000 );
  69. if (result) {
  70. NSLog(@"Usage-> %.02f%% of %.02f gb used.", percentageUsed, gbTotlal);
  71. } else {
  72. NSLog(@"%@\n%@\n", routeError, error);
  73. }
  74. }];
  75.  
  76. }
  77.  
  78.  
  79. + (void) listFolder:(NSString *) path completion:(void (^)(NSArray*))listFolderFinished{
  80.  
  81. DBUserClient *client = [DropboxAccountManager getDropboxClient];
  82.  
  83. [[client.filesRoutes listFolder:path]
  84. setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderError *routeError, DBRequestError *error) {
  85. if (response) {
  86. NSArray<DBFILESMetadata *> *entries = response.entries;
  87. NSString *cursor = response.cursor;
  88. BOOL hasMore = [response.hasMore boolValue];
  89.  
  90. if (hasMore) {
  91. NSLog(@"Folder is large enough where we need to call `listFolderContinue:`");
  92. [self listFolderContinueWithClient:client cursor:cursor array:entries completion:listFolderFinished];
  93. } else {
  94.  
  95. NSLog(@"List folder complete.");
  96. listFolderFinished(entries);
  97.  
  98. }
  99. } else {
  100. NSLog(@"%@\n%@\n", routeError, error);
  101. }
  102. }];
  103. }
  104.  
  105.  
  106. + (void)listFolderContinueWithClient:(DBUserClient *)client cursor:(NSString *)cursor array:(NSArray*)oldArray completion:(void (^)(NSArray*))listFolderFinished{
  107. [[client.filesRoutes listFolderContinue:cursor] setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderContinueError *routeError, DBRequestError *error) {
  108. if (response) {
  109. NSArray<DBFILESMetadata *> *entries = [response.entries arrayByAddingObjectsFromArray:oldArray];
  110.  
  111. NSString *cursor = response.cursor;
  112. BOOL hasMore = [response.hasMore boolValue];
  113.  
  114.  
  115. if (hasMore) {
  116. [self listFolderContinueWithClient:client cursor:cursor array:entries completion:listFolderFinished];
  117. } else {
  118. listFolderFinished(entries);
  119. NSLog(@"List folder complete.");
  120. }
  121. } else {
  122. NSLog(@"%@\n%@\n", routeError, error);
  123. }
  124. }];
  125. }
  126.  
  127.  
  128. + (void)uploadFile:(NSString *)localPath remotePath:(NSString *)remotepath{
  129. DBUserClient *client = [DropboxAccountManager getDropboxClient];
  130. DBFILESWriteMode *mode = [[DBFILESWriteMode alloc] initWithOverwrite];
  131.  
  132. NSData *fileData = [@"file data exampleasdsadsad" dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
  133.  
  134. [[client.filesRoutes uploadData:remotepath mode:mode autorename:false clientModified:nil mute: false inputData:fileData]
  135. setResponseBlock:^(DBFILESFileMetadata *result, DBFILESUploadError *routeError, DBRequestError *error) {
  136. if (result) {
  137. NSLog(@"%@\n", result);
  138. } else {
  139. NSLog(@"%@\n%@\n", routeError, error);
  140. }
  141. }];
  142. }
  143.  
  144. + (void)batchUploadFiles:(NSString *)localPath remotePath:(NSString *)remotepath {
  145. DBUserClient *client = [DropboxAccountManager getDropboxClient];
  146. DBFILESWriteMode *mode = [[DBFILESWriteMode alloc] initWithOverwrite];
  147. NSFileManager *fileManager = [NSFileManager defaultManager];
  148. NSString *workingDirectoryName = @"/data";
  149. NSURL *workingDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0]
  150. URLByAppendingPathComponent:workingDirectoryName];
  151.  
  152. [fileManager createDirectoryAtPath:[workingDirectory path]
  153. withIntermediateDirectories:YES
  154. attributes:nil
  155. error:nil];
  156.  
  157. NSMutableDictionary<NSURL *, DBFILESCommitInfo *> *uploadFilesUrlsToCommitInfo = [NSMutableDictionary new];
  158.  
  159. NSLog(@"\n\nCreating files in: %@\n\n", [workingDirectory path]);
  160. // create a bunch of fake files
  161. for (int i = 0; i < 10; i++) {
  162. NSString *fileName = [NSString stringWithFormat:@":test_file_%d", i];
  163. NSString *fileContent = [NSString stringWithFormat:@"%@'s content. Test content here sdsdsdsd.", fileName];
  164. NSURL *fileUrl = [workingDirectory URLByAppendingPathComponent:fileName];
  165.  
  166. // set to test large file
  167. BOOL testLargeFile = NO;
  168.  
  169. // don't create a file for the name test_file_5 so we use a custom large file
  170. // there instead
  171. if (i != 5 || !testLargeFile) {
  172. NSError *fileCreationError;
  173. [fileContent writeToFile:[fileUrl path] atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&fileCreationError];
  174.  
  175. if (fileCreationError) {
  176. NSLog(@"Error creating file: %@", fileCreationError);
  177. }
  178. } else {
  179. if (![fileManager fileExistsAtPath:[fileUrl path]]) {
  180. NSLog(@"\n\nPlease create a large file named %@ to test chunked uploading\n\n", [fileUrl lastPathComponent]);
  181. }
  182. }
  183.  
  184. DBFILESCommitInfo *commitInfo = [[DBFILESCommitInfo alloc] initWithPath:[NSString stringWithFormat:@"%@/%@", @"/data", fileName] mode:mode autorename:false clientModified:nil mute:false];
  185. [uploadFilesUrlsToCommitInfo setObject:commitInfo forKey:fileUrl];
  186. }
  187.  
  188. [client.filesRoutes batchUploadFiles:uploadFilesUrlsToCommitInfo queue:nil progressBlock:^(int64_t uploaded, int64_t uploadedTotal, int64_t expectedToUploadTotal) {
  189.  
  190. NSLog(@"Uploaded: %lld UploadedTotal: %lld ExpectedToUploadTotal: %lld", uploaded, uploadedTotal, expectedToUploadTotal);
  191. } responseBlock:^(NSDictionary<NSURL *,DBFILESUploadSessionFinishBatchResultEntry *> *fileUrlsToBatchResultEntries,
  192. DBASYNCPollError *finishBatchRouteError, DBRequestError *finishBatchRequestError,
  193. NSDictionary<NSURL *,DBRequestError *> *fileUrlsToRequestErrors) {
  194. if (fileUrlsToBatchResultEntries) {
  195. for (NSURL *clientSideFileUrl in fileUrlsToBatchResultEntries) {
  196. DBFILESUploadSessionFinishBatchResultEntry *resultEntry = fileUrlsToBatchResultEntries[clientSideFileUrl];
  197. if ([resultEntry isSuccess]) {
  198. NSString *dropboxFilePath = resultEntry.success.pathDisplay;
  199. NSLog(@"File successfully uploaded from %@ on local machine to %@ in Dropbox.",
  200. [clientSideFileUrl absoluteString], dropboxFilePath);
  201. } else if ([resultEntry isFailure]) {
  202. // This particular file was not uploaded successfully, although the other
  203. // files may have been uploaded successfully. Perhaps implement some retry
  204. // logic here based on `uploadError`
  205. DBRequestError *uploadNetworkError = fileUrlsToRequestErrors[clientSideFileUrl];
  206. DBFILESUploadSessionFinishError *uploadSessionFinishError = resultEntry.failure;
  207.  
  208. NSLog(@"%@\n", uploadNetworkError);
  209. NSLog(@"%@\n", uploadSessionFinishError);
  210. }
  211. }
  212. } else if (finishBatchRouteError) {
  213. NSLog(@"Either bug in SDK code, or transient error on Dropbox server: %@", finishBatchRouteError);
  214. } else if (finishBatchRequestError) {
  215. NSLog(@"Request error from calling `/upload_session/finish_batch/check`");
  216. NSLog(@"%@", finishBatchRequestError);
  217. } else if ([fileUrlsToRequestErrors count] > 0) {
  218. NSLog(@"Other additional errors (e.g. file doesn't exist client-side, etc.).");
  219. NSLog(@"%@", fileUrlsToRequestErrors);
  220. }
  221. }];
  222. }
  223.  
  224. + (void)downloadFile:(NSString *)remotePath {
  225. dispatch_queue_t queue_a = dispatch_queue_create("dowloadQueue", DISPATCH_QUEUE_CONCURRENT);
  226.  
  227. for (int i = 0; i < 10; i++){
  228. dispatch_async(queue_a, ^{
  229.  
  230.  
  231. NSLog(@"A - %i", i);
  232. });
  233.  
  234.  
  235. }
  236.  
  237. }
  238. + (void)printEntries:(NSArray<DBFILESMetadata *> *)entries {
  239.  
  240. int size = [entries count];
  241. NSLog(@"there are %d entries in the array", size);
  242.  
  243. /** for (DBFILESMetadata *entry in entries) {
  244. if ([entry isKindOfClass:[DBFILESFileMetadata class]]) {
  245. DBFILESFileMetadata *fileMetadata = (DBFILESFileMetadata *)entry;
  246. NSLog(@"File data: %@\n", fileMetadata);
  247. } else if ([entry isKindOfClass:[DBFILESFolderMetadata class]]) {
  248. DBFILESFolderMetadata *folderMetadata = (DBFILESFolderMetadata *)entry;
  249. NSLog(@"Folder data: %@\n", folderMetadata);
  250. } else if ([entry isKindOfClass:[DBFILESDeletedMetadata class]]) {
  251. DBFILESDeletedMetadata *deletedMetadata = (DBFILESDeletedMetadata *)entry;
  252. NSLog(@"Deleted data: %@\n", deletedMetadata);
  253. }
  254. }**/
  255. }
  256.  
  257.  
  258.  
  259.  
  260.  
  261. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement