Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1412: Picking a folder in iOS causes the original folder to be deleted. #1457

Merged
merged 4 commits into from
Apr 22, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 8.0.2
### iOS
Fixes the bug [#1412](https://github.com/miguelpruivo/flutter_file_picker/issues/1412) that picking a folder in iOS causes the original folder to be deleted.

## 8.0.1
### iOS
Fixes an issue preventing compilation on iOS when using Pod::PICKER_DOCUMENT = false.
Expand Down
40 changes: 23 additions & 17 deletions ios/Classes/FilePickerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -400,27 +400,33 @@ - (void)documentPicker:(UIDocumentPickerViewController *)controller
_result = nil;
return;
}
NSMutableArray<NSURL *> *newUrls = [NSMutableArray new];
for (NSURL *url in urls) {
// Create file URL to temporary folder
NSURL *tempURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
// Append filename (name+extension) to URL
tempURL = [tempURL URLByAppendingPathComponent:url.lastPathComponent];
NSError *error;
// If file with same name exists remove it (replace file with new one)
if ([[NSFileManager defaultManager] fileExistsAtPath:tempURL.path]) {
[[NSFileManager defaultManager] removeItemAtPath:tempURL.path error:&error];
NSMutableArray<NSURL *> *newUrls;
if(controller.documentPickerMode == UIDocumentPickerModeOpen) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentPickerMode is deprecated. Could you rewrite this to only use the deprecated API up to iOS 14 ?

See https://developer.apple.com/documentation/uikit/uidocumentpickerviewcontroller#1658475

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't have a mac device now, so I can't rewrite and test it.
In addition, I noticed that the deprecated API was also used in the previous code. Maybe this also needs to be migrated to the new API, but I am not familiar with objc.
BTW, on my own ios device, when using the init(forOpeningContentTypes:) swift api, I can directly get the absolute path of file/directory. Why doesn't file_picker use this api, but makes a cache file?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not now why file_picker uses a cache for that, maybe @miguelpruivo knows?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not now why file_picker uses a cache for that, maybe @miguelpruivo knows?

There are 2 approaches when opening descriptors for files within app's container

  1. Opening a resource directly to the file through URI/UTI or;
  2. Caching a copy of the file so it can be manipulated individually within the app and without any constraint regarding the original file;

2 is typically the preferred option since most of the apps use the files for either manipulating (editing) or, even if it's just for sending somewhere, it doesn't have any consequence. If I recall, this was the original approach from the Flutter's image_picker so I followed the same path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@navaronbracke @miguelpruivo
Please pay attention to #1412 . This bug is very serious on iOS and will endanger user data security.
It has been half a year since I raised the issue, but this dangerous bug still exists and has not been fixed.
This PR is made based on the current implementation, I know that UIDocumentPickerMode is a deprecated api, but the code in this library always uses this deprecated api when creating UIDocumentPickerViewController.
In this case, why is this PR banned because I use the same deprecated API? (In fact, another PR #1452 that was approved also used the deprecated UIDocumentPickerMode api).
I think a more reasonable solution is to first pass this PR to solve the BUG instead of banning this PR because of an deprecated api that already exists in the library.
Please allow me to repeat it again: this bug is very serious, it will delete files without the user knowing! Please don't keep this bug alive for longer.

newUrls = urls;
}
if(controller.documentPickerMode == UIDocumentPickerModeImport) {
newUrls = [NSMutableArray new];
for (NSURL *url in urls) {
// Create file URL to temporary folder
NSURL *tempURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
// Append filename (name+extension) to URL
tempURL = [tempURL URLByAppendingPathComponent:url.lastPathComponent];
NSError *error;
// If file with same name exists remove it (replace file with new one)
if ([[NSFileManager defaultManager] fileExistsAtPath:tempURL.path]) {
[[NSFileManager defaultManager] removeItemAtPath:tempURL.path error:&error];
if (error) {
NSLog(@"%@", error.localizedDescription);
}
}
// Move file from app_id-Inbox to tmp/filename
[[NSFileManager defaultManager] moveItemAtPath:url.path toPath:tempURL.path error:&error];
if (error) {
NSLog(@"%@", error.localizedDescription);
} else {
[newUrls addObject:tempURL];
}
}
// Move file from app_id-Inbox to tmp/filename
[[NSFileManager defaultManager] moveItemAtPath:url.path toPath:tempURL.path error:&error];
if (error) {
NSLog(@"%@", error.localizedDescription);
} else {
[newUrls addObject:tempURL];
}
}

[self.documentPickerController dismissViewControllerAnimated:YES completion:nil];
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A package that allows you to use a native file explorer to pick sin
homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker
repository: https://github.com/miguelpruivo/flutter_file_picker
issue_tracker: https://github.com/miguelpruivo/flutter_file_picker/issues
version: 8.0.1
version: 8.0.2

dependencies:
flutter:
Expand Down
Loading