-
Notifications
You must be signed in to change notification settings - Fork 71
/
ODBoxHandler.m
327 lines (268 loc) · 17.6 KB
/
ODBoxHandler.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//
// ODBoxHandler.m
// OpenDropboxBrowser
//
// Created by Sam Spencer on 2/10/17.
// Copyright © 2017 Spencer Software. All rights reserved.
//
#import "ODBoxHandler.h"
@import MobileCoreServices;
const struct ODBFileDictionaryKeys ODBFileKeys = {
.kDropboxFileType = @"type",
.kDropboxFileTypeFile = @"file",
.kDropboxFileTypeFolder = @"folder",
.kDropboxFileName = @"name",
.kDropboxFileSize = @"size",
.kDropboxFileModifiedDate = @"modified",
.kDropboxFileIcon = @"icon"
};
@interface ODBoxHandler ()
/// The client object returned from the SDK's authentication process if it was successful. This value may be nil if the client is not authenticated or there was an error.
@property (nonatomic, strong, nullable) DBUserClient *mainClient;
/// The app's Dropbox Authorization key.
@property (nonatomic, strong, nullable) NSString *appAuthorizationKey;
@end
@implementation ODBoxHandler
// MARK: -
// MARK: Object lifecycle
+ (ODBoxHandler *)sharedHandler {
static ODBoxHandler *singleton;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [[self alloc] init];
});
return singleton;
}
- (instancetype)init {
self = [super init];
if (self) {
// Perform setup operations
_mainClient = [DBClientsManager authorizedClient];
}
return self;
}
// MARK: -
// MARK: Authentication
- (void)prepareForPotentialSessionWithKey:(NSString *)appKey {
[DBClientsManager setupWithAppKey:appKey];
self.appAuthorizationKey = appKey;
self.mainClient = [DBClientsManager authorizedClient];
}
- (void)handleDropboxAuthenticationResponse:(NSURL *)applicationReceivedURL {
DBOAuthResult *authResult = [DBClientsManager handleRedirectURL:applicationReceivedURL];
if (authResult != nil) {
if ([authResult isSuccess]) {
NSLog(@"[ODBoxHandler] Authorization success. User is logged into Dropbox.");
self.mainClient = [DBClientsManager authorizedClient];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ODBoxHandler.authentication.success" object:nil];
} else if ([authResult isCancel]) {
NSLog(@"[ODBoxHandler] Authorization cancelled. Flow was manually canceled by user.");
[[NSNotificationCenter defaultCenter] postNotificationName:@"ODBoxHandler.authentication.cancelled" object:nil];
} else if ([authResult isError]) {
NSLog(@"[ODBoxHandler] Authorization error. %@", authResult);
[[NSNotificationCenter defaultCenter] postNotificationName:@"ODBoxHandler.authentication.error" object:nil];
}
}
}
- (BOOL)clientIsAuthenticated {
self.mainClient = [DBClientsManager authorizedClient];
if (self.mainClient) return YES;
else return NO;
}
- (void)clientRequestedLogout {
NSLog(@"[ODBoxHandler] Unlinking accounts and logging out of Dropbox...");
[DBClientsManager unlinkAndResetClients];
}
- (BOOL)applicationIsConfiguredForAuthorization {
// First, check if there is a valid authoirzation key.
if (self.appAuthorizationKey == nil || [self.appAuthorizationKey isEqualToString:@""] || [self.appAuthorizationKey isEqualToString:@"APP_KEY"]) {
NSLog(@"\n\n[ODBoxHandler] WARNING: The application has not specified an authorization key to be used with the Dropbox API. If you have not done so already, please visit https://www.dropbox.com/developers/apps and register your application. To set your app key for OpenDropboxBrowser, call prepareForPotentialSessionWithKey: in your app's didFinishLaunchingWithOptions: callback and supply your key in the method parameter.\n\n Failure to properly setup OpenDropboxBrowser before displaying the Browser Controller to the user will result in an incompatibility alert.\n\n");
return NO;
}
// Check if the app's Transport Security Protocols are up to date.
NSArray *appQuerySchemes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"LSApplicationQueriesSchemes"];
if (appQuerySchemes == nil || appQuerySchemes.count == 0) {
// No LSApplicationQueriesSchemes have been added.
NSLog(@"\n\n[ODBoxHandler] WARNING: The application has not specified any LSApplicationQueriesSchemes in its Info.plist file. Add the following entry to your Info.plist file:\n <key>LSApplicationQueriesSchemes</key>\n <array>\n <string>dbapi-8-emm</string>\n <string>dbapi-2</string>\n </array>\n\n Failure to properly setup OpenDropboxBrowser before displaying the Browser Controller to the user will result in an incompatibility alert.\n\n");
return NO;
} else {
// Some LSApplicationQueriesSchemes have been added. We need to check if they are the correct entries.
NSInteger appropriateAppQueryEntries = 0;
for (NSString *entry in appQuerySchemes) {
if ([entry isEqualToString:@"dbapi-8-emm"]) appropriateAppQueryEntries++;
else if ([entry isEqualToString:@"dbapi-2"]) appropriateAppQueryEntries++;
}
if (appropriateAppQueryEntries < 2) {
// The app has not supplied the correct entries in the Info.plist file.
NSLog(@"\n\n[ODBoxHandler] WARNING: The application has not specified the correct entries for LSApplicationQueriesSchemes in its Info.plist file. Ensure the following two entries have been added to the LSApplicationQueriesSchemes array in your Info.plist file:\n <string>dbapi-8-emm</string>\n <string>dbapi-2</string>\n\n Failure to properly setup OpenDropboxBrowser before displaying the Browser Controller to the user will result in an incompatibility alert.\n\n");
return NO;
}
}
// Check if the app's URL callbacks match its app key.
NSArray *appURLTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
if (appURLTypes == nil || appURLTypes.count == 0) {
// No CFBundleURLTypes have been added.
NSLog(@"\n\n[ODBoxHandler] WARNING: The application has not specified any CFBundleURLTypes in its Info.plist file. Add the following entry to your Info.plist file:\n <key>CFBundleURLTypes</key>\n <dict>\n <key>CFBundleURLSchemes</key>\n <array>\n <string>db-<APP_KEY></string>\n </array>\n <key>CFBundleURLName</key>\n <string></string>\n </dict>\n </array>\n\n Failure to properly setup OpenDropboxBrowser before displaying the Browser Controller to the user will result in an incompatibility alert.\n\n");
return NO;
} else {
// Some CFBundleURLTypes have been added. We need to check if there is a correct entry.
BOOL appropriateAppURLEntry = NO;
NSString *appropriateURLScheme = [NSString stringWithFormat:@"db-%@", self.appAuthorizationKey];
for (NSDictionary *URLSchema in appURLTypes) {
NSArray *URLSchemes = URLSchema[@"CFBundleURLSchemes"];
for (NSString *URLScheme in URLSchemes) {
if ([URLScheme isEqualToString:appropriateURLScheme]) {
appropriateAppURLEntry = YES;
break;
}
}
// If we've found the right entry we can stop looping.
if (appropriateAppURLEntry == YES) break;
}
if (appropriateAppURLEntry == NO) {
// The app has not supplied the correct entries in the Info.plist file.
NSLog(@"\n\n[ODBoxHandler] WARNING: The application has not specified the correct URL for CFBundleURLTypes in its Info.plist file. Ensure the following entry has been added to a CFBundleURLScheme in the CFBundleURLTypes array in your Info.plist file:\n <string>db-%@</string>\n\n Failure to properly setup OpenDropboxBrowser before displaying the Browser Controller to the user will result in an incompatibility alert.\n\n", self.appAuthorizationKey);
return NO;
}
}
// At this point, if the method has not already returned NO, the app should be capable of passing authorization.
return YES;
}
// MARK: -
// MARK: Downloads
- (void)downloadDropboxFile:(NSString *)file completion:(void (^)(NSURL *filePath, NSError *error))finishBlock updateProgress:(void (^)(NSNumber *progress))progressChanged {
// Download to NSURL
NSURL *outputDirectory = nil;
if (self.customDownloadDirectory == nil)
outputDirectory = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask][0];
else
outputDirectory = self.customDownloadDirectory;
NSString *fileName = [file lastPathComponent];
NSURL *outputURL = [outputDirectory URLByAppendingPathComponent:fileName];
[[[self.mainClient.filesRoutes downloadUrl:file overwrite:self.downloadsOverwriteLocalConflicts destination:outputURL] setResponseBlock:^(DBFILESFileMetadata * _Nullable result, DBFILESDownloadError * _Nullable routeError, DBRequestError * _Nullable networkError, NSURL * _Nonnull destination) {
if (result) {
NSLog(@"[ODBoxHandler] %@\n", result);
NSData *data = [[NSFileManager defaultManager] contentsAtPath:[destination path]];
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"[ODBoxHandler] %@\n", dataStr);
finishBlock(outputURL, nil);
if ([self.delegate respondsToSelector:@selector(dropboxHandler:didFinishDownloadingFile:atURL:data:)])
[self.delegate dropboxHandler:self didFinishDownloadingFile:fileName atURL:outputURL data:nil];
} else {
NSLog(@"[ODBoxHandler] %@\n%@\n", routeError, networkError);
finishBlock(nil, networkError.nsError);
if ([self.delegate respondsToSelector:@selector(dropboxHandler:didFailToDownloadFile:error:)])
[self.delegate dropboxHandler:self didFailToDownloadFile:fileName error:networkError.nsError];
// if ([self.delegate respondsToSelector:@selector(finishedDownloadingFileToLocalURL:)])
// [self.delegate downloadEncounteredError:error];
}
}] setProgressBlock:^(int64_t bytesDownloaded, int64_t totalBytesDownloaded, int64_t totalBytesExpectedToDownload) {
CGFloat progressFloat = (CGFloat)totalBytesDownloaded / (CGFloat)totalBytesExpectedToDownload;
NSNumber *downloadProgressFloat = [NSNumber numberWithFloat:progressFloat];
NSLog(@"[ODBoxHandler] Downloading... %.02f%%", progressFloat * 100);
progressChanged(downloadProgressFloat);
}];
}
- (void)downloadDropboxFileData:(NSString *)file completion:(void (^)(NSData *fileData, NSError *error))finishBlock updateProgress:(void (^)(NSNumber *progress))progressChanged {
NSString *fileName = [[file lastPathComponent] stringByDeletingPathExtension];
// Download to NSData
[[[self.mainClient.filesRoutes downloadData:file] setResponseBlock:^(DBFILESFileMetadata * _Nullable result, DBFILESDownloadError * _Nullable routeError, DBRequestError * _Nullable networkError, NSData * _Nullable fileData) {
if (result) {
NSLog(@"[ODBoxHandler] %@\n", result);
NSString *dataStr = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
NSLog(@"[ODBoxHandler] %@\n", dataStr);
finishBlock(fileData, nil);
if ([self.delegate respondsToSelector:@selector(dropboxHandler:didFinishDownloadingFile:atURL:data:)])
[self.delegate dropboxHandler:self didFinishDownloadingFile:fileName atURL:nil data:fileData];
} else {
finishBlock(nil, networkError.nsError);
NSLog(@"[ODBoxHandler] %@\n%@\n", routeError, networkError);
if ([self.delegate respondsToSelector:@selector(dropboxHandler:didFailToDownloadFile:error:)])
[self.delegate dropboxHandler:self didFailToDownloadFile:fileName error:networkError.nsError];
}
}] setProgressBlock:^(int64_t bytesDownloaded, int64_t totalBytesDownloaded, int64_t totalBytesExpectedToDownload) {
CGFloat progressFloat = (CGFloat)totalBytesDownloaded / (CGFloat)totalBytesExpectedToDownload;
NSNumber *downloadProgressFloat = [NSNumber numberWithFloat:progressFloat];
NSLog(@"[ODBoxHandler] Downloading... %.02f%%", progressFloat * 100);
progressChanged(downloadProgressFloat);
}];
}
// MARK: -
// MARK: Files
- (void)fetchFileListsInDirectory:(NSString *)parentDirectory completion:(void (^)(NSArray *files, NSError *error))finishBlock {
NSLog(@"[ODBoxHandler] Beginning file fetch...");
if ([parentDirectory isEqualToString:@"/"]) parentDirectory = @"";
[[self.mainClient.filesRoutes listFolder:parentDirectory recursive:@NO includeMediaInfo:@NO includeDeleted:@NO includeHasExplicitSharedMembers:@NO] setResponseBlock:^(DBFILESListFolderResult * _Nullable result, DBFILESListFolderError * _Nullable routeError, DBRequestError * _Nullable networkError) {
NSLog(@"[ODBoxHandler] Returned from file fetch.");
if (result) {
NSLog(@"[ODBoxHandler] New file list with %i entries", (int)result.entries.count);
NSMutableArray *newFileList = [NSMutableArray arrayWithCapacity:result.entries.count];
for (DBFILESMetadata *file in result.entries) {
NSDictionary *fileEntry;
if ([file isKindOfClass:[DBFILESFileMetadata class]]) {
// We have a file
DBFILESFileMetadata *fileObject = (DBFILESFileMetadata *)file;
NSString *fileIconName = [self fileIconForFileName:fileObject.name];
fileEntry = @{ODBFileKeys.kDropboxFileType : ODBFileKeys.kDropboxFileTypeFile, ODBFileKeys.kDropboxFileName : fileObject.name, ODBFileKeys.kDropboxFileSize : fileObject.size, ODBFileKeys.kDropboxFileModifiedDate : fileObject.serverModified, ODBFileKeys.kDropboxFileIcon : fileIconName};
} else {
// We have a directory
DBFILESFolderMetadata *folder = (DBFILESFolderMetadata *)file;
fileEntry = @{ODBFileKeys.kDropboxFileType : ODBFileKeys.kDropboxFileTypeFolder, ODBFileKeys.kDropboxFileName : folder.name, ODBFileKeys.kDropboxFileIcon : @"folder"};
}
[newFileList addObject:fileEntry];
}
finishBlock(newFileList.copy, nil);
} else {
NSLog(@"[ODBoxHandler] Error fetching files: %@", networkError);
finishBlock(nil, networkError.nsError);
}
}];
}
- (void)searchFileListsInDirectory:(NSString *)parentDirectory query:(NSString *)query completion:(void (^)(NSArray *files, NSError *error))finishBlock {
[[self.mainClient.filesRoutes search:parentDirectory query:query] setResponseBlock:^(DBFILESSearchResult * _Nullable result, DBFILESSearchError * _Nullable routeError, DBRequestError * _Nullable networkError) {
if (result) {
NSMutableArray *matchList = [NSMutableArray arrayWithCapacity:result.matches.count];
for (DBFILESSearchMatch *match in result.matches) {
NSDictionary *fileEntry;
if ([match.metadata isKindOfClass:[DBFILESFileMetadata class]]) {
// We have a file
DBFILESFileMetadata *fileObject = (DBFILESFileMetadata *)match.metadata;
NSString *fileIconName = [self fileIconForFileName:fileObject.name];
fileEntry = @{ODBFileKeys.kDropboxFileType : ODBFileKeys.kDropboxFileTypeFile, ODBFileKeys.kDropboxFileName: fileObject.name, ODBFileKeys.kDropboxFileSize : fileObject.size, ODBFileKeys.kDropboxFileModifiedDate : fileObject.serverModified, ODBFileKeys.kDropboxFileIcon : fileIconName};
} else {
// We have a directory
DBFILESFolderMetadata *folder = (DBFILESFolderMetadata *)match.metadata;
fileEntry = @{ODBFileKeys.kDropboxFileType : ODBFileKeys.kDropboxFileTypeFolder, ODBFileKeys.kDropboxFileName : folder.name, ODBFileKeys.kDropboxFileIcon : @"folder"};
}
[matchList addObject:fileEntry];
}
finishBlock(matchList.copy, nil);
} else {
finishBlock(nil, networkError.nsError);
}
}];
}
+ (NSString *)encodeFolderPath:(NSString *)folder currentPath:(NSString *)path {
return [NSString stringWithFormat:@"%@/%@", path, folder];;
}
- (NSString *)fileIconForFileName:(NSString *)fileName {
CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
NSString *fileIconName;
if (UTTypeConformsTo(fileUTI, kUTTypeImage)) fileIconName = @"image";
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) fileIconName = @"movie";
else if (UTTypeConformsTo(fileUTI, kUTTypeAudio)) fileIconName = @"audio";
else if (UTTypeConformsTo(fileUTI, kUTTypeText)) fileIconName = @"text";
else if (UTTypeConformsTo(fileUTI, kUTTypeSpreadsheet)) fileIconName = @"spreadsheet";
else if (UTTypeConformsTo(fileUTI, kUTTypePresentation)) fileIconName = @"presentation";
else if (UTTypeConformsTo(fileUTI, kUTTypePDF)) fileIconName = @"pdf";
else if (UTTypeConformsTo(fileUTI, kUTTypeScalableVectorGraphics)) fileIconName = @"vector";
else if (UTTypeConformsTo(fileUTI, kUTTypeArchive)) fileIconName = @"package";
else if (UTTypeConformsTo(fileUTI, kUTTypeDiskImage)) fileIconName = @"disc";
else if (UTTypeConformsTo(fileUTI, kUTTypeSourceCode)) fileIconName = @"developer";
else if (UTTypeConformsTo(fileUTI, kUTTypeSystemPreferencesPane)) fileIconName = @"settings";
else fileIconName = @"text";
CFRelease(fileUTI);
return fileIconName;
}
@end