Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ declare module 'react-native-document-picker' {
xls: '.xls';
xlsx: '.xlsx';
zip: '.zip .gz';
folder: 'folder';
};
};
type PlatformTypes = {
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ function pick(opts) {
);
}

if (Array.isArray(opts.type) && opts.type.length > 1) {
if (opts.type.includes('folder')) {
throw new TypeError('When type array is folder then other options are not supported');
}
}

if ('mode' in opts && !['import', 'open'].includes(opts.mode)) {
throw new TypeError('Invalid mode option: ' + opts.mode);
}
Expand Down Expand Up @@ -140,6 +146,7 @@ const Types = {
xls: '.xls',
xlsx: '.xlsx',
zip: '.zip .gz',
folder: 'folder',
},
};

Expand Down
103 changes: 87 additions & 16 deletions windows/RNDocumentPicker/RCTDocumentPickerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class RCTDocumentPickerModule : ReactContextNativeModuleBase, ILifecycleEventLis

private static readonly String E_FAILED_TO_SHOW_PICKER = "FAILED_TO_SHOW_PICKER";
private static readonly String E_DOCUMENT_PICKER_CANCELED = "DOCUMENT_PICKER_CANCELED";
private static readonly String E_UNEXPECTED_EXCEPTION = "UNEXPECTED_EXCEPTION";
private static readonly String E_FOLDER_PICKER_CANCELED = "FOLDER_PICKER_CANCELED";

private static readonly String E_UNEXPECTED_EXCEPTION = "UNEXPECTED_EXCEPTION";

private static readonly String OPTION_TYPE = "type";
private static readonly String CACHE_TYPE = "cache";
Expand Down Expand Up @@ -76,11 +78,20 @@ public void pick(JObject options, IPromise promise)
// Get file type array options
var fileTypeArray = options.Value<JArray>(OPTION_TYPE);
var cache = options.Value<Boolean>(CACHE_TYPE);

//if pick called to launch folder picker.
bool isFolderPicker = false;

// Init file type filter
if (fileTypeArray != null && fileTypeArray.Count > 0)
{
foreach (String typeString in fileTypeArray)
{
if(typeString == "folder"){
isFolderPicker = true;
break;
}

List<String> types = typeString.Split(' ').ToList();
foreach (String type in types)
{
Expand All @@ -96,33 +107,63 @@ public void pick(JObject options, IPromise promise)
openPicker.FileTypeFilter.Add("*");
}

RunOnDispatcher(async () =>
if(isFolderPicker)
{
try
var openFolderPicker = new FolderPicker();

RunOnDispatcher(async () =>
{
if (_isInForeground)
try
{
var isMultiple = options.Value<bool>(OPTION_MULIPLE);
var readContent = options.Value<bool>(OPTION_READ_CONTENT);
if (isMultiple)
if (_isInForeground)
{
await PickMultipleFileAsync(openPicker, cache, readContent, promise);
openFolderPicker.ViewMode = PickerViewMode.List;
openFolderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
openFolderPicker.FileTypeFilter.Add("*");

await PickFolderAsync(openFolderPicker, promise);
}
else
{
await PickSingleFileAsync(openPicker, cache, readContent, promise);
_pendingPicker = openPicker;
}
}
else
catch (Exception ex)
{
_pendingPicker = openPicker;
promise.Reject(E_FAILED_TO_SHOW_PICKER, ex.Message);
}
}
catch (Exception ex)
});
}
else
{
RunOnDispatcher(async () =>
{
promise.Reject(E_FAILED_TO_SHOW_PICKER, ex.Message);
}
});
try
{
if (_isInForeground)
{
var isMultiple = options.Value<bool>(OPTION_MULIPLE);
var readContent = options.Value<bool>(OPTION_READ_CONTENT);
if (isMultiple)
{
await PickMultipleFileAsync(openPicker, cache, readContent, promise);
}
else
{
await PickSingleFileAsync(openPicker, cache, readContent, promise);
}
}
else
{
_pendingPicker = openPicker;
}
}
catch (Exception ex)
{
promise.Reject(E_FAILED_TO_SHOW_PICKER, ex.Message);
}
});
}
}
catch (Exception ex) {
promise.Reject(E_UNEXPECTED_EXCEPTION, ex.Message);
Expand Down Expand Up @@ -208,6 +249,36 @@ private async Task<bool> PickSingleFileAsync(FileOpenPicker picker, Boolean cach
return true;
}

private async Task<JObject> PrepareFolder(StorageFolder folder)
{
String base64Content = null;
var basicProperties = await folder.GetBasicPropertiesAsync();

return new JObject {
{ FIELD_URI, folder.Path },
{ FIELD_FILE_COPY_URI, folder.Path },
{ FIELD_NAME, folder.Name },
{ FIELD_SIZE, basicProperties.Size},
{ FIELD_CONTENT, base64Content }
};
}

private async Task<bool> PickFolderAsync(FolderPicker picker, IPromise promise)
{
var folder = await picker.PickSingleFolderAsync().AsTask().ConfigureAwait(false);
if (folder != null)
{
JArray jarrayObj = new JArray();
jarrayObj.Add(PrepareFolder(folder).Result);
promise.Resolve(jarrayObj);
}
else
{
promise.Reject(E_FOLDER_PICKER_CANCELED, "User canceled document picker");
}

return true;
}
private void OnInvoked(Object error, Object success, ICallback callback)
{
callback.Invoke(error, success);
Expand Down