You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Dropbox.m 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright @ 2017-present Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <React/RCTBridgeModule.h>
  17. #import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h>
  18. #import "Dropbox.h"
  19. RCTPromiseResolveBlock currentResolve = nil;
  20. RCTPromiseRejectBlock currentReject = nil;
  21. @implementation Dropbox
  22. + (NSString *)getAppKey{
  23. NSArray *urlTypes
  24. = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
  25. for (NSDictionary<NSString *, NSArray *> *urlType in urlTypes) {
  26. NSArray *urlSchemes = urlType[@"CFBundleURLSchemes"];
  27. if (urlSchemes) {
  28. for (NSString *urlScheme in urlSchemes) {
  29. if (urlScheme && [urlScheme hasPrefix:@"db-"]) {
  30. return [urlScheme substringFromIndex:3];
  31. }
  32. }
  33. }
  34. }
  35. return nil;
  36. }
  37. RCT_EXPORT_MODULE();
  38. + (BOOL)requiresMainQueueSetup {
  39. return NO;
  40. }
  41. - (NSDictionary *)constantsToExport {
  42. BOOL enabled = [Dropbox getAppKey] != nil;
  43. return @{
  44. @"ENABLED": [NSNumber numberWithBool:enabled]
  45. };
  46. };
  47. RCT_EXPORT_METHOD(authorize:(RCTPromiseResolveBlock)resolve
  48. reject:(__unused RCTPromiseRejectBlock)reject) {
  49. currentResolve = resolve;
  50. currentReject = reject;
  51. dispatch_async(dispatch_get_main_queue(), ^{
  52. DBScopeRequest *scopeRequest = [[DBScopeRequest alloc] initWithScopeType:DBScopeTypeUser
  53. scopes:@[]
  54. includeGrantedScopes:NO];
  55. [DBClientsManager authorizeFromControllerV2:[UIApplication sharedApplication]
  56. controller:[[self class] topMostController]
  57. loadingStatusDelegate:nil
  58. openURL:^(NSURL *url) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; }
  59. scopeRequest:scopeRequest];
  60. });
  61. }
  62. RCT_EXPORT_METHOD(getDisplayName: (NSString *)token
  63. resolve: (RCTPromiseResolveBlock)resolve
  64. reject:(RCTPromiseRejectBlock)reject) {
  65. DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:token];
  66. [[client.usersRoutes getCurrentAccount] setResponseBlock:^(DBUSERSFullAccount *result, DBNilObject *routeError, DBRequestError *networkError) {
  67. if (result) {
  68. resolve(result.name.displayName);
  69. } else {
  70. NSString *msg = @"Failed!";
  71. if (networkError) {
  72. msg = [NSString stringWithFormat:@"Failed! Error: %@", networkError];
  73. }
  74. reject(@"getDisplayName", @"Failed", nil);
  75. }
  76. }];
  77. }
  78. RCT_EXPORT_METHOD(getSpaceUsage: (NSString *)token
  79. resolve: (RCTPromiseResolveBlock)resolve
  80. reject:(RCTPromiseRejectBlock)reject) {
  81. DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:token];
  82. [[client.usersRoutes getSpaceUsage] setResponseBlock:^(DBUSERSSpaceUsage *result, DBNilObject *routeError, DBRequestError *networkError) {
  83. if (result) {
  84. DBUSERSSpaceAllocation *allocation = result.allocation;
  85. NSNumber *allocated = 0;
  86. NSNumber *used = 0;
  87. if ([allocation isIndividual]) {
  88. allocated = allocation.individual.allocated;
  89. used = result.used;
  90. } else if ([allocation isTeam]) {
  91. allocated = allocation.team.allocated;
  92. used = allocation.team.used;
  93. }
  94. id objects[] = { used, allocated };
  95. id keys[] = { @"used", @"allocated" };
  96. NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
  97. forKeys:keys
  98. count:2];
  99. resolve(dictionary);
  100. } else {
  101. NSString *msg = @"Failed!";
  102. if (networkError) {
  103. msg = [NSString stringWithFormat:@"Failed! Error: %@", networkError];
  104. }
  105. reject(@"getSpaceUsage", msg, nil);
  106. }
  107. }];
  108. }
  109. + (BOOL)application:(UIApplication *)app
  110. openURL:(NSURL *)url
  111. options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
  112. if (currentReject == nil || currentResolve == nil) {
  113. return NO;
  114. }
  115. BOOL canHandle = [DBClientsManager handleRedirectURL:url completion:^(DBOAuthResult *authResult) {
  116. if (authResult) {
  117. if ([authResult isSuccess]) {
  118. NSInteger msTimestamp = authResult.accessToken.tokenExpirationTimestamp * 1000;
  119. NSDictionary *authInfo = @{@"token": authResult.accessToken.accessToken,
  120. @"rToken": authResult.accessToken.refreshToken,
  121. @"expireDate": @(msTimestamp)
  122. };
  123. currentResolve(authInfo);
  124. } else {
  125. NSString *msg;
  126. if ([authResult isError]) {
  127. msg = [NSString stringWithFormat:@"%@, error type: %zd", [authResult errorDescription], [authResult errorType]];
  128. } else {
  129. msg = @"OAuth canceled!";
  130. }
  131. currentReject(@"authorize", msg, nil);
  132. }
  133. currentResolve = nil;
  134. currentReject = nil;
  135. }
  136. }];
  137. return canHandle;
  138. }
  139. + (UIViewController *)topMostController {
  140. UIViewController *topController
  141. = [UIApplication sharedApplication].keyWindow.rootViewController;
  142. while (topController.presentedViewController) {
  143. topController = topController.presentedViewController;
  144. }
  145. return topController;
  146. }
  147. + (void)setAppKey {
  148. NSString *appKey = [self getAppKey];
  149. if (appKey) {
  150. [DBClientsManager setupWithAppKey:appKey];
  151. }
  152. }
  153. @end