expo-document-picker 11.2.2 → 11.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -1
- package/README.md +3 -3
- package/android/build.gradle +2 -2
- package/android/src/main/java/expo/modules/documentpicker/DocumentPickerModule.kt +58 -20
- package/android/src/main/java/expo/modules/documentpicker/DocumentPickerOptions.kt +4 -1
- package/android/src/main/java/expo/modules/documentpicker/DocumentPickerResults.kt +6 -6
- package/build/ExpoDocumentPicker.web.d.ts +2 -2
- package/build/ExpoDocumentPicker.web.d.ts.map +1 -1
- package/build/ExpoDocumentPicker.web.js +4 -2
- package/build/ExpoDocumentPicker.web.js.map +1 -1
- package/build/index.d.ts +3 -3
- package/build/index.d.ts.map +1 -1
- package/build/index.js +35 -1
- package/build/index.js.map +1 -1
- package/build/types.d.ts +57 -14
- package/build/types.d.ts.map +1 -1
- package/build/types.js.map +1 -1
- package/expo-module.config.json +3 -0
- package/ios/DocumentPickerModule.swift +172 -0
- package/ios/DocumentPickerOptions.swift +68 -0
- package/ios/DocumentPickerResponse.swift +13 -0
- package/ios/DocumentPickingDelegate.swift +26 -0
- package/ios/Exceptions.swift +19 -0
- package/ios/{EXDocumentPicker.podspec → ExpoDocumentPicker.podspec} +10 -3
- package/package.json +2 -2
- package/src/ExpoDocumentPicker.web.ts +6 -4
- package/src/index.ts +44 -4
- package/src/types.ts +91 -47
- package/ios/EXDocumentPicker/EXDocumentPickerModule.h +0 -7
- package/ios/EXDocumentPicker/EXDocumentPickerModule.m +0 -257
- package/unimodule.json +0 -4
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
// Copyright 2018-present 650 Industries. All rights reserved.
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
#import <EXDocumentPicker/EXDocumentPickerModule.h>
|
|
5
|
-
#import <ExpoModulesCore/EXUtilitiesInterface.h>
|
|
6
|
-
#import <ExpoModulesCore/EXFileSystemInterface.h>
|
|
7
|
-
|
|
8
|
-
#import <UIKit/UIKit.h>
|
|
9
|
-
#import <MobileCoreServices/MobileCoreServices.h>
|
|
10
|
-
|
|
11
|
-
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
|
|
12
|
-
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
|
|
13
|
-
#endif
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
|
|
17
|
-
API_AVAILABLE(ios(14.0))
|
|
18
|
-
static UTType* EXConvertMimeTypeToUTType(NSString *mimeType)
|
|
19
|
-
{
|
|
20
|
-
// UTType#typeWithMIMEType doesn't work with wildcard mimetypes
|
|
21
|
-
// so support common top level types with wildcards here.
|
|
22
|
-
if ([mimeType isEqualToString:@"*/*"]) {
|
|
23
|
-
return UTTypeItem;
|
|
24
|
-
} else if ([mimeType isEqualToString:@"image/*"]) {
|
|
25
|
-
return UTTypeImage;
|
|
26
|
-
} else if ([mimeType isEqualToString:@"video/*"]) {
|
|
27
|
-
return UTTypeMovie;
|
|
28
|
-
} else if ([mimeType isEqualToString:@"audio/*"]) {
|
|
29
|
-
return UTTypeAudio;
|
|
30
|
-
} else if ([mimeType isEqualToString:@"text/*"]) {
|
|
31
|
-
return UTTypeText;
|
|
32
|
-
} else {
|
|
33
|
-
return [UTType typeWithMIMEType:mimeType];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
#endif
|
|
37
|
-
|
|
38
|
-
// Deprecated in iOS 14
|
|
39
|
-
static NSString * EXConvertMimeTypeToUTI(NSString *mimeType)
|
|
40
|
-
{
|
|
41
|
-
CFStringRef uti;
|
|
42
|
-
|
|
43
|
-
// UTTypeCreatePreferredIdentifierForTag doesn't work with wildcard mimetypes
|
|
44
|
-
// so support common top level types with wildcards here.
|
|
45
|
-
if ([mimeType isEqualToString:@"*/*"]) {
|
|
46
|
-
uti = kUTTypeItem;
|
|
47
|
-
} else if ([mimeType isEqualToString:@"image/*"]) {
|
|
48
|
-
uti = kUTTypeImage;
|
|
49
|
-
} else if ([mimeType isEqualToString:@"video/*"]) {
|
|
50
|
-
uti = kUTTypeMovie;
|
|
51
|
-
} else if ([mimeType isEqualToString:@"audio/*"]) {
|
|
52
|
-
uti = kUTTypeAudio;
|
|
53
|
-
} else if ([mimeType isEqualToString:@"text/*"]) {
|
|
54
|
-
uti = kUTTypeText;
|
|
55
|
-
} else {
|
|
56
|
-
CFStringRef mimeTypeRef = (__bridge CFStringRef)mimeType;
|
|
57
|
-
uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeTypeRef, NULL);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return (__bridge_transfer NSString *)uti;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
@interface EXDocumentPickerModule () <UIDocumentPickerDelegate, UIAdaptivePresentationControllerDelegate>
|
|
64
|
-
|
|
65
|
-
@property (nonatomic, weak) EXModuleRegistry *moduleRegistry;
|
|
66
|
-
@property (nonatomic, weak) id<EXFileSystemInterface> fileSystem;
|
|
67
|
-
@property (nonatomic, weak) id<EXUtilitiesInterface> utilities;
|
|
68
|
-
|
|
69
|
-
@property (nonatomic, strong) EXPromiseResolveBlock resolve;
|
|
70
|
-
@property (nonatomic, strong) EXPromiseRejectBlock reject;
|
|
71
|
-
|
|
72
|
-
@property (nonatomic, assign) BOOL shouldCopyToCacheDirectory;
|
|
73
|
-
|
|
74
|
-
@end
|
|
75
|
-
|
|
76
|
-
@implementation EXDocumentPickerModule
|
|
77
|
-
|
|
78
|
-
EX_EXPORT_MODULE(ExpoDocumentPicker);
|
|
79
|
-
|
|
80
|
-
- (void)setModuleRegistry:(EXModuleRegistry *)moduleRegistry
|
|
81
|
-
{
|
|
82
|
-
_moduleRegistry = moduleRegistry;
|
|
83
|
-
|
|
84
|
-
if (_moduleRegistry != nil) {
|
|
85
|
-
_fileSystem = [moduleRegistry getModuleImplementingProtocol:@protocol(EXFileSystemInterface)];
|
|
86
|
-
_utilities = [moduleRegistry getModuleImplementingProtocol:@protocol(EXUtilitiesInterface)];
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
EX_EXPORT_METHOD_AS(getDocumentAsync,
|
|
91
|
-
options:(NSDictionary *)options
|
|
92
|
-
resolve:(EXPromiseResolveBlock)resolve
|
|
93
|
-
reject:(EXPromiseRejectBlock)reject)
|
|
94
|
-
{
|
|
95
|
-
if (_resolve != nil) {
|
|
96
|
-
return reject(@"E_DOCUMENT_PICKER", @"Different document picking in progress. Await other document picking first.", nil);
|
|
97
|
-
}
|
|
98
|
-
_resolve = resolve;
|
|
99
|
-
_reject = reject;
|
|
100
|
-
|
|
101
|
-
NSArray *mimeTypes = options[@"type"] ?: @[@"*/*"];
|
|
102
|
-
if (mimeTypes.count == 0) {
|
|
103
|
-
reject(@"E_DOCUMENT_PICKER", @"type must be a list of strings.", nil);
|
|
104
|
-
_resolve = nil;
|
|
105
|
-
_reject = nil;
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
_shouldCopyToCacheDirectory = options[@"copyToCacheDirectory"] && [options[@"copyToCacheDirectory"] boolValue] == YES;
|
|
109
|
-
|
|
110
|
-
EX_WEAKIFY(self);
|
|
111
|
-
|
|
112
|
-
dispatch_async(dispatch_get_main_queue(), ^{
|
|
113
|
-
EX_ENSURE_STRONGIFY(self);
|
|
114
|
-
UIDocumentPickerViewController *documentPickerVC;
|
|
115
|
-
|
|
116
|
-
@try {
|
|
117
|
-
// TODO: drop #if macro once Xcode is updated to 12
|
|
118
|
-
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
|
|
119
|
-
if (@available(iOS 14, *)) {
|
|
120
|
-
NSMutableArray *utTypes = [mimeTypes mutableCopy];
|
|
121
|
-
for (int i = 0; i < [mimeTypes count]; i++) {
|
|
122
|
-
utTypes[i] = (NSString *)EXConvertMimeTypeToUTType(mimeTypes[i]);
|
|
123
|
-
}
|
|
124
|
-
documentPickerVC = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:utTypes asCopy:true];
|
|
125
|
-
} else {
|
|
126
|
-
#endif
|
|
127
|
-
NSMutableArray *utiTypes = [mimeTypes mutableCopy];
|
|
128
|
-
for (int i = 0; i < [mimeTypes count]; i++) {
|
|
129
|
-
utiTypes[i] = (NSString *)EXConvertMimeTypeToUTI(mimeTypes[i]);
|
|
130
|
-
}
|
|
131
|
-
documentPickerVC = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:utiTypes
|
|
132
|
-
inMode:UIDocumentPickerModeImport];
|
|
133
|
-
// TODO: drop #if macro once Xcode is updated to 12
|
|
134
|
-
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
|
|
135
|
-
}
|
|
136
|
-
#endif
|
|
137
|
-
}
|
|
138
|
-
@catch (NSException *exception) {
|
|
139
|
-
reject(@"E_PICKER_ICLOUD", @"DocumentPicker requires the iCloud entitlement. If you are using ExpoKit, you need to add this capability to your App Id. See `https://docs.expo.dev/versions/latest/expokit/advanced-expokit-topics#using-documentpicker` for more info.", nil);
|
|
140
|
-
self->_resolve = nil;
|
|
141
|
-
self->_reject = nil;
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
documentPickerVC.delegate = self;
|
|
145
|
-
documentPickerVC.presentationController.delegate = self;
|
|
146
|
-
|
|
147
|
-
// Because of the way IPad works with Actionsheets such as this one, we need to provide a source view and set it's position.
|
|
148
|
-
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
149
|
-
documentPickerVC.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX([self->_utilities.currentViewController.view frame]), CGRectGetMaxY([self->_utilities.currentViewController.view frame]), 0, 0);
|
|
150
|
-
documentPickerVC.popoverPresentationController.sourceView = self->_utilities.currentViewController.view;
|
|
151
|
-
documentPickerVC.modalPresentationStyle = UIModalPresentationPageSheet;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
[self->_utilities.currentViewController presentViewController:documentPickerVC animated:YES completion:nil];
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
|
|
159
|
-
{
|
|
160
|
-
NSError *fileSizeError = nil;
|
|
161
|
-
unsigned long long fileSize = [EXDocumentPickerModule getFileSize:[url path] error:&fileSizeError];
|
|
162
|
-
if (fileSizeError) {
|
|
163
|
-
_reject(@"E_INVALID_FILE", @"Unable to get file size", fileSizeError);
|
|
164
|
-
_resolve = nil;
|
|
165
|
-
_reject = nil;
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
NSURL *newUrl = url;
|
|
170
|
-
if (_shouldCopyToCacheDirectory) {
|
|
171
|
-
if (!_fileSystem) {
|
|
172
|
-
_reject(@"E_CANNOT_PICK_FILE", @"No FileSystem module.", nil);
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
NSString *directory = [_fileSystem.cachesDirectory stringByAppendingPathComponent:@"DocumentPicker"];
|
|
176
|
-
NSString *extension = [url pathExtension];
|
|
177
|
-
NSString *path = [_fileSystem generatePathInDirectory:directory withExtension:[extension isEqualToString:@""] ? extension : [@"." stringByAppendingString:extension]];
|
|
178
|
-
NSError *error = nil;
|
|
179
|
-
newUrl = [NSURL fileURLWithPath:path];
|
|
180
|
-
[[NSFileManager defaultManager] copyItemAtURL:url toURL:newUrl error:&error];
|
|
181
|
-
if (error != nil) {
|
|
182
|
-
self.reject(@"E_CANNOT_PICK_FILE", @"File could not be saved to app storage", error);
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
NSString *extension = [url pathExtension];
|
|
188
|
-
NSString *mimeType = [EXDocumentPickerModule getMimeType:extension];
|
|
189
|
-
|
|
190
|
-
NSMutableDictionary *response = [@{
|
|
191
|
-
@"type": @"success",
|
|
192
|
-
@"uri": [newUrl absoluteString],
|
|
193
|
-
@"name": [url lastPathComponent],
|
|
194
|
-
@"size": @(fileSize)
|
|
195
|
-
} mutableCopy];
|
|
196
|
-
|
|
197
|
-
if (mimeType != nil) {
|
|
198
|
-
response[@"mimeType"] = mimeType;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
_resolve(response);
|
|
202
|
-
|
|
203
|
-
_resolve = nil;
|
|
204
|
-
_reject = nil;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// Document picker view controller has been cancelled with a button
|
|
208
|
-
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
|
|
209
|
-
{
|
|
210
|
-
_resolve(@{@"type": @"cancel"});
|
|
211
|
-
_resolve = nil;
|
|
212
|
-
_reject = nil;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Document picker view controller has been dismissed by gesture
|
|
216
|
-
- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController
|
|
217
|
-
{
|
|
218
|
-
[self documentPickerWasCancelled:presentationController.presentedViewController];
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
+ (unsigned long long)getFileSize:(NSString *)path error:(NSError **)error
|
|
222
|
-
{
|
|
223
|
-
NSDictionary<NSFileAttributeKey, id> *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:error];
|
|
224
|
-
if (*error) {
|
|
225
|
-
return 0;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (fileAttributes.fileType != NSFileTypeDirectory) {
|
|
229
|
-
return fileAttributes.fileSize;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// The path is pointing to the folder
|
|
233
|
-
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:error];
|
|
234
|
-
if (*error) {
|
|
235
|
-
return 0;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
NSEnumerator *contentsEnumurator = [contents objectEnumerator];
|
|
239
|
-
NSString *file;
|
|
240
|
-
unsigned long long folderSize = 0;
|
|
241
|
-
while (file = [contentsEnumurator nextObject]) {
|
|
242
|
-
folderSize += [EXDocumentPickerModule getFileSize:[path stringByAppendingPathComponent:file] error:error];
|
|
243
|
-
if (*error) {
|
|
244
|
-
return 0;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return folderSize;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
+ (NSString *)getMimeType:(NSString *)fileExtension{
|
|
252
|
-
NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExtension, NULL);
|
|
253
|
-
NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);
|
|
254
|
-
return mimeType;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
@end
|
package/unimodule.json
DELETED